Re: select() always blocks non-blocking socket
- From: "Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx>
- Date: Mon, 5 May 2008 15:04:29 -0500
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;
}
}
.
- Follow-Ups:
- Re: select() always blocks non-blocking socket
- From: Olaf Lahl
- Re: select() always blocks non-blocking socket
- References:
- select() always blocks non-blocking socket
- From: Olaf Lahl
- select() always blocks non-blocking socket
- Prev by Date: select() always blocks non-blocking socket
- Next by Date: Re: non-blocking connect might block winsock? ?????????
- Previous by thread: select() always blocks non-blocking socket
- Next by thread: Re: select() always blocks non-blocking socket
- Index(es):
Relevant Pages
|
|