LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-16-2011, 10:53 AM   #1
PinoyAko
LQ Newbie
 
Registered: May 2011
Posts: 28

Rep: Reputation: 0
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?
 
Old 05-16-2011, 07:41 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by PinoyAko View Post
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 View Post
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)??
 
Old 05-17-2011, 12:34 AM   #3
PinoyAko
LQ Newbie
 
Registered: May 2011
Posts: 28

Original Poster
Rep: Reputation: 0
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
IRQ timeouts keysorsoze Linux - Hardware 0 03-09-2008 10:25 AM
watchdog timeouts geletine *BSD 1 05-07-2006 11:06 AM
Connection Timeouts lobo78 Linux - Networking 1 11-01-2005 06:40 PM
sendmail timeouts lehmm Linux - Networking 0 09-27-2005 01:54 AM
crond timeouts ? startail Linux - Software 0 01-11-2004 04:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration