473,385 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 44913
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.