473,725 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Av ailable)
{
// Receive incomming buffer
}

Thread.Sleep(10 00;
}

// Client socket is disconnected
---------------------

The problem is that tcpSocket.Conne cted 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 45039
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.co m

"Morten" <mo****@n.n> wrote in message
news:eO******** ******@TK2MSFTN GP10.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.Av ailable)
{
// Receive incomming buffer
}

Thread.Sleep(10 00;
}

// Client socket is disconnected
---------------------

The problem is that tcpSocket.Conne cted 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(O bject stateInfo)
{
Socket tcpSocket;
tcpSocket = (Socket)stateIn fo;
int Available = 0;

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

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

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

}
else if(!tcpSocket.C onnected)
{
// Never gets to this point.
RaiseNotifyEven t(NotifyCommand .Disconnected, "null");
break;
}
Thread.Sleep(10 00);
}
Regards,
Morten

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in message news:uJ******** ******@TK2MSFTN GP09.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.co m

"Morten" <mo****@n.n> wrote in message
news:eO******** ******@TK2MSFTN GP10.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.Av ailable)
{
// Receive incomming buffer
}

Thread.Sleep(10 00;
}

// Client socket is disconnected
---------------------

The problem is that tcpSocket.Conne cted 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******** ******@TK2MSFTN GP10.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.Av ailable)
{
// Receive incomming buffer
}

Thread.Sleep(10 00;
}

// Client socket is disconnected
---------------------

The problem is that tcpSocket.Conne cted 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******* *******@TK2MSFT NGP10.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.Conne cted 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.goo gle.com...
"Morten" <mo****@n.n> wrote in message

news:<eO******* *******@TK2MSFT NGP10.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.Conne cted 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
4488
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 computations, the only data transfered is user mouse/kbd input. It works synchronously, but somehow, when I play in client window, both client and server have 17 fps, while when playing in server window, server has 44 fps while client ...
4
3852
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). I had some troubles when I started 6 threads (each thread made a separate connection) and sent messages to same server simultaneously. Sometimes, not always, the socket looks like ok, but really it is dead. I don't why it happens.
5
16936
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 server and recives data from server. If server application shuts down client immedeatly recieves disconnection or exception, but im not able to detect if some body removed network cable of node , where server is running. Client continuesly waits for...
8
41421
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 into a CLOSE_WAIT state ad infinitum and never closes fully until my client and server applications shut down. Ive disabled lingeroptions. Is there anything else easy that i can do to force the loopback to close immediately?
3
4276
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 know how detect this situation the same situation is when I plug out patch-cord For waiting for a data I use this loop While Me.mSocket.Connected
5
8238
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 common process of connecting and of the server and client... here is the code i am trying to implement. this code snippet is for message transfer only for multiple clients. /////////////////////////////file receive method when sent from...
2
2254
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 won't close. Thank you using System; using System.Collections.Generic;
0
1745
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 createSocketServer($host='192.168.1.34',$port=2222) { $max_clients = 10;
1
4683
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 thread: http://bytes.com/topic/net/answers/828133-c-net-detecting-socket-closed I have managed to detect disconnect by the client when the client window form is closed, but if the client computer switches off or powers off, it doesn't detect a...
0
8888
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8097
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.