Detect client socket disconnect

  • Thread starter Thread starter Morten
  • Start date Start date
M

Morten

How do I detect if a client socket is no longer connected to the listen tcp
socket ?

I have tried with (just an example):

---------------------

Socket tcpSocket;

while(tcpSocket.Connected)
{

if(tcpSocket.Available)
{
// Receive incomming buffer
}

Thread.Sleep(1000;
}

// Client socket is disconnected


---------------------

The problem is that tcpSocket.Connected never gets false, when the client
first is connected. Even though the client do a client.Close(), it still
sees that its connected.

Best regards
-Morten
 
Morten,

This is just a guess, but have you read everything from the buffer? If
there is information in the buffer to be read, then it might not pick up the
disconnected state until the buffer is cleared.

Hope this helps.
 
Yes I'm quiet sure since I check for available data.

public void HandleRequest(Object stateInfo)
{
Socket tcpSocket;
tcpSocket = (Socket)stateInfo;
int Available = 0;

while(Form1.bListen)
{
Debug.WriteLine("##TRACE##: Looking for incomming data...");

if((Available = tcpSocket.Available) > 0)
{

Debug.WriteLine("##TRACE##: Data incomming...");

}
else if(!tcpSocket.Connected)
{
// Never gets to this point.
RaiseNotifyEvent(NotifyCommand.Disconnected, "null");
break;
}
Thread.Sleep(1000);
}
Regards,
Morten

Nicholas Paldino said:
Morten,

This is just a guess, but have you read everything from the buffer? If
there is information in the buffer to be read, then it might not pick up the
disconnected state until the buffer is cleared.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Morten said:
How do I detect if a client socket is no longer connected to the listen tcp
socket ?

I have tried with (just an example):

---------------------

Socket tcpSocket;

while(tcpSocket.Connected)
{

if(tcpSocket.Available)
{
// Receive incomming buffer
}

Thread.Sleep(1000;
}

// Client socket is disconnected


---------------------

The problem is that tcpSocket.Connected never gets false, when the client
first is connected. Even though the client do a client.Close(), it still
sees that its connected.

Best regards
-Morten
 
I would like to second this question.

The Connected property of the Socket class does not return false when the
Socket no longer is connected to the host connection. Is there another way
to verify that the Socket is disconnected?

Garrison
 
Morten said:
How do I detect if a client socket is no longer connected to the listen tcp
socket ?

I have tried with (just an example):
.
.
The problem is that tcpSocket.Connected never gets false, when the client
first is connected. Even though the client do a client.Close(), it still
sees that its connected.
Morten -

The Connected property shows the state of the socket at the time
of the last IO operation. Thus the socket could be closed by the
remote host with the Connected property value still being true. The
value won't change until after you try reading or writing to the
socket.

You should be able to use the Available property to determine if
the remote host has closed the connection. The Available property will
either return the number of bytes available to be read on the socket
(remember that its possible for a connected socket to still return
zero bytes of data), or will throw a SocketException if the remote
host shuts down or has closed the connection. All you should need to
do is catch the SocketException in your code. (note that this behavior
changed in .NET 1.1. In 1.0 the Available property just returned zero
if the remote host closed the connection).

Alternatively, you can also utilize the Poll() Socket method with
the SelectRead SelectMode parameter. This method will return a true
value if data is available or if the connection has been closed by the
remote host. You will then need to differentiate between which of
these situations has occurred (by reading the socket buffer, and
seeing if it returns a zero value).

Hope this helps shed some light on your problem.

Rich Blum - Author
"C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176
"Network Performance Open Source Toolkit" (Wiley)
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0471433012.html
 
Thank you Rich Blum, especially the point with using the Poll method worked
out my problem. Great! Thanx :)

-Morten
 
Back
Top