Disconnect

void Disconnect()
  • Description: The Disconnect() method is used to terminate the connection of the socket client. When this method is called, the client disconnects from the server and stops communication. After the connection is terminated, the client can no longer send or receive messages. This method runs asynchronously and completes when the disconnection process is finished.

  • Parameters: This method does not take any parameters.

  • Returns: This method does not return any value. It simply terminates the connection.


Functionality:

When the Disconnect() method is called, the client attempts to close the connection to the server. After the connection is terminated, the OnConnectionChanged event is triggered, and the connection status is updated. This event is used to indicate whether the disconnection was successful or not.

  • For TCP Clients: When the connection is successfully closed, the client can no longer send or receive messages.

  • For UDP Clients: While UDP is a connectionless protocol, the Disconnect() method forces the client to stop participating in message exchange with the server.


Important Notes:

  • After disconnection, the client cannot send or receive any data.

  • The IsConnected property will return false after the connection is terminated.

  • To reconnect, the Connect() method must be called again.


Usages:

socketClient.OnConnectionChanged += (isConnected) =>
{
    Console.WriteLine(isConnected ? "Connected." : "Disconnected.");
};

socketClient.Disconnect();

The Disconnect() method terminates the connection between the client and the server, stopping all communication. After disconnection, the client cannot perform message operations, and the OnConnectionChanged event is triggered. To reconnect, the Connect() method must be called again.

Last updated