Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello, guys.
I have created a server application with many client connecting to it. when a client hardware fails or power suddenly goes off, the socket at client side does't close socket, and the correspoiding socket at server side is blocked(recv called), and will not close, taking server resources. I detect these idle sockets by measuring time, but I don't know how to close them. I searched google, but I failed. any help, sample code or link appreciated.
mr.abzadeh
Posted
Comments
Vedat Ozan Oner 8-Feb-14 17:13pm    
have you seen here? http://msdn.microsoft.com/en-us/library/windows/desktop/ms740121%28v=vs.85%29.aspx

I am familiar with scenarios where the connection is interrupted without the socket being notified, leaving it in a state as you describe.
In your case, you should be able to change the socket into non-blocking mode by calling ioctlsocket() then close it upon exiting the recv() call.

C++
//-------------------------
// Set the socket I/O mode: In this case FIONBIO
// enables or disables the blocking mode for the 
// socket based on the numerical value of iMode.
// If iMode = 0, blocking is enabled; 
// If iMode != 0, non-blocking mode is enabled.
int iResult;
u_long iMode = 0;

iResult = ioctlsocket(m_socket, FIONBIO, &iMode);

Soren Madsen
 
Share this answer
 
v3
Comments
mr.abzadeh 9-Feb-14 1:31am    
I don't wish to close socket, The socket will close only if client closes it. From your comment, I noticed that this code is possible, but may not be efficient. Am I right?
thread A:
int iResult;
u_long iMode = 0;

iResult = ioctlsocket(m_socket, FIONBIO, &iMode);
while (1)
{
int iRet = recv(m_socket);
if (iRet == 0) donothing();
if (iRet == SOCKET_ERROR) break;
//process returned data here
Sleep(1);
}
Thread B:
closesocket(m_socket);
SoMad 9-Feb-14 2:00am    
No, that is not what I was saying. I was not suggesting that you change your code to use non-blocking sockets, but that you do the ioctlsocket() call from thread B when it has detected the idle situation and only to force the recv() to exit.

Soren Madsen
If you close the socket, the recv() function will return immediately with zero data bytes. This is a decent little trick when using a blocking winsock socket.
 
Share this answer
 
Comments
mr.abzadeh 9-Feb-14 1:22am    
Suppose thread A has called recv(s1), where s1 is a blocking socket. You mean I call closesocket(s1) from thread B, Then break loop on thread A when 0 bytes recieved? I have already done this, and it doesn't work.
a sample code is here:
Thread A:
while(1)
{
int iRet = recv(s1, ...);
if (iRet == 0 || iRet == SOCKET_ERROR) break;
//process returned data here
}
Thread B:
closesocket(s1);
Albert Holguin 9-Feb-14 11:40am    
I know this works, because I use it. What version of Winsock are u using?
mr.abzadeh 10-Feb-14 2:53am    
Thanks. It works,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900