Sign in to follow this  
SelethD

How do I close a socket but not the tcpClient object

Recommended Posts

SelethD    456
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.

[size="2"]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[/size][size="2"]([/size][size="2"][color="#a31515"][size="2"][color="#a31515"]"127.0.0.1"[/color][/size][/color][/size][size="2"], 6313);
[/size][size="2"]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


[/size]

Share this post


Link to post
Share on other sites
hplus0603    11347
[quote name='SelethD' timestamp='1310772889' post='4835836']
I need to know how to close the connection, and allow a new connection without creating the tcpclient object all over again.
[/quote]

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.

Share this post


Link to post
Share on other sites
SelethD    456
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.

Share this post


Link to post
Share on other sites
hplus0603    11347
[quote name='SelethD' timestamp='1310781760' post='4835867']
however, each tutorial I see seems to do things totally different from any other tutorial. Its very confusing.
[/quote]

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this