473,218 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 software developers and data experts.

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 2664
> 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
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
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
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
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
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
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
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
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
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.