TcpClient and detecting disconnections

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have an instance of a TcpClient using a NetworkStream and I want to be able to detect when the connection is lost (or cable unplugged). What is the best way of doing this?

I don't want to use getByte because it reads from the stream.

I tried inheriting TcpClient to access the protected function Client.Connected but this returns true even when the connection has been lost.

Any help is appreciated.
 
I tried inheriting TcpClient to access the protected function Client
Connected but this returns true even when the connection has been lost.

I found that myself. If you want to look at the stream without "reading" it
you could try a Peek as that will return data from the stream but not
advance the stream position so you can always re-read the data using a get.
 
Peek returns -1 (when no data in stream) regardless of whether a connection is currently there or not. It appears that until a read operation is attempted the stream thinks everything is ok. Once I do the read operation the following exception is thrown "Run-time exception thrown : System.IO.IOException - Unable to read data from the transport connection". This is okay, but I would love to know a method that doesn't consume data from the stream.

Do you know of any other way to acheive this?

----- Adrian Forbes [ASP MVP] wrote: -----
I tried inheriting TcpClient to access the protected function Client
Connected but this returns true even when the connection has been lost.

I found that myself. If you want to look at the stream without "reading" it
you could try a Peek as that will return data from the stream but not
advance the stream position so you can always re-read the data using a get.
 
Also, if the connection exists and there is no data in the stream, readByte seems to wait until it can read a byte. So this makes it difficult to use exception handling to determine if the connection exists or not.

----- Pi wrote: -----

Peek returns -1 (when no data in stream) regardless of whether a connection is currently there or not. It appears that until a read operation is attempted the stream thinks everything is ok. Once I do the read operation the following exception is thrown "Run-time exception thrown : System.IO.IOException - Unable to read data from the transport connection". This is okay, but I would love to know a method that doesn't consume data from the stream.

Do you know of any other way to acheive this?

----- Adrian Forbes [ASP MVP] wrote: -----
I tried inheriting TcpClient to access the protected function Client
Connected but this returns true even when the connection has been lost.

I found that myself. If you want to look at the stream without "reading" it
you could try a Peek as that will return data from the stream but not
advance the stream position so you can always re-read the data using a get.
 
Also, if the connection exists and there is no data in the stream, readByte seems to wait until it can read a byte. So this makes it difficult to use exception handling to determine if the connection exists or not

----- Pi wrote: ----

Peek returns -1 (when no data in stream) regardless of whether a connection is currently there or not. It appears that until a read operation is attempted the stream thinks everything is ok. Once I do the read operation the following exception is thrown "Run-time exception thrown : System.IO.IOException - Unable to read data from the transport connection". This is okay, but I would love to know a method that doesn't consume data from the stream

Do you know of any other way to acheive this

----- Adrian Forbes [ASP MVP] wrote: ----
I tried inheriting TcpClient to access the protected function Clien
Connected but this returns true even when the connection has been lost

I found that myself. If you want to look at the stream without "reading" i
you could try a Peek as that will return data from the stream but no
advance the stream position so you can always re-read the data using a get
 
Hi... I'm far from an expert in these matters (but) I believe you will want
to look into what is typically known as a "heartbeat" process. In the case
of a TCP/IP connection you would set up another port which is simply used
for signaling. It doesn't interrupt your dataflow but it does permit a
server to ask "are you there" and expect the answer "yes" to return.

Tom Leylan

Pi said:
I have an instance of a TcpClient using a NetworkStream and I want to be
able to detect when the connection is lost (or cable unplugged). What is
the best way of doing this?
I don't want to use getByte because it reads from the stream.

I tried inheriting TcpClient to access the protected function
Client.Connected but this returns true even when the connection has been
lost.
 
I don't ever use a Network stream when I use a TCPClient Object I just have
a byte array, that gets the data from TCPClient.Getstream.BeginRead.

In the asyncorinse sub that is called after beginread I do an EndRead if the
value it returns is less then 1 the connection has been terminated so I shut
down my class.

This doesn't directly answer you question but maybe it's a push in the right
direction.



Pi said:
I have an instance of a TcpClient using a NetworkStream and I want to be
able to detect when the connection is lost (or cable unplugged). What is
the best way of doing this?
I don't want to use getByte because it reads from the stream.

I tried inheriting TcpClient to access the protected function
Client.Connected but this returns true even when the connection has been
lost.
 
When it returns -1 the connection has gone. When the connection is open and
you Peek when nothing is there the call will block until something is there
(ie something is sent), or it will return -1 if the connection is closed.

Pi said:
Peek returns -1 (when no data in stream) regardless of whether a
connection is currently there or not. It appears that until a read
operation is attempted the stream thinks everything is ok. Once I do the
read operation the following exception is thrown "Run-time exception thrown
: System.IO.IOException - Unable to read data from the transport
connection". This is okay, but I would love to know a method that doesn't
consume data from the stream.
Do you know of any other way to acheive this?

----- Adrian Forbes [ASP MVP] wrote: -----
I tried inheriting TcpClient to access the protected function Client
Connected but this returns true even when the connection has been
lost.

I found that myself. If you want to look at the stream without "reading" it
you could try a Peek as that will return data from the stream but not
advance the stream position so you can always re-read the data using a get.
 
Back
Top