473,756 Members | 7,560 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Detecting Disconnected Sockets

1 New Member
I have a created a multi-threaded TcpClient server that works for my needs. Each thread uses synchronous (blocked) communication to wait for a command from the client

Here's the problem...if a client does not gracefully disconnect (unplug the device from the network or a power failure), my server does not recognize the lost connection and just continues to wait to receive data.

Any help would be great, I have been searching online for a few days now and I know I'm doing everything correctly except this problem.

Thank you in advance.

Here is a new Thread of the server that to receives the data:

class ConnectionThrea d
{
public TcpListener threadListener;
private static int connections = 0;

public event textParserEvent Handler incomingText;

public void HandleConnectio n()
{
int recv;
byte[] data = new byte[1024];
string foundText = null;
string[] textList = null;
TcpClient client = threadListener. AcceptTcpClient ();
NetworkStream ns = client.GetStrea m();

textParserEvent Args e = new textParserEvent Args(ns);
while (true)
{
try
{
data.Initialize ();
//*** Waits on next line for data, returns 0 if graceful disconnect ***///
recv = ns.Read(data, 0, data.Length);
if (recv == 0)
break;
foundText = Encoding.ASCII. GetString(data, 0, recv);
textList = foundText.Split (' ');
e.foundText = textList;
incomingText(th is, e);
}
catch (System.IO.IOEx ception f)
{
Console.WriteLi ne("Data Read Error: " + f.Message);
}
}
ns.WriteTimeout = 1;
client.LingerSt ate.Enabled = false;
ns.Close();
client.Close();

connections--;
Console.WriteLi ne("Client disconnected: {0} active connections", connections);
Thread.CurrentT hread.Abort();
}
}
Dec 1 '06 #1
0 1041

Sign in to post your reply or Sign up for a free account.

Similar topics

11
20796
by: Adam Parkin | last post by:
Hello all, I need to be able to detect if there is an active available Internet connection in my VB6 program. In my program I'm using the Internet Transfer Control to transfer some files by HTTP, but as it stands now, there's no code to detect if there is in fact a network connection available before I try sending. Is there some easy way to do this? Thanks,
0
1869
by: Molly | last post by:
Peace, Pythonphiles! Python's std lib BaseHTTPServer: I'm stuck with a nasty spoiler -- I get erratic "Network error: disconnected by peer" for _POST_ requests only. =Background= I'm developing a Python CGI app. Was using Xitami, which is really light and nice, reasonable performance (circa 200-300 msec to respond,
1
7622
by: Tim Gosselin | last post by:
I am writing a tcp tunnel but cannot find a way of detecting when a socket shuts down its read end without writing to the socket. For testing the write end of the remote endpoint I just do a: if not sock.recv(buffsize) I cannot write to the socket and check if send returns 0 though, because that involves sending data out of the which may not have come in yet. When I try polling the socket with:
6
18505
by: Michael Kennedy [UB] | last post by:
Hi, I have a project using the TcpClient and its associated NetworkStream. Everything works well except for one condition which I haven't found any information about dealing with: How do I detected when the socket connection was closed on the server-side? That is, if my client connects to the server and everything is initialized
3
15140
by: James Yang | last post by:
Hi, I am using Sockets to connect to a remote computer and send data using Socket.Send() and receive using Socket.Receive() (block mode) . for somereason tho, when the client disconnects the Socket.Receive() just passes without any exception. Is there any way to detect disconnection using Sockets? I believe I saw a way of doing this using IAsyncResult, and eventhandling but..no article really explained how to use it properly.
3
10612
by: Adam Clauss | last post by:
There seems to be various methods to determine when the remote client disconnects, but all of them I have seen are Synchronous. AKA: Right before you try to send or receive data, check. Is there no way to do raise an event for this? That way you know as soon as the client disconnects? There are events for Accepting, Connecting, Receiving, Sending... why not one for disconnecting? -- Adam Clauss cabadam@tamu.edu
1
2018
by: PeterZ | last post by:
Hi, I have two tables and a dataRelation (win forms project). The dataRelation is bound to a dataGrid. I'm trying to detect when a user navigates to a new row in the datagrid so I can obtain a new record ID number from the db. Screenshot: http://www.insightgis.com.au/web/stuff/new_row_detect.gif
1
700
by: rs | last post by:
how I the client tell the server that the socket is closed? or this there an even that informs the server that the clients socket is close? Oh, I am using vb.net 2003 Thanks
5
42083
by: tichi | last post by:
I'm writing a program that in its most simplest form acts as a proxy between a local client and a remote server. But I cannot figure out how to detect when either the connection to the client is close or the connection to the server is closed. I've tried looking at socket.connected - it always returns true, even when the client program has disconnected. I've tried getting an exception from the networkstream.DataAvailable, it always just reads...
0
9287
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9886
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
9857
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
8723
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...
0
5155
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...
0
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3817
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
2
3369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2677
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.