recvfrom() questions
Hi, How do I make non-blocking recvfrom() calls? I'm trying to set up a client that sends x number of UDP packets and then waits for some time to receive an ACK before timing out, but everytime I call recvfrom() it just waits there till it gets something. I googled around and it seems i need to set a timeval variable and use it with select(), but I'm not quite sure how to do that.
You have two options:
1) set the socket in non-blocking mode, so that recvfrom() will return immediately
2) using select() with a timeout, as you're suggesting
I don't know what help you're asking for -- the man page for select() (and the documentation on MSDN) is pretty clear. Try finding some sample code that uses it, perhaps, if some particular part of calling select is confusing you?
1) set the socket in non-blocking mode, so that recvfrom() will return immediately
2) using select() with a timeout, as you're suggesting
I don't know what help you're asking for -- the man page for select() (and the documentation on MSDN) is pretty clear. Try finding some sample code that uses it, perhaps, if some particular part of calling select is confusing you?
enum Bool { True, False, FileNotFound };
Thanks.
What kind of behavior does setting the socket in non-blocking mode exhibit? Will the recvfrom automatically return once it gets a msg, and block while it doesn't, or does it wait for a certain amount of time before returning?
I've checked the man page for select() but I guess I'm not sure of the parameters.
int select(int nfds, fd_set *restrict readfds,
fd_set *restrict writefds, fd_set *restrict errorfds,
struct timeval *restrict timeout);
Other than the timeout value, I'm not sure what the other parameters are. How would I link it with my socket and the recvfrom() call, and what does *restrict do?
What kind of behavior does setting the socket in non-blocking mode exhibit? Will the recvfrom automatically return once it gets a msg, and block while it doesn't, or does it wait for a certain amount of time before returning?
I've checked the man page for select() but I guess I'm not sure of the parameters.
int select(int nfds, fd_set *restrict readfds,
fd_set *restrict writefds, fd_set *restrict errorfds,
struct timeval *restrict timeout);
Other than the timeout value, I'm not sure what the other parameters are. How would I link it with my socket and the recvfrom() call, and what does *restrict do?
"restrict" is just an aliasing hint, it's part of the C99 language. Think of it in the same class as "volatile".
The other parameters are the fd_sets for the file descriptors you want to select on (typically, your socket or sockets you want to read from). Check the part of FD_SET, or just go to Beej's networking tutorial, and you'll learn all about it.
The behavior of a non-blocking socket is that it returns the data if it's already there (i e, in the kernel receive buffer), and it returns no data if there is no data; it never waits.
The other parameters are the fd_sets for the file descriptors you want to select on (typically, your socket or sockets you want to read from). Check the part of FD_SET, or just go to Beej's networking tutorial, and you'll learn all about it.
The behavior of a non-blocking socket is that it returns the data if it's already there (i e, in the kernel receive buffer), and it returns no data if there is no data; it never waits.
enum Bool { True, False, FileNotFound };
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement