> For the complete documentation index, see [llms.txt](https://tariksavas.gitbook.io/tarnet/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tariksavas.gitbook.io/tarnet/docs/how-to-use/socketbuilder/buildclient.md).

# BuildClient

```csharp
ISocketClient BuildClient()
```

* **Description:**\
  This method builds and returns a socket client instance based on the configurations provided. The client connects to the server and exchanges messages according to the defined message settings.
* **Returns:**\
  `ISocketClient`: Returns the created socket client instance.
  * If there is a missing or invalid configuration (e.g., `SocketConfig`, `MessageConfig`, or `ClientValidationConfig`), this method returns `null`.

***

The `BuildClient` method is responsible for creating and configuring a socket client instance based on the provided configurations. It starts by checking if the necessary configurations (`SocketConfig`, `MessageConfig`, and `ClientValidationConfig`) are provided and valid.

Once the configurations are validated, the method proceeds to create a socket client based on the selected socket protocol (TCP or UDP). The appropriate message service is created according to the `MessageConfig` settings, which could involve compression or default message processing.

* **TCP Client:** If the protocol is TCP, a client is created to establish a connection with the server and initiate message exchanges.
* **UDP Client:** If the protocol is UDP, a client is created to send and receive messages without a direct connection.

Once the client is successfully created, it can be used to connect to the server and exchange messages as per the configured message settings.

***

#### Important Notes:

* **Client Validation:** For TCP clients, client validation is performed using the provided `ClientValidationConfig`, which can include IP whitelists and blacklists, as well as authentication keys. However, for UDP clients, client validation is not required, so the `ClientValidationConfig` is ignored.
* **Error Handling:** If any configuration is invalid, an error message will be logged, and `null` will be returned.
* **Message Processing:** The message service created here depends on the `MessageConfig` settings, allowing for either compressed or standard message handling.

***

**Usages:**

{% tabs %}
{% tab title="Tcp" %}

```csharp
SocketBuilder socketBuilder = new SocketBuilder(SocketProtocolType.Tcp);

socketBuilder.SetSocketConfig(socketConfig)
                .SetMessageConfig(messageConfig)
                    .SetClientValidationConfig(clientValidationConfig);

ISocketClient socketClient = socketBuilder.BuildClient();
```

{% endtab %}

{% tab title="Udp" %}

```csharp
SocketBuilder socketBuilder = new SocketBuilder(SocketProtocolType.Udp);

socketBuilder.SetSocketConfig(socketConfig)
                .SetMessageConfig(messageConfig);

ISocketClient socketClient = socketBuilder.BuildClient();
```

{% endtab %}
{% endtabs %}
