> 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/buildserver.md).

# BuildServer

```csharp
ISocketServer BuildServer()
```

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

***

The `BuildServer` method is responsible for creating and configuring a socket server instance. It first checks if the necessary configurations (`SocketConfig`, `MessageConfig`, and `ClientValidationConfig`) have been correctly provided.&#x20;

Once the configurations are verified, the method constructs a server based on the selected socket protocol (either TCP or UDP). It then creates an appropriate message service, which could be either a compressed or default message processor based on the settings in `MessageConfig`. The server will be able to process and manage incoming connections and data transmission, depending on the chosen protocol and message settings.

* **TCP Server:** If the protocol is TCP, a server that can handle multiple client connections is created, and the message service is initialized for TCP.
* **UDP Server:** If the protocol is UDP, the method builds a server capable of managing datagram-based communication, without the need for the connection management found in TCP.

If the server is successfully created, an instance of `ISocketServer` is returned, which can be used to accept client connections and handle data transmission.

***

#### Important Notes:

* **Client Validation:** For TCP servers, client validation is performed using the provided `ClientValidationConfig`, which can include IP whitelists and blacklists, as well as authentication keys. However, for UDP servers, 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);

ISocketServer socketServer = socketBuilder.BuildServer();
```

{% endtab %}

{% tab title="Udp" %}

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

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

ISocketServer socketServer = socketBuilder.BuildServer();
```

{% endtab %}
{% endtabs %}
