Re: UDP and OT
Re: UDP and OT
- Subject: Re: UDP and OT
- From: Quinn <email@hidden>
- Date: Wed, 11 Dec 2002 09:55:28 +0000
At 0:09 +0000 11/12/02, email@hidden wrote:
I have playing with some of OT calls, so that I can find other machine
running my appl, so thinking of sending out an UDP message to anyone
listening on a specified port, so I thought the below code would work
EndpointRef endPoint;
InetAddress dest_addr;
TUnitData udata;
Initialize();
endPoint = OTOpenEndpointInContext( OTCreateConfiguration(kUDPName), 0,
nil, &err, NULL);
err = OTBind(endPoint, nil, nil);
OTInitInetAddress(&dest_addr, 3200, kOTAnyInetAddress);
This is wrong. If you want to broadcast, you need to use the address
255.255.255.255, not kOTAnyInetAddress, which translates to 0.
Better yet, get the subnet broadcast address from
OTInetGetInterfaceInfo.
udata.addr.len = sizeof(dest_addr);
udata.addr.maxlen = sizeof(dest_addr);
udata.addr.buf = (unsigned char *) &dest_addr;
udata.opt.len = 0;
udata.opt.maxlen = 0;
udata.opt.buf = nil;
// Does it copy the data ???
udata.udata.len = 30;
udata.udata.maxlen = 30;
udata.udata.buf = (UInt8 *) NewPtr (30);
Yes.
btw This is covered in the OT documentation. In summary, for async
requests, any input parameters are copied before the call returns,
but any output parameters must survive until the corresponding
request completion notifier.
if ( which == 1 )
{
//
// Send the data to anybody listening on Port 3200, UDP port
//
err = OTSndUData(endPoint, &udata );
// wait for replies I guess
}
else
{
//
// listening on Port 3200, UDP port, and see if anyone calls
//
OTSetBlocking(endPoint);
err = OTRcvUData(endPoint, &udata, NULL );
// Send a response back about my self.
}
OTCloseProvider(endPoint);
this code is fairly simplistic at the minute, but I just wanted to do
proof of concept first.
[...]
Also why do you need to OTBind, as you usually don't need to do a bind
unless you want specific port ???, usually when listening ???, but I got an
error before I added, when I was trying to do the OTSndUdata, of endpoint in
wrong state (-3155 I think it was)
Huh? On OT you always have to bind before sending or receiving.
Perhaps you're confusing OT with sockets, where binding is optional
for outgoing connections.
However, you should bind the client using OTBind(ep, NULL, NULL).
You don't need the client to be bound to a specific port.
You should also use the IP_BROADCAST option on the client and
probably the IP_REUSEADDR option on the server. See IM:NWOT for
details.
Finally, I recommend you stay away from broadcasting on a subnet. If
you want to do service discovery, use multicasts. Better yet, use
Rendezvous.
S+E
--
Quinn "The Eskimo!" <
http://www.apple.com/developer/>
Apple Developer Technical Support * Networking, Communications, Hardware
_______________________________________________
macnetworkprog mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/macnetworkprog
Do not post admin requests to the list. They will be ignored.