472,952 Members | 2,231 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,952 software developers and data experts.

Detect client socket disconnect

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
5 44815
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Michael Rybak | last post by:
hi, everyone. I'm writing a 2-players game that should support network mode. I'm now testing it on 1 PC since I don't have 2. I directly use sockets, and both client and server do...
4
by: Frank Meng | last post by:
Hi. I am trying a csharp sample from http://www.codeproject.com/csharp/socketsincs.asp . (Sorry I didn't post all the source codes here, please get the codes from above link if you want to try)....
5
by: Vijay | last post by:
Hi all, i have TCP client server application written in C# using async socket methods, eg BeginReceive(), BeginEnd() etc server is continuesly running in the background, client connect to...
8
by: Claire | last post by:
I'm trying to debug my network application ie I want to check my error handling when the connection is broken. Im using 127.0.0.1 as the connection address. Unfortunately, the client socket goes...
3
by: Szafranr | last post by:
Hi I have application where I used tcpListener to connect with another system. Every thing is ok that it's time to error handling and I have problem when the TCP client is disconect I don't...
5
by: raghubr | last post by:
Hi all, Can any one pls guide me through..I need to transfer the file from server to client and client to server using sockets in an Asynchronous mode so this file transfer doesn't hinder the...
2
by: Sparky74 | last post by:
Hi. Can somebody please help me - I cannot get my TCP client application to close its socket one I attempt a send. It will close the socket fine if I don't call Send or BeginSend. Soon as I do, it...
0
by: khu84 | last post by:
Here is client server very simple code, seems to work with telnet but with with web client code gives blank output. Following is the server code:- <?php function...
1
by: s3raph | last post by:
Hi, I'm having trouble detecting the client machine connection lost when the client physically pushes the power or restart button on their computer. Using the code from Plater in the following...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.