Cybernetix
20-03-2009, 12:16 PM
I'm currently writing a simple Windows Service in C# which connects to a telnet server and dumps everything on the pipe to a text file.
The process is:
+ Use TcpClient to establish a connection (creating the socket).
+ Get a NetworkStream from TcpClient.
+ Create a StreamWriter (which creates an output file).
+ Loop over the NetworkStream and write the value to the StreamWriter.
I'm doing this synchronously to ensure that the data is written in the order it was sent down the pipe. Nice and simple solution...
... but heres the issue. It turns out that TcpClient has a Connected property which I am using inside my read loop to determine if the connection is still alive. If I forcefully terminate my telnet server during the read loop, TcpClient.Connected still remains true. After reading MSDN and some other forum posts it states that the Connected property gets updated whenever a network operation is performed. I'm still reading the NetworkStream which returns 0 bytes read and TcpClient.Connection remains true.
I've tried to use Socket.Poll() to send a dummy packet to the telnet server with no success.
Does anyone have any possible solutions or ideas to catch connection loss using TcpClient?
Cheers in advance!
The process is:
+ Use TcpClient to establish a connection (creating the socket).
+ Get a NetworkStream from TcpClient.
+ Create a StreamWriter (which creates an output file).
+ Loop over the NetworkStream and write the value to the StreamWriter.
I'm doing this synchronously to ensure that the data is written in the order it was sent down the pipe. Nice and simple solution...
... but heres the issue. It turns out that TcpClient has a Connected property which I am using inside my read loop to determine if the connection is still alive. If I forcefully terminate my telnet server during the read loop, TcpClient.Connected still remains true. After reading MSDN and some other forum posts it states that the Connected property gets updated whenever a network operation is performed. I'm still reading the NetworkStream which returns 0 bytes read and TcpClient.Connection remains true.
I've tried to use Socket.Poll() to send a dummy packet to the telnet server with no success.
Does anyone have any possible solutions or ideas to catch connection loss using TcpClient?
Cheers in advance!