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 false, and no exceptions. Anyone know how I can detect when these sockets are remotely closed?? - TcpListener listener;
-
TcpClient client;
-
TcpClient server;
-
while (true)
-
{
-
Thread.Sleep(10);
-
IPAddress addy = IPAddress.Parse("127.0.0.1");
-
listener = new TcpListener(addy, 4001);
-
listener.Start();
-
client = listener.AcceptTcpClient();
-
Console.WriteLine("[[Client connected.]]");
-
server = new TcpClient();
-
server.Connect("imperian.com", 23);
-
Console.WriteLine("[[Server connected.]]");
-
NetworkStream cstm = client.GetStream();
-
NetworkStream sstm = server.GetStream();
-
bool quit = false;
-
while (!quit)
-
{
-
Thread.Sleep(10);
-
-
if (!client.Client.Connected)
-
{
-
Console.WriteLine("[[Client disconnected]]");
-
}
-
-
try
-
{
-
if (cstm.DataAvailable)
-
{
-
byte[] readBuffer = new byte[1024];
-
StringBuilder builder = new StringBuilder();
-
int numBytes = 0;
-
do
-
{
-
numBytes = cstm.Read(readBuffer, 0, readBuffer.Length);
-
builder.AppendFormat("{0}", Encoding.ASCII.GetString(readBuffer));
-
} while (cstm.DataAvailable);
-
-
byte[] writeBuffer = Encoding.ASCII.GetBytes(builder.ToString());
-
sstm.Write(writeBuffer, 0, writeBuffer.Length);
-
}
-
-
if (sstm.DataAvailable)
-
{
-
byte[] readBuffer = new byte[1024];
-
StringBuilder builder = new StringBuilder();
-
int numBytes = 0;
-
do
-
{
-
numBytes = sstm.Read(readBuffer, 0, readBuffer.Length);
-
builder.AppendFormat("{0}", Encoding.ASCII.GetString(readBuffer));
-
} while (sstm.DataAvailable);
-
-
byte[] writeBuffer = Encoding.ASCII.GetBytes(builder.ToString());
-
cstm.Write(writeBuffer, 0, writeBuffer.Length);
-
}
-
}
-
catch (Exception)
-
{
-
cstm.Close();
-
Console.WriteLine("[[Client disconnected.]]");
-
sstm.Close();
-
Console.WriteLine("[[Server disconnected.]]");
-
break;
-
quit = true;
-
}
-
}
-
}
I wrote this code and use it frequently: -
bool SocketConnected(Socket s)
-
{
-
bool part1 = s.Poll(1000, SelectMode.SelectRead);
-
bool part2 = (s.Available == 0);
-
if (part1 & part2)
-
{//connection is closed
-
return false;
-
}
-
return true;
-
}
-
5 41489
here is a link to a great response similar to your problem. http://bytes.com/forum/thread234523.html
at the bottom of the linked page, it says something about the .poll() function of the socket class, i think that will help
I wrote this code and use it frequently: -
bool SocketConnected(Socket s)
-
{
-
bool part1 = s.Poll(1000, SelectMode.SelectRead);
-
bool part2 = (s.Available == 0);
-
if (part1 & part2)
-
{//connection is closed
-
return false;
-
}
-
return true;
-
}
-
Thank you so much, that worked!
I used that code and it works wonderfully...thanks a bunch!!!
to: Plater Plater
You are very good. After searching on google in many hours, but I can not find any working solutions. Now, I used that code and it works wonderfully...thanks a bunch!!!
good job!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
by: Jonathan |
last post by:
Hi,
how to detect when an asynchronous socket is closed?
Thanks!
|
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
|
by: schwehr |
last post by:
Hi All,
I've tried to RTFM this and am having no luck. First off, I am using
Mac OSX 10.4.7 with python 2.4.2 from fink. I am trying to connect to
a server that should be rejecting...
|
by: Kurt |
last post by:
I have a client & server app which communicates using the socket class. If I
shutdown the server closing the socket the client still thinks the socket is
open. Even calling send does not throw an...
|
by: O.B. |
last post by:
In the following code snippet, the thread successfully makes it to the
line where it waits for data to be received. Then the client closes the
connection. The thread wakes up and returns from the...
|
by: Plater |
last post by:
I'm having a bit of trouble figuring out how to detect if a socket is closed or not. Outside of using read()/write(). I also don't want to set the socket to non-blocking if at all possible.
I wrote...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |