Archived

This topic is now archived and is closed to further replies.

krad7

help reg sockets

Recommended Posts

krad7    122
i am implementing an application which transmits udp packets back and forth. now, i want to give a timeout on the receiver side when he is trying to receive upd packets. for example, the server goes down, the receiver has no way to know if the server has gone down or not, so it''ll be waiting forever on ''recvfrom'' command. how do i give a timeout to ''recvfrom''? i heard you should use something called ''select'' and ''poll''.. how do i use that? thanks in advance Slow and steady wins the race.

Share this post


Link to post
Share on other sites
eNGIMa    122
The select function allows you to check if there is any data waiting for a given socket.

int select(
int nfds,
fd_set FAR *readfds,
fd_set FAR *writefds,
fd_set FAR *exceptfds,
const struct timeval FAR *timeout
);

Im looking at the documentation for the windows version of sockets (which is mostly compatible with berkley sockets in *nix, which I geuss you're running) so I'm unsure of the first parameter of select. I you set it to NULL I think it should work

The next three parameters are lists of sockets. Declare your fd_set for each and use,
FD_SET(socket s, fd_set * set);
to add your sockets to the set.

struct timeval {
long tv_sec;
long tv_usec;
};

Use timeval for the timeout, or pass NULL for a blocking operation.

The call to select will return the number of sockets ready. And I *think* from there you can use,
FD_ISSET(socket s, fd_set * set);
to check if the socket is ready.

I hope this is helpful, if you need anything clarified then post again ...

[edited by - eNGIMa on May 1, 2002 8:21:04 PM]

Share this post


Link to post
Share on other sites