I got this code in my program. I expected the select to timeout but it never timeouts.
here it is. Can you please check if there is something wrong with the code
Code:
int
check_sockets(int sock1_fd, int sock2_fd)
{
struct timeval tv;
fd_set fds;
int retval;
int i;
int fd_max;
rv = 0;
if ( sock1_fd > sock2_fd)
fd_max = sock1_fd;
else
fd_max = sock2_fd;
while (running) {
tv.tv_sec = 2;
tv.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(sock1_fd, &fds);
FD_SET(sock2_fd, &fds);
i = select( fd_max + 1, &fds, NULL, NULL, &tv);
if (running == 0)
break;
if (i == 0)
{
/*handle timeout*/
continue;
}
else
{
/* data is ready on socket1 */
if (FD_ISSET(sock1_fd, &fds)) {
/*check socket 1 for data*/
}
/* data is ready on socket2 */
if
(FD_ISSET(sock2_fd, &fds)) {
/*check socket 2 for data*/
}
}
}
return retval;
}
socket1 is a normal udp socket. and socket 2 is a raw socket.
Are there any instances where I should set the socket as non-blocking before using select?