Re: select() always blocks non-blocking socket



Thank you, Ben. You are right, so I moved initialization of the fd into the loop, but still I always have to wait for the entire timeout period. By the way, actually I'd prefer a blocking socket, since this code runs in its own thread anyway. But then the thread gets stuck inside recv() forever as soon as the news server stops data transmission. Any idea?

Thank you, Olaf

for ( int cbBytes = 0, nRet = SOCKET_ERROR; ; )
{
fd_set fdRead;
FD_ZERO( &fdRead );
FD_SET( m_Socket, &fdRead );
nRet = select( 1, &fdRead, NULL, NULL, &tvTimeout );
// ...



"Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx> wrote in message news:OcxnzqurIHA.1952@xxxxxxxxxxxxxxxxxxxxxxx
Olaf Lahl wrote:
Hi,

I have simple app which sends NNTP commands like LIST or GROUP to a
news server and attempts to read the server's response. My problem
is, that select() will always wait for the entire timeout period
event though I created a non-blocking socket. If I specify a small
timeout value, select() will frequently return without any data, if I
supply a large value like 5 or 10 sec, it will always wait for
exactly that amount of time! Isn't the timeout parameter supposed to
be a maximum value? Here is the code that does it (error checking and
other code ommitted for brevity). Any idea what I'm doing wrong?

You're not filling in fdRead on each loop. The first three parameters to select are in/out and get overwritten. So the first time select times out, it clears fdRead and from then on it isn't even checking for activity.


Thanks in advance, Olaf

m_Socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
connect( m_Socket, ( sockaddr* )&saServer, sizeof( saServer ) );
u_long ulIoctlBlock = 1;
ioctlsocket( m_Socket, FIONBIO, &ulIoctlBlock ); // <-- Create
non-blocking socket
fd_set fdRead;
FD_ZERO( &fdRead );
FD_SET( m_Socket, &fdRead );
timeval tvTimeout;
tvTimeout.tv_sec = 5; // <-- Maximum (?) timeout value
tvTimeout.tv_usec = 0;
for ( int cbBytes = 0, nRet = SOCKET_ERROR; ; )
{
nRet = select( 1, &fdRead, NULL, NULL, &tvTimeout ); <-- Will
always wait for the entire timeout period!
if ( nRet == SOCKET_ERROR )
{
break;
}
if ( nRet == 0 )
{
continue;
}
if ( ( cbBytes = recv( m_Socket, chBuffer, ulBufferSize, 0 ) ) <=
0 ) // <-- May reach this line without having read any data if
timeout period is zero or too short
{
break;
}
}



.



Relevant Pages

  • Re: select() always blocks non-blocking socket
    ... server and attempts to read the server's response. ... selectwill always wait for the entire timeout period event though I ... created a non-blocking socket. ... FD_ZERO(&fdRead); ...
    (microsoft.public.win32.programmer.networks)
  • Re: Need Help in Loop Foreach
    ... I'd like to insert a loop, in such a way that it reads all the "System ID" ... # Query all the systems status by inputting the SystemIDs on Expect Script ... intial expect until a timeout occured. ... # may want to break after sending exit in above section if this does ...
    (comp.lang.tcl)
  • Re: creating a new thread to run while loop
    ... > connection suddenly dropped, so i have to go for an alternative which ... thread) is not able to timeout / return error / etc and is basically being ... The only options seems to be to terminate the ... The example that you supplied w/ the loop in the main thread that causes the ...
    (microsoft.public.windowsce.embedded.vc)
  • TimeOut Expired. Using Temporary tables
    ... I am iterating through these records in a WHILE Loop and performing certain business rules on these records. ... This entire processing is being done in a stored procedure which is being executed using ADO.NET's ExecuteNonQuery method. ... However after around 18 hrs of processing time i get a TimeOut error. ... Post Made from http://www.DotNetJunkies.com/newsgroups Our newsgroup engine supports Post Alerts, Ratings, and Searching. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Tuning a select() loop for os.popen3()
    ... > get incomplete stdout. ... and I made readList the condition for the while loop. ... a 0.1 second timeout is about the same as no ...
    (comp.lang.python)