Sign in to follow this  

VB.NET: Properly terminating socket connection help

This topic is 3491 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have client and server applications which are connected using System.Net.Sockets and the connection is properly established, but I have no code set up to terminate and reuse sockets once a client disconnects. Would I need to send the server a 'Disconnect' message from the client once it starts closing to tell it to close the connection on the server side, or can I detect a disconnecting client from the server and just do it entirely from there? Also, I would need to properly cleanup the socket on the client side too right? So that the client doesn't have active network connections after the application exits? Any help with this would be great... it seems like either everything is documented except what I want, or I am a terrible googler :P

Share this post


Link to post
Share on other sites
Anyone? Anyone at all?

I see there are things like Socket.Close and Socket.Client.Close and all .Dispose and .Disconnect but I can't seem to figure out how to put them all together without getting an exception.

SOMEONE must know a proper way to do this? Has nobody ever used a socket communication program before? You'd think you'd need to do this for every program...

Share this post


Link to post
Share on other sites
Whether I close the client and reopen it or I try to disconnect using clientsocket.close, when I try to reconnet I get the error "Cannot access disposed object. Object name: System.Net.Sockets.TcpClient"

Perhaps this will help me get a response?

Share this post


Link to post
Share on other sites
Quote:

Whether I close the client and reopen it or I try to disconnect using clientsocket.close, when I try to reconnet I get the error "Cannot access disposed object. Object name: System.Net.Sockets.TcpClient"


When you call the Close() method you are calling the Dispose() method too (Close usually calls dispose - see the dispose pattern for more information on this). So you are getting this exception because you have created an object, disposed of it and then tried to use it again after its been disposed / probably garbage collected. However, I would say you are correct in calling the Socket.Close() method in your Disconnect method - your problem lies within your Open or Connect method code.

In the Open or Connect method of your application make sure you create a new Socket and then open it. Then in the close method, dispose of the socket like you have been doing. Your code may look something like this (NOTE: this is in C# but should be pretty similar):


private TcpClient clientSocket;

public Connect()
{
clientSocket = new TcpClient();
clientSocket.Open();
}

public Disconnect()
{
clientSocket.Close();
clientSocket = null;
}




Hope that helps,

James

EDIT:

I've just read through your first post properly and in answer to it, the above code is probably the best practice for opening / closing a socket connection with a server. However, if there is any method such as Disconnect() on the clientSocket then that should be called before the Close() method.

Share this post


Link to post
Share on other sites

This topic is 3491 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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