> For the complete documentation index, see [llms.txt](https://help.diarkis.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.diarkis.io/en/diarkis-server/tcp.md).

# TCP Server

## Overview

The TCP server is one of the three real-time communication servers of Diarkis and can implement custom commands for an application.

Commands are formatted packets sent from clients and processed by the server. This is how the Diarkis server cluster interacts with clients.

## Setting Up the TCP Server

To expose built-in commands to the client, you can set it up using the diarkisexec package.

* You can expose built-in commands by specifying the module with `diarkisexec.SetupDiarkis()`. By specifying `ConfigPath` for each module, you can customize the settings.
* You can expose custom commands using `diarkisexec.SetServerCommandHandler()`.
* You can set up a UDP server with `diarkisexec.SetupDiarkisUDPServer()`. You can customize the server settings with the JSON file passed as arguments.

> :warning: The above functions must be executed before calling `diarkisexec.StartDiarkis()`.

For more details, refer to the [diarkisexec API Reference](https://docs.diarkis.io/docs/server/current/diarkis/diarkisexec/index.html).

```go
package main

import (
	"github.com/Diarkis/diarkis/diarkisexec"
	"github.com/Diarkis/diarkis/server"
	"github.com/Diarkis/diarkis/user"
)

var ver uint8 = 10
var cmd uint16 = 100

func main() {
	logConfigPath := "/configs/shared/log.json"
	meshConfigPath := "/configs/shared/mesh.json"

	diarkisexec.SetupDiarkis(logConfigPath, meshConfigPath, &diarkisexec.Modules{
		Room:       &diarkisexec.Options{ExposeCommands: true},
		Group:      &diarkisexec.Options{ConfigPath: "/configs/shared/group.json", ExposeCommands: true},
		Dive:       &diarkisexec.Options{ConfigPath: "/configs/shared/dive.json", ExposeCommands: true},
		Field:      &diarkisexec.Options{ConfigPath: "/configs/shared/field.json", ExposeCommands: true},
		DM:         &diarkisexec.Options{ConfigPath: "/configs/shared/dm.json", ExposeCommands: true},
		MatchMaker: &diarkisexec.Options{ConfigPath: "/configs/shared/matching.json", ExposeCommands: true},
		Session:    &diarkisexec.Options{ConfigPath: "/configs/shared/session.json", ExposeCommands: true},
	})

	diarkisexec.SetupDiarkisTCPServer("/configs/tcp/main.json")

	diarkisexec.SetServerCommandHandler(ver, cmd, helloWorld)

	diarkisexec.StartDiarkis()
}

func helloWorld(ver uint8, cmd uint16, payload []byte, userData *user.User, next func(error)) {
	userData.ServerRespond([]byte("Hello World"), ver, cmd, server.Ok, true)
	next(nil)
}
```

## Mesh Configuration

Specify the path to the JSON file in meshConfigPath. For more details, refer to [#mesh-she-ding](#mesh-she-ding "mention").

## TCP Server Configuration

The configuration is written in JSON.

```json
{
  "connectionTTL": 10,
  "address": "127.0.0.1",
  "nic": "eth0",
  "port": "7200",
  "maxRcvSize": 8000,
  "noDelay": false,
  "enableEncryption": true
}
```

<table><thead><tr><th width="230">Key</th><th width="119">Default</th><th>Description</th></tr></thead><tbody><tr><td>connectionTTL</td><td>10</td><td>The TTL for the connection. If this time is exceeded, the client is disconnected from the server.</td></tr><tr><td>address</td><td>"127.0.0.1"</td><td>The address to bind the UDP server to.</td></tr><tr><td>nic</td><td>"eth0"</td><td>The interface name to obtain the address from. Used when the address is not specified.</td></tr><tr><td>port</td><td>"7100"</td><td>The port for the UDP server to bind to. The UDP server will automatically find an available port starting from the specified port.</td></tr><tr><td>maxRcvSize</td><td>8000</td><td>The maximum TCP packet size (bytes) for each request packet.</td></tr><tr><td>noDelay</td><td>false</td><td>Set to true to disable Nagle’s algorithm (no buffering before writing).</td></tr><tr><td>enableEncryption</td><td>true</td><td>Set to false to disable encryption and decryption of packets. The HTTP server needs the same configuration.</td></tr></tbody></table>

> :warning: In a cloud environment, set the address to the private IP address and use the `DIARKIS_CLOUD_ENV` environment variable.

For more details, refer to the [server API Reference](https://docs.diarkis.io/docs/server/current/diarkis/server/index.html).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://help.diarkis.io/en/diarkis-server/tcp.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
