Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

select() ignoring timeval argument

I am new to this thread.

I am using select() in my code.

my code snippet:
---------------------
twait.tv_sec = 0;
twait.tv_usec = THIRTY_MILLISECONDS;

pollfds2_count = select (0, readfds, writefds,exceptfds, &twait);
From one of the support forums i came to know that select() will return immediately even timeval parameter set to "non zero" or non NULL values. Am i correct? (They mentioned a possible bug in windows).

For our application to work correctly select should wait for THIRTY_MILLISECONDS.

How can i ensure that?

How sleep will help?

Please help me.

Regards,
Naga

Comments

  • vicky_devvicky_dev Posts: 3Member
    You seem to have misunderstood the functionality of select. Select [b]does not[/b] wait for the time interval specified. It checks the status of the specified sockets, that is whether the socket has data ready to be read (sockets in readfds), or socket is ready to be written (sockets in writefds) or have errors (exceptfds).
    The timeout specifies the [b]maximum[/b] time for the status to change.

    A sample usage of select is to check if there is data to be read from socket to ensure that read does not block:
    [code]
    twait.tv_sec = 0;
    twait.tv_usec = THIRTY_MILLISECONDS

    count = select (0, readfds, NULL,NULL, &twait);
    if( count == 0 ) {
    // select failed after waiting for THIRTY_MILLLISECONDS
    } else {
    // We have data ready to be read
    }

    [/code]

    If you want to wait for THIRTY_MILLLISECONDS for whatever reason, use
    Sleep(THIRTY_MILLLISECONDS)

    Regards
  • vicky_devvicky_dev Posts: 3Member
    You seem to have misunderstood the functionality of select. Select [b]does not[/b] wait for the time interval specified. It checks the status of the specified sockets, that is whether the socket has data ready to be read (sockets in readfds), or socket is ready to be written (sockets in writefds) or have errors (exceptfds).
    The timeout specifies the [b]maximum[/b] time for the status to change.

    A sample usage of select is to check if there is data to be read from socket to ensure that read does not block:
    [code]
    twait.tv_sec = 0;
    twait.tv_usec = THIRTY_MILLISECONDS

    count = select (0, readfds, NULL,NULL, &twait);
    if( count == 0 ) {
    // select failed after waiting for THIRTY_MILLLISECONDS
    } else {
    // We have data ready to be read
    }

    [/code]

    If you want to wait for THIRTY_MILLLISECONDS for whatever reason, use
    Sleep(THIRTY_MILLLISECONDS)

    Regards
  • SephirothSephiroth Fayetteville, NC, USAPosts: 1,035Member
    The sleep() method will work, but it can cause the entire program to sleep, unless it is multithreaded and the network thread is all by itself. The easiest way to avoid blocking is to simply specify the non-blocking style for the socket. This way you can check it a thousand times a second and it won't block.
    [code]
    sockfd = socket(PF_INET, SOCK_STREAM, 0);
    fcntl(sockfd, F_SETFL, O_NONBLOCK);
    [/code]
    If you want a GREAT guide on programming sockets (TCP and UDP), check out the link below.

    http://beej.us/guide/bgnet/output/html/multipage/index.html

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
Sign In or Register to comment.