TCPClient - Detectiing loss of remote host connection

G

Guest

Before I go and make massive changes to a TCP/IP library I made, I was
wondering if the detection of connection loss is possible through the
TCPClient class.

The problem I have is that I make a connection to another application using
a TCPClient in one app and a Listener with Client in the other app. I close
the app with the listener. I then try to send data to the listener app.
TCPClient.Connected returns true after the remote connection is forcefully
closed. I understand that the value of TCPClient.Connect is based on the
success of the previous send/recieve action. When I perform a send operation,
there is no exception thrown. Trying to perform a send operation a second
time throws a socket exception stating the connection was closed at the
remote end.

I was hoping that the exception would be thrown on the first send operation
after the connection was closed. Any way around this or will I have to send
test bytes at the start of each send operation in order to test the integrity
of the TCP connection? Any help would be great thanks :blush:)
 
M

Mubashir Khan

i remember for Socket class .... Receive and Callback for asynchronous
receive returns with 0 byte to read ...... i have never used TcpClient
though .......
 
W

William Stacey [C# MVP]

This is one of those issues I would love to be able remember perfectly, but
never seem to be able to. But basically, if you think about the stream
nature of tcp (has nothing to do with TcpClient) having it work the way you
want would be the worst kind of blocking operation - blocking on a high
latency network call. You would need to wait for each send to reach host
*and get a ack. You would not want that. IIRC, you could actually manage to
send multiple sends before every getting the error because of buffering and
Nagel. A common way to handle this is in your protocol. A typical
send/request would do in many cases. You do you send and sync or async wait
for a reply. You can error on the reply if you don't get in x seconds for
example.

--
William Stacey [C# MVP]
PCR concurrency library: www.codeplex.com/pcr
PSH Scripts Project www.codeplex.com/psobject


| Before I go and make massive changes to a TCP/IP library I made, I was
| wondering if the detection of connection loss is possible through the
| TCPClient class.
|
| The problem I have is that I make a connection to another application
using
| a TCPClient in one app and a Listener with Client in the other app. I
close
| the app with the listener. I then try to send data to the listener app.
| TCPClient.Connected returns true after the remote connection is forcefully
| closed. I understand that the value of TCPClient.Connect is based on the
| success of the previous send/recieve action. When I perform a send
operation,
| there is no exception thrown. Trying to perform a send operation a second
| time throws a socket exception stating the connection was closed at the
| remote end.
|
| I was hoping that the exception would be thrown on the first send
operation
| after the connection was closed. Any way around this or will I have to
send
| test bytes at the start of each send operation in order to test the
integrity
| of the TCP connection? Any help would be great thanks :blush:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top