Connect

void Connect()
  • Description: The Connect() method is used to initiate the connection of the socket client to the specified server. This method attempts to connect to the server using the configured IP address and port. Once the connection is successful, the client can start sending and receiving messages. This method runs asynchronously, so it does not block the program while the connection is being established.

  • Parameters: This method does not take any parameters.

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


Functionality:

When the Connect() method is called, the client attempts to establish a connection to the server. The OnConnectionChanged event is triggered only if the connection is successful.

  • For TCP clients: The Connect() method tries to establish a reliable, connection-oriented communication channel. Once the connection is established, the client can start exchanging messages.

  • For UDP clients: UDP is a connectionless protocol, but the Connect() method still prepares the client to send and receive messages. There is no formal session establishment like in TCP, and messages can be sent without waiting for a connection.


Important Notes:

  • If the connection fails (for example, if the server is unreachable), the IsConnected property will return false.

  • Ensure the client is properly configured (such as server IP and port) before calling the Connect() method.


Usages:

socketClient.Connect();

The Connect() method starts the process of connecting the client to the server. Once the connection is successful, the client can begin message exchange.

Last updated