TITLEDarkNet Logo
DarkNet
Home
Purchase
Contact
In Action
 
General
Performance
Ease of use
Asynchronous
Error System
Documentation
Demo Code
 
Protocol
Instances
Packets
Encryption
Compatibility
Disconnecting
Demo - Cube World
 
NAT Traversal
 
Input
Output
 
Firewall

 

 

Disconnecting

Two types of disconnect methods are available using DarkNet: hard and graceful.

Hard

A hard disconnect is where a client disconnects immediately without waiting for data transfer to complete and without sending any final data. This can be achieved using mnDisconnectClient when graceful disconnect is disabled (it is by default).

Graceful

A graceful disconnect occurs when a client begins disconnecting but both the server and client are able to receive all data that has been sent before this point. Below is a visual explanation of how graceful disconnection is achieved.


mnEnableGracefulDisconnect is used to enabled graceful disconnect on both the client and server. The client and server can both send and receive data from each other.


mnClientConnected is used to determine what stage of the disconnect process we are in:
- C_NOT_CONNECTED (0) to indicate that the client is not connected at all.

- C_CONNECTED (1) to indicate that the client is fully connected.

- C_NO_SEND (2) to indicate that you have used mnShutdownClient and so all further attempts to send will fail, but new data can still be received.

- C_NO_RECV (3) to indicate that you have not used mnShutdownClient and can still send, but mnShutdownClient has been used on the other end so no more new data can be received. Data that is currently in the TCP packet queue can still be received via mnRecvTCP.

- C_NO_SEND_RECV (4) to indicate that you have used mnShutdownClient and so all further attempts to send will fail and mnShutdownClient has been used on the other end and so no more new data can be received. However, there is still data in the TCP packet queue that can still be dealt with. When the TCP packet queue is empty mnClientConnected will return 0 and the client ID will be freed by mnClientJoined.

On the server and client side mnClientConnected returns 1.
Diagram showing server and client fully connected to each other.

 

The server begins to gracefully disconnect the client by using mnShutdownClient. The client will receive all TCP data sent by the server before this point. The server can no longer send data to the client.

On the server side mnClientConnected returns 2 and on the client side mnClientConnected returns 3.
Diagram showing server/client connection where server has begun graceful disconnect using mnShutdownClient.


The client can still send data to the server, the blue box below is a packet containing a farewell message.
Diagram showing server/client midway through graceful disconnect where client can still send data to the server.


After sending the farewell message the client uses mnShutdownClient. The server will receive all data sent by the client before this point. The client can no longer send data to the server.

On the server side mnClientConnected will return 4 until all data has been received. When all data has received mnClientConnected will return 0 and the client will be disconnected when mnClientJoined is next used.

On the client side mnClientConnected will return 0.Diagram showing a graceful disconnect that has finished.

Demo

The below demo demonstrates the above disconnect process, where the server begins the process.

Screenshot

Screenshot of DarkNet client and server having completed a graceful disconnect.

Code

C++ - Client
C++ - Server

C# - Client
C# - Server

VB.NET - Client
VB.NET - Server

DarkBASIC Pro - Client
DarkBASIC Pro - Server


 

© Copyright Michael Pryor