Windows programming

Moderators: None (Apply to moderate this forum)
Number of threads: 3711
Number of posts: 9173

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
select() ignoring timeval argument Posted by 1inmillion on 11 Feb 2009 at 11:40 PM
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

Report
Re: select() ignoring timeval argument Posted by vicky_dev on 5 Mar 2009 at 10:36 PM
You seem to have misunderstood the functionality of select. Select does not 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 maximum 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:
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
}



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

Regards
Report
Re: select() ignoring timeval argument Posted by Sephiroth on 6 Mar 2009 at 9:24 AM
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.
sockfd = socket(PF_INET, SOCK_STREAM, 0);
fcntl(sockfd, F_SETFL, O_NONBLOCK);

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

-Sephiroth
Report
Re: select() ignoring timeval argument Posted by vicky_dev on 5 Mar 2009 at 10:37 PM
You seem to have misunderstood the functionality of select. Select does not 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 maximum 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:
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
}



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

Regards



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.