Is TcpClient a single connection use? How do I send a second message?

453 views Asked by At

I have a TcpClient object which can connect to a foreign program. I can send one message (xml serialized message). But as soon as I close my network stream my TcpClient closes and disposes. And the second time I try to send a message via my TcpClient I get an error "cannot access a disposed object, Net.Sockets.Socket".

I am new to using TcpClient but I was under the impression I would connect once with the other program then be able to pass messages back and forth for the lifetime of my program and simply disconnect the TcpClient before closing my program.

The only way I've been able to send multiple messages is if I new the TcpClient, and perform the connect again prior to sending my message. Is this the correct way of sending multiple messages and my manager and I are simply under the wrong impression of TcpClient?

Another impression I had regarding TcpClient was that once connected I'd be able to receive messages from the other program. So my program would be sitting there doing nothing then receive a TcpClient message (some defined xml message) stating inventory has been changed. Is this possible or do I need to set up a TcpListener for that type of functionality to work?



Please don't let this example confuse you or the issue I'm just trying to get my impression across in a different way. An example of what my impression was is a phone conversation. The TcpClient is the phone and I make a connection by calling another phone number. I would then be connected and be able to talk back and forth. There may be pauses of several minutes before one of us starts the conversation back up again. Then at the end of the work day would hang up the phone.

But after working with TcpClient for a while I'm getting the impression that it's a walkie-talkie conversation. I connect via TcpClient which is my pressing the walkie-talkie button. I talk, and the closing of the network stream is me releasing the button. I can hear back from the person on the other side of the conversation but as soon as I press the button again it changes frequency and it's an entirely new conversation requiring me to new the tcpClient.



EDIT

Everyone that said not to close the stream was right. The reason I kept closing the stream was because the receiver software would then process my message. But the receiving software was also looking for a message to be wrapped in the ASCII STX and ETX characters. If I wrapped my message, sent it along and lo and behold they started processing the message with my stream still open.

1

There are 1 answers

0
Peacock On BEST ANSWER

Everyone that said not to close the stream was right.

The reason I kept closing the stream was because the receiving software would then process my message. But the receiving software would also process my message if it was wrapped in the ASCII "STX" and "ETX" characters.

My ultimate problem was a little bit of ignorance on TcpClient and a little ignorance on the receiving software's protocols.