By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,666 Members | 1,815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,666 IT Pros & Developers. It's quick & easy.

Detect client socket disconnect

P: n/a
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


Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Morten" <mo****@n.n> wrote in message
news:eO**************@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):

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

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


Nov 15 '05 #2

P: n/a
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]" <mv*@spam.guard.caspershouse.com> wrote in message news:uJ**************@TK2MSFTNGP09.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Morten" <mo****@n.n> wrote in message
news:eO**************@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):

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

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



Nov 15 '05 #3

P: n/a
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" <mo****@n.n> wrote in message
news:eO**************@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):

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

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


Nov 15 '05 #4

P: n/a
"Morten" <mo****@n.n> wrote in message news:<eO**************@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/WileyT...471433012.html
Nov 15 '05 #5

P: n/a
Thank you Rich Blum, especially the point with using the Poll method worked
out my problem. Great! Thanx :)

-Morten

"Rich Blum" <ri*******@juno.com> wrote in message
news:cc**************************@posting.google.c om...
"Morten" <mo****@n.n> wrote in message

news:<eO**************@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/WileyT...471433012.html

Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.