|
Home > Archive > Unix Programming > June 2005 > select() question.
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
select() question.
|
|
| Groleo 2005-06-16, 7:52 am |
| Hi. From what I read in select() man, I understood that if an event
occurs,
in the given timeout, the select will return.
Still, from the code below, I can understand that select will wait
"timeout"
and report what file handles have changed.
Can some one explain what exactly is the behaviour of select()?
And if the filehandle/socket have to be set non-blocking.
Socket::recv( string& response)
{
char recvBuffer_cp[ 1024+1];
timeval sleepTime;
fd_set readable, er;
for(;;)
{
cout << "Loop:1"<<endl; // debug info
bzero( recvBuffer_cp, 1024+1);
FD_ZERO( &readable);
FD_ZERO (&er);
FD_SET( socket_i, &readable);
sleepTime.tv_sec = 1; // n number of seconds to sleep.
sleepTime.tv_usec = 0; // n number of micro seconds to
sleep.
int retVal =select( socket_i +1, &readable, NULL, &er,
&sleepTime);
cout << "Loop:2"<<endl;// debug info
if( retVal ==0)
{
// throw strerror( errno);
return;
}
retVal =read( socket_i, recvBuffer_cp, 1024);
cout << recvBuffer_cp;
response +=recvBuffer_cp;
}
| |
| David Schwartz 2005-06-16, 5:52 pm |
|
"Groleo" <groleo@gmail.com> wrote in message
news:1118927345.359246.177570@f14g2000cwb.googlegroups.com...
> Can some one explain what exactly is the behaviour of select()?
Exactly as you described. It waits until the timeout or one of the file
descriptors are ready.
> And if the filehandle/socket have to be set non-blocking.
No, they don't. The 'select' will work exactly the same regardless of
whether the sockets are set blocking or non-blocking. However, it almost
always an error to use 'select' with blocking sockets. If you are in the
..001% case where this makes sense, you wouldn't be asking this question. ;)
DS
| |
| jimjim 2005-06-17, 5:53 pm |
| Hi Groleo,
** Can't really aprehend the deeper reasons for some to give cryptic answers
! **
You can use select( ) with both blocking and non-blocking sockets. However,
the aim of select( ) is to enable an application to multiplex I/O (read( )
ing, write( ) ing, etc, from/to a socket, pipe, etc) usually in a way that
will prevent a process from blocking during I/O. ** You should bear in mind
that there is always the case after the select( ) has returned due to
readiness for reading/writing from/to a socket, that the socket is not ready
anymore when the read( )/write( ) is performed **. This will result your
application to block during the read/write. It is therefore a common
practice to combine non-blocking sockets and select( )
l8rs
|
|
|
|
|