473,394 Members | 1,794 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,394 software developers and data experts.

Detecting Disconnected Sockets

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 ConnectionThread
{
public TcpListener threadListener;
private static int connections = 0;

public event textParserEventHandler incomingText;

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

textParserEventArgs e = new textParserEventArgs(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(this, e);
}
catch (System.IO.IOException f)
{
Console.WriteLine("Data Read Error: " + f.Message);
}
}
ns.WriteTimeout = 1;
client.LingerState.Enabled = false;
ns.Close();
client.Close();

connections--;
Console.WriteLine("Client disconnected: {0} active connections", connections);
Thread.CurrentThread.Abort();
}
}
Dec 1 '06 #1
0 1025

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

Similar topics

11
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,...
0
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...
1
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...
6
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...
3
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...
3
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...
1
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...
1
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
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...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.