Main Site Documentation

Tcp server - client disconection


#1

Hello,
I´m using a Fez Domino withy a wiznet 5100 shield as a tcp server

I have a thread wich accepts tcp connections and another one that will process the requests.
In this 2nd thread I have a endless loop for processing all the incoming msgs and accordinh with the msgs the server will send back (using the same connection) the apropriate answers.
I have a problem; if the client hangs or desconnect I do not know about this situation and do not have the chance to make a ‘sockect.close’.
Restarting the client and if this happens more than twice I have no available sockets.
Anybody knows how can I be notified that a client no longer is connected to the server ?
Thanks in advance


#2

Detection of remote end disconnect of tcp sessions is not easy.

If the remote end terminated the session by closing its socket, then the server side will usually see a socket read completewith a zero length and/or an exception on the read. You will need to experiment on what is implemented.

But, when the remote session is terminated abnormally, or the communications path between the remote and server is no longer available, detection gets a bit more complicated.

TCP does a have a mechanism for detecting remote end abnormal disconnect, but the time it takes for this detection to occur can take from minutes for infinity. Generally, you can not depend upon the “feature”/

So… how is it done. You will have to add a “heartbeat” message between the client and the server. If the server does not hear from the client within a time period it can assume the client has gone away and you can clean up.


#3

Hello,

In your loop you can check bytesRead.
when 0 the connection is closed and you can break the loop

                // blocks until a client sends a message
                bytesRead = m_SocketClient.Receive(buffer, start, remaining,SocketFlags.None);

#4

with 'bytesRead = m_SocketClient.Receive(buffer, start, remaining,SocketFlags.None);
does no work because sometimes the client is not transmiting and at that moemnt bytesRead = 0

thanks


#5

Thanks Mike,

unfortunatly I can not send any ‘heratbeat’ because I do not know the complete protocoll from the other side.
I could try ‘clientSocket.Poll(60 * c_microsecondsPerSecond, SelectMode.SelectRead)’, for a minute silence, but unfornatly this finction returns false in 2 conditions: the connection is aborted, or no msgs were sent.
I´ll try to investigate and find a solution

thanks for he help