Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Detect client socket disconnect

9 views
Skip to first unread message

Morten

unread,
Dec 22, 2003, 9:50:54 AM12/22/03
to
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


Nicholas Paldino [.NET/C# MVP]

unread,
Dec 22, 2003, 9:57:18 AM12/22/03
to
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]
- m...@spam.guard.caspershouse.com

"Morten" <morten@n.n> wrote in message
news:eOA6AsJy...@TK2MSFTNGP10.phx.gbl...

Morten

unread,
Dec 22, 2003, 10:19:56 AM12/22/03
to
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 [.NET/C# MVP]" <m...@spam.guard.caspershouse.com> wrote in message news:uJcikwJy...@TK2MSFTNGP09.phx.gbl...

Garrison

unread,
Dec 22, 2003, 8:00:43 PM12/22/03
to
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" <morten@n.n> wrote in message
news:eOA6AsJy...@TK2MSFTNGP10.phx.gbl...

Rich Blum

unread,
Dec 23, 2003, 7:38:33 AM12/23/03
to
"Morten" <morten@n.n> wrote in message news:<eOA6AsJy...@TK2MSFTNGP10.phx.gbl>...
> 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

Morten

unread,
Dec 23, 2003, 10:27:20 AM12/23/03
to
Thank you Rich Blum, especially the point with using the Poll method worked
out my problem. Great! Thanx :)

-Morten

"Rich Blum" <rich...@juno.com> wrote in message
news:ccdac0da.03122...@posting.google.com...

0 new messages