How do I close a socket but not the tcpClient object

Started by
3 comments, last by SelethD 12 years, 9 months ago
I am making a client in c#

I am using the tcpclient object.

I need to know how to close the connection, and allow a new connection without creating the tcpclient object all over again.

In the 'init' phase of my client, I create the object...
clientSocket = new TcpClient();

Then when a player selects a server to connect to, I connect with this...
clientSocket.Connect([color="#a31515"][color="#a31515"]"127.0.0.1", 6313);
serverStream = clientSocket.GetStream();

So if the player wants to move to a different server, what do I do to close only the socke... not the entire TcpClient object?


I have googled all day, and the only results show me how to close the entire object....
clientSocket.Close();

thanks for any help


Advertisement

I need to know how to close the connection, and allow a new connection without creating the tcpclient object all over again.


WHat happens if you call Connect() again on the object that you called Close() on?

Also, it's not clear that the TcpClient is actually possible to use in the way you want. Why can't you create a new instance? If you're worried about re-binding events, then perhaps the right thing to do is to have your owning object expose the public events, and bind to the currently active TcpClient as a private implementation detail.
enum Bool { True, False, FileNotFound };
If I try to Connect again after a Close, I get an exception, where it tells me the object has been discarded (I assume that means its ready for deletion)

I could re-create the object, I just feel odd doing that, because I feel like maybe there are 'things' left in memory (im not entirely comfortable with managed code)

The main reason I suppose I do not like to recreate the client object because I have been comparing my code to other clients for online games, (yes... im one of those 'making my own morpg' ) and it appears to me that everyone has a client object created directly after the .exe is launced, even before you select a server. So how are they doing a disconnect when you need to select a new sever without having to recreate their client object?

I suppose if someone could show me a simple link or an example of how a person woudl ....
open a connection to a server
read and write data over that connection async
and properly close that connection
all in c#

I would be fine.

however, each tutorial I see seems to do things totally different from any other tutorial. Its very confusing.

however, each tutorial I see seems to do things totally different from any other tutorial. Its very confusing.


There is no "right" or "wrong" way; only ways that "meet requirements" or "don't work."

The TcpClient is similar to a "socket" -- you create and close them for each session in most code. The "Client" object is generally something you yourself write, and that just uses the TcpClient internally.

Btw: if you think "things will be around in memory," then you need to do a bit of deep-diving into the .NET framework. It uses garbage collection. You Close() and/or Dispose() objects if you want to get rid of managed resources before collection time (i e, deterministically), but that's about it.
enum Bool { True, False, FileNotFound };
Thanks for the info. I will just create and dispose of the tcpclient object when needed.

This topic is closed to new replies.

Advertisement