LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Select() never timeouts (https://www.linuxquestions.org/questions/programming-9/select-never-timeouts-881024/)

PinoyAko 05-16-2011 10:53 AM

Select() never timeouts
 
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?

dwhitney67 05-16-2011 07:41 PM

Quote:

Originally Posted by PinoyAko (Post 4357967)
I got this code in my program. I expected the select to timeout but it never timeouts.

Based on the code you submitted, I find that hard to believe. Here... try a simpler program:
Code:

#include <sys/select.h>
#include <stdio.h>
#include <assert.h>

void timer(int secs, int usecs)
{
  struct timeval tv;

  tv.tv_sec  = secs;
  tv.tv_usec = usecs;

  int i = select(0, NULL, NULL, NULL, &tv);

  assert(i == 0);
  printf("timeout.\n");
}

int main()
{
  timer(2, 0);
  return 0;
}

Quote:

Originally Posted by PinoyAko (Post 4357967)
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?

Setting a socket to non-blocking will not have any bearing on select(). The select() function merely tells you when there is activity on a particular descriptor.

Why are you using a raw socket (and what flavor, tcp or udp)??

PinoyAko 05-17-2011 12:34 AM

Oooppss my bad. After checking all the variables included, I found out that the value of timeout was actually 2000 (I actually modified posted code the number 2 was actually a defined value which was not updated on the server where I am compiling the change was only done on my local computer :) I guess I should diff every files before compiling to check for inconsistencies.

As for the raw socket it is actually an ICMP socket. I will be sending some icmp requests.

Thank you


All times are GMT -5. The time now is 08:18 AM.