Home > Archive > Unix Programming > August 2006 > Problem with select() with multiple sockets
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 |
Problem with select() with multiple sockets
|
|
| devendra.chandola@gmail.com 2006-08-31, 8:00 am |
| >From one of my client application, I open non-blocking TCP sockets to
multiple servers and then immediately call select() on all these
sockets with time-out value of 30 seconds. The select() always returns
before timeout value and its value is always less than the available
servers.
For example I open 3 non blocking TCP connections for connecting to 3
up and running servers on same machine. Then I call select() on these 3
sockets. The return value of select() is 2 rather than 3. Even the logs
of 3 servers shows connection attempts.
Following is code snippets:
SOCKET fds[MAX_SOCKETS];
struct sockaddr_in ads [MAX_SOCKETS];
for (int nNumConnections = 0; nNumConnections < 3; nNumConnections++)
{
ads[nNumConnections].sin_addr.s_addr = inet_addr(lpszIpAddr);
ads[nNumConnections].sin_family = AF_INET;
ads[nNumConnections].sin_port = htons((unsigned short)
(nPort[nNumConnections]) );
}
fd_set writeFDS,exceptFDS;
FD_ZERO (&writeFDS);
FD_ZERO (&exceptFDS);
int nMaxFd = 0;
int nSocketCount = 0;
for (int i = 0; i < 3; i++)
{
fds = socket( AF_INET, SOCK_STREAM , IPPROTO_TCP );
// Set the socket in non-blocking mode
int flags = fcntl(fds, F_GETFL, 0);
if ( flags < 0 ) flags = 0;
flags |= O_NONBLOCK; // set non-blocking
if((fcntl(fds, F_SETFL, flags)) == -1) // returns -1 on error
{
return -1;
}
int nResult = connect(fds, (SOCKCONST sockaddr *) &ads, sizeof (ads));
if (nResult == 0)
{
if (fds >= nMaxFd) nMaxFd = fds + 1;
FD_SET (fds, &writeFDS);
FD_SET (fds, &exceptFDS);
nSocketCount ++
}
else if (nResult == SOCKET_ERROR)
{
if(errno == EINPROGRESS)
{
if (fds >= nMaxFd) nMaxFd = fds + 1;
FD_SET (fds, &writeFDS);
FD_SET (fds, &exceptFDS);
nSocketCount++;
}
}
}
cout << "Socket count in the FD_SET :" << nSocketCount;
if (nSocketCount > 0)
{
struct timeval tv;
tv.tv_sec = 60;
tv.tv_usec = 0;
nSelectStatus = select (nMaxFd, NULL, &writeFDS, &exceptFDS, &tv);
cout <<"Return value of select :" << nSelectStatus;
if (nSelectStatus <=0 || nSelectStatus == SOCKET_ERROR)
{
// timeout or other error
}
else
{
for (int i = 0; i < 3; i++)
{
if (FD_ISSET (fds[nIndex], &exceptFDS))
{
// exception associated with the socket fds[nIndex]
}
else if (FD_ISSET (fds[nIndex], &writeFDS))
{
// socket fds[nIndex] is ready to send the data
}
}
}
}//if (nMaxFd > 0)
return 0;
}
Output:
Socket count in the FD_SET :3
Return value of select :2 (sometime it comes 1 or 3)
I am not very clear the functionality of select() in case of multiple
sockets.
Does it wait for all sockets to be ready OR return as soon as get some
of the sockets running ?
-Devendra
| |
| wwwdev 2006-08-31, 5:11 pm |
| Hi devendra.chandola@gmail.com,
quote: I am not very clear the functionality of select() in case of multiple
sockets.
Basically so
socket(...);
connect(...);...;connect(...);
while(...){
FD_ZERO();
FD_SET();...;FD_SET();
rc = select();
if(FD_ISSET())
......
}
quote:
Does it wait for all sockets to be ready OR return as soon as get some
of the sockets running ?
-Devendra
--as soon as get some
of the sockets running--
But not more then 60 sec in your case.
There are several samples. See here http://nonblock.tripod.com or http://hpaftpd.sourceforge.net | |
| Rainer Temme 2006-08-31, 9:59 pm |
| devendra.chandola@gmail.com wrote:
> I am not very clear the functionality of select() in case of multiple
> sockets.
> Does it wait for all sockets to be ready OR return as soon as get some
> of the sockets running ?
Devendra,
as quite often, the man-pages would be of great help as well.
select() returns as soon as one of the conditions that you're
waiting for applies. So, in your case it is quite reasonable
that select returns with 1,2 or 3 instead of with 3 all the time.
This is simply due to the fact, that the establishment of the
three connections don't take exactly the same time.
Rainer
| |
| David Schwartz 2006-08-31, 9:59 pm |
|
devendra.chandola@gmail.com wrote:
> I am not very clear the functionality of select() in case of multiple
> sockets.
> Does it wait for all sockets to be ready OR return as soon as get some
> of the sockets running ?
It returns as soon as any condition you are waiting for is satisfied.
It's very hard to imagine how a function that only returned when all
conditions were satisfied would be useful.
DS
|
|
|
|
|