473,495 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2677
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
18446
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
9062
by: Daniel | last post by:
TcpClient close() method socket leak when i use TcpClient to open a connection, send data and close the TcpClient with myTcpClientInstance.Close(); it takes 60 seconds for the actual socket on...
3
3017
by: מורדי | last post by:
Hi, I'm writing a client/server application in which the client send a series of screenshots to the server to be saved using the tcpclient. in most cases the first screenshot is transmitted ok...
3
28611
by: Ricardo Quintanilla | last post by:
i had a problem whom i do not know how to explain. i was using a TcpClient (System.Net.Sockets.TcpClient) object to send and receive data to an AS400 socket. Two months ago it started to work...
1
2437
by: Claire | last post by:
Hi Im writing an application using the above controls in blocking mode. Ive not used them before and I'm more used to asynchronous socket programming utilizing socket events. As there are no...
0
1940
by: Torsten Brasch | last post by:
Hi All and Happy New Year ;) I have a very strange problem with System.Net.Sockets.TcpClient(). For some reason, the number of bytes I can receive is limited to 5460 bytes. I made sure that the...
1
4506
by: hamil | last post by:
I am having trouble using the TcpListener and TcpClient classes. At the end of this post is server code that runs, and a class whose purpose is described below. I need to know when the client...
1
1514
by: Inspired | last post by:
hi.. im writing an application that uses tcpclient/tcplistener to send file via network, what im doing is dividing the file into small parts each part equals the buffer size (8192) and send the...
2
6715
by: Freddy Setiawan | last post by:
Anyone got solution for detecting if the TCPclient is disconnected? i have "googled" and almost found nothing, i'm using .NET Framework V1.1. they said to use Poll method, but there is no such...
0
6991
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
7160
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,...
0
7196
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6878
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7373
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4897
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4583
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
286
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.