How do you keep a TCPClient alive.

  • Thread starter Darrell Sparti, MCSD
  • Start date
D

Darrell Sparti, MCSD

I'm writing a remote client in C# that connects to a remote connection
manager via a TcpClient. The connection and handshake work perfectly.
I must then remain connected and respond to the connection manager's
request. I've tried calling read in a loop right after the handshake
but the first time the socket's read method is called, the socket
reports it's disconnected. I've created a TcpClientEx class inheriting
from the TcpClient and set the KeepAlive socket option but that didn't
change anything. I know the connection manager isn't closing the
connection. How can I keep the connection alive and waiting for
commands? Thanks in advance for your help!
 
D

Darrell Sparti, MCSD

The connection manager sends them to me and I must respond. That's why
I have to keep reading from the socket basically listening for the
server's are you there messages and responding to them. There are
other commands the server sends as well that I need to respond to.
Thanks!!!
 
D

Darrell Sparti, MCSD

Thank you Sahil, I found the problem. Once the connection is
established, I capture the network stream and maintain it instead of
the socket itself. The network stream manages the socket for me. I
can loop checking for CanRead and DataAvailable. When I close the
socket, the network stream is closed with it so all works fine now.
Thanks again for your help!!!!
 
D

Darrell Sparti, MCSD

Thanks Sahil, I figured it out. I maintain the network stream instead
of the socket. Then I just check the CanRead and DataAvailable
properties in my loop. When I finally disconnect the socket, it
automatically closes the stream for me. Thanks again for your help!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top