Winsock2 help please!
-
I wrote a winsock service that spawns a new thread on the "accept" function. Sometimes it seems that when I "recv" the client doesn't seem to have closed the connection gracefully? The service continues to peg the CPU at 100% (even though I have checked for SOCKET_ERROR and byte size of 0 returned from "recv"). My question is.....(code is "pseudo-pseudocode") Can I rely on the following to work properly considering the problems that I have found with closing the socket on the client end? int nTimeout = 1000; nError = setsockopt(client, SOL_SOCKET, SO_RCVTIMEO, (char*)&nTimeout, sizeof(nTimeout)); Or, is something like this going to be more reliable (albeit pretty ugly...), where the "accept" calls a timer thread which in turn calls the client thread until it times out(as follows)? UINT ClientTimerThread(LPVOID pParam) { CWinThread* thread; LPDWORD exitvalue; if (WaitForSingleObjectEx(thread = AfxBeginThread(ClientThread,pParam), 10000, false) == WAIT_TIMEOUT) { socket s = (SOCKET)pParam; closesocket(s); GetExitCodeThread(thread, exitvalue); TerminateThread(thread, (DWORD) exitvalue); } return 0; } Any advice would be greatly appreciated... ~LizardWiz()
-
I wrote a winsock service that spawns a new thread on the "accept" function. Sometimes it seems that when I "recv" the client doesn't seem to have closed the connection gracefully? The service continues to peg the CPU at 100% (even though I have checked for SOCKET_ERROR and byte size of 0 returned from "recv"). My question is.....(code is "pseudo-pseudocode") Can I rely on the following to work properly considering the problems that I have found with closing the socket on the client end? int nTimeout = 1000; nError = setsockopt(client, SOL_SOCKET, SO_RCVTIMEO, (char*)&nTimeout, sizeof(nTimeout)); Or, is something like this going to be more reliable (albeit pretty ugly...), where the "accept" calls a timer thread which in turn calls the client thread until it times out(as follows)? UINT ClientTimerThread(LPVOID pParam) { CWinThread* thread; LPDWORD exitvalue; if (WaitForSingleObjectEx(thread = AfxBeginThread(ClientThread,pParam), 10000, false) == WAIT_TIMEOUT) { socket s = (SOCKET)pParam; closesocket(s); GetExitCodeThread(thread, exitvalue); TerminateThread(thread, (DWORD) exitvalue); } return 0; } Any advice would be greatly appreciated... ~LizardWiz()
Please do not even consider applying your second solution:
TerminateThread
is not to be used unless under catastrophic conditions. In Winsock2SO_RCVTIMEO
works fine (you sure is Winsock2 and not 1.1 what you're using?) Nevertheless, even if it didn't work that wouldn't justify the CPU peak. Can you post more of the reading code? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo