Click to See Complete Forum and Search --> : problem with select() in winsock 2.0


MrDoomMaster
December 10th, 2004, 04:47 PM
Hey guys, I have a small function in a class of mine which will return 1 if the socket is connected, and 0 if it is not connected. I attempt to do this using select() to detect the writability of the socket.

Here is the code:


int WinSock_Utils::B_ConnectionStatus(void)
{
if(OpMode != wsu::NONBLOCKING)
return 0;

FD_ZERO(&SyncState);

if(HostType == wsu::CLIENT)
FD_SET(Socket, &SyncState);
else
FD_SET(ConnectedSocket, &SyncState);

return select(-1, NULL, &SyncState, NULL, &SelectTimer);
}


This returns 1 every time, and never returns 0. The connection is successful to the client, because my client posts text when it is connected. Yet, when I close my client (which calls B_Disconnect member of my class), it properly disconnects using closesocket() and all of that stuff, but the function you see above (which is only used in my server) always returns 1!! Even after the client has been closed! It should return 0!

Any tips? What am I doing wrong?

Thanks! If you need more code, let me know!

MrDoomMaster
December 12th, 2004, 02:06 AM
Can no one help? :(

Naumaan
December 13th, 2004, 12:23 AM
The following code is working fine at my end


fd_set write; // fd_set variable to store socket and number of sockets
write.fd_array[0]=S;
write.fd_count=1;
timeval t; //timeval variable to store time to wait the select function
t.tv_sec=5;
t.tv_usec=100;
int flag=select(0,NULL,&write,NULL,&t);
if(flag==0 || flag==SOCKET_ERROR)
{
// if select function failes then we terminate the thread which is above created
TerminateThread(m_hThread,0);
m_uEventsTotal--; // decreasing EventTotal variable;
return FALSE; // returning false in case to failure.
}

MrDoomMaster
December 13th, 2004, 04:41 AM
Well, I tried applying your timeval settings, none of that helped. For servers, would you use the connected socket, or listening socket to check for writeability? If you want to see if a client is still connected, I imagine you would use the connected socket, and not the listening one.

Anyhow, this problem still lingers and I'm hoping someone can give me an answer soon! My project is on hault because of this...

Thanks for the reply, but this still doesn't work.

Naumaan
December 13th, 2004, 05:00 AM
I m using this code at client end and select function waits for 5 seconds and 100 u seconds.

check MSDN for select function
//
The parameter writefds identifies the sockets that are to be checked for writability. If a socket is processing a connect call (nonblocking), a socket is writable if the connection establishment successfully completes.
//

Are u using blocking or non blocking socket. check the documentation of select function for further assistance.

MrDoomMaster
December 13th, 2004, 03:10 PM
I create the socket using 'socket' then I create a name for the socket using SOCKETADDR_IN structure, then I call 'bind' on this socket, and then 'listen' and 'accept' to create a new socket which represents the connection between the client and server. After this, I use my function you see in the first post above to check to see if the connection has been closed or not, so that the server can perform appropriate cleanup.

I have reviewed MSDN a great deal before posting here, it doesn't cover such an issue as this.

Any other possibilities?

Thanks!