Click to See Complete Forum and Search --> : help! problem about the select function.
_J@e_
June 10th, 2004, 08:43 AM
hi, guys,
In my current Winsock-based TCP program, on the client side, after I send my request data packet to the Server side using the send socket function, I attempt to get the socket status to see if it has something to read using the select function, and the select function always return 0 meaning the time limit expired on some users' computers, but it's just fine on another users' computers.
Why? Does anybody can tell me why and how to fix it? Thanks!
j0nas
June 10th, 2004, 08:48 AM
Is it select() on the client side that doesn't work?
Have you issued a (non-blocking) recv() before calling select()? What timeout value do you have for the select call? Maybe you can show us some of the code that doesn't work.
_J@e_
June 10th, 2004, 08:55 AM
well, here's the code...
//...
SOCKET sckClient = socket(AF_INET, SOCK_STREAM, 0);
int nOne = 1;
setsockopt(sckClient, IPPROTO_TCP, TCP_NODELAY, (char *)&nOne, sizeof(int)); // just_say_no
struct sockaddr_in addr_to = { 0 };
//...
connect(sckClient, (struct sockaddr *)&addr_to, sizeof(addr_to));
char *buf;
//...
send(sckClient, (const char *)((char*)buf), nDataToSend, 0);
int nRetCode = ::select((int)sckClient + 1, &fdsRead, NULL, NULL, &tvTimeOut);
if (nRetCode==0 )
{ //ERROR: the time limit expired
//Here's the problem! some computers meet the error, but some don't! WHY???
}
recv(sckClient,(char *)buf,nDataToRead, 0);
//...
j0nas
June 10th, 2004, 11:00 AM
How did you initialize fdsRead and tvTimeOut?
I hope you did something like this:tvTimeOut.tv_sec = 30;
tvTimeOut.tv_usec = 0;
FD_ZERO(&fdsRead);
FD_SET(sckClient, &fdsRead);
Currently (at least in the code that you posted), you don't check for SOCKET_ERROR and if the read set is true. I would do something like this:if (nRetCode == 0)
; // got timeout
else if (nRetCode == SOCKET_ERROR)
; // some socket error, call WSAGetLastError() to get a specific error
else {
if (FD_ISSET(sckClient, &fdsRead)) {
; // data ready to be received
}
}
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.