468,491 Members | 1,992 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

TcpClient and detecting disconnections

Pi
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.
Nov 20 '05 #1
7 2496
> 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.
Nov 20 '05 #2
Pi
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.
Nov 20 '05 #3
Pi
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.
Nov 20 '05 #4
Pi
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
Nov 20 '05 #5
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" <an*******@discussions.microsoft.com> wrote...
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.

Nov 20 '05 #6
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" <an*******@discussions.microsoft.com> wrote in message
news:27**********************************@microsof t.com...
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.

Nov 20 '05 #7
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" <an*******@discussions.microsoft.com> wrote in message
news:31**********************************@microsof t.com...
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.

Nov 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Michael Kennedy [UB] | last post: by
3 posts views Thread by Daniel | last post: by
3 posts views Thread by מורדי | last post: by
3 posts views Thread by Ricardo Quintanilla | last post: by
reply views Thread by Torsten Brasch | last post: by
1 post views Thread by Inspired | last post: by
2 posts views Thread by Freddy Setiawan | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.