Handling disconnects

TheThing edited this page Mar 9, 2011 · 1 revision

After the recent release of Network Library, a better support was added to facilitate and manage disconnects, both for the client and for the host. Previous versions threw an exceptions once a client or host got disconnected but this is no longer like so. Here I will show you how to manage and what to do with the information that is given.

Managing Disconnections

Network Library connection has an event that is fired every time a client get's disconnected if the connection is host or when the current connection get's disconnected. To simply manage disconnections, all you need to do is add an event to the OnDisconnected event inside the connection object.

theConnection.OnDisconnected += new delegateDisconnected(Network_OnDisconnected);


void Network_OnDisconnected(object source, object reason)

There are 2 parameters that is passed on in the event. The previous one is the source connection of the client that got disconnected if the connection is host. Meaning if our current program is hosting, the source parameter contains the client that got disconnected. The reason is an instance of Exception that contains details about the reason for the disconnection.

Manually Disconnecting

With the new version of Network Library it is possible to manually disconnecting a client. If you want to disconnect a client, the easiest way is to run the Disconnect method that can be found in the INetwork object. If a client wants to disconnect from the host, all you have to do is following:


This will take care of closing the connection and disposing any necessary resources related to the connection. If on the other hand you want the host to kick a client, you can run the Disconnect that can be found inside the ConnectionHost, like so:

(theConnection as ConnectionHost).Disconnect(client);

Disconnect in the ConnectionHost takes a single parameter that is the source connection of the client you want to disconnect.

Well that's it for now, you should now be able to create a simple client/host application. If you are however interested on how to fully utilize the power the Network Library gives you, head over to the Advanced Section.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.