Re: select() not working with UDP
Re: select() not working with UDP
- Subject: Re: select() not working with UDP
- From: email@hidden
- Date: Tue, 30 Jun 2009 07:37:56 -0700
Thanks Quinn, works great now!
On Tue, Jun 30, 2009 at 2:20 AM, Quinn
<email@hidden> wrote:
At 23:48 -0700 29/6/09,
email@hidden wrote:
My simple UDP packet echoer works without using select(). But, I need to use select() as the complexity of my app increases.
The problem is that select() only returns after the timeout, but not when any UDP data has arrived.
select does work with UDP. I'm not 100% what's going on with your program, but you do seem to have fallen prey to two standard select gotchas.
o On success, select will modify the FD_SETs that you pass in to it; you really should recalculate readfds every time around your for loop.
o On success, select returns a count of the number of ready descriptors, not zero. A select loop would typically look like this
do {
int fdCount;
fdCount = select(sockfdIn+1, &readfds, NULL, NULL, &tv);
if (fdCount > 0) {
fprintf(stderr, "select returns\n");
break;
} else if (fdCount == 0) {
fprintf(stderr, "select timeout\n");
[... take timeout action ...]
} else {
if (errno != EINTR) {
fprintf(stderr, "select failed with %d\n", errno);
break;
}
}
} while (1);
I fixed both of these in your code and got it to work. One further gotcha, which confused me for a while, was the IPv4 vs IPv6 issue. Common names, like localhost or foo.local., may resolve to either IPv4 or IPv6 addresses. In production code, you should be listening on both flavours. For testing, you may want to stick with IPv4 and then force your clients to work via IPv4 (in my case it was a question of using 127.0.0.1 rather than localhost).
S+E
--
Quinn "The Eskimo!" <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden