# Transports

While **DOTSNET** handles all the higher logic like spawning, unspawning, observers, etc., the Transport handles only the low level packet sending.

### Available Transports

DOTSNET can work with different low level transports, and comes with a few by default:

* [**kcp2k**](https://github.com/vis2k/kcp2k/): kcp translated to C#, line by line
  * kcp is just a reliability algorithm
  * Over a C# UDP socket
  * Supports reliable & unreliable channels
* [**Telepathy**](https://github.com/vis2k/Telepathy): battle tested C# TCP
* [**libuv2k**](https://github.com/libuv/libuv): DEPRECATED
* [**LiteNetLib**](https://github.com/RevenantX/LiteNetLib)**:** DEPRECATED
* **MemoryTransport**:&#x20;
  * uses memcpy to move messages between client & server
  * Can be used for benchmarking and for single player games if needed.
  * Does not work over the network.

**DOTSNET** uses `kcp` by default.

If you want to try a different transport, select the **NetworkClient/Server** **GameObject** in your scene, remove the **KcpTransportServer/ClientAuthoring** component and add the ones from the transport of choice, for example:

![](https://lh3.googleusercontent.com/mo94zP8rn17RBNRhcuLd73FzynAGZPtjogp2oyoSOSmgElNRxGIgncUBRbhXckf5cXBgMXZSPgWDIza9frj8db4d18jjlFLjoWu0wZFdpi-9RnFXiXbBCzQu_twXvE0HH-lttN26)![](https://lh3.googleusercontent.com/sDwO22NdsCWSA9ZNFNclHK_DQWihboYPFY2mIT_DgHzgGP2lFolfjevAujebkm8EoS6hIThCxLUav3wWHy45s0LgZqPhm7ogoQHOzpdNEZjq_CCn1lXQ4DzHancNlncbe50M9AD1)

{% hint style="info" %}
**Note**: not all Transports will use a Port, which is why Port is always a transport specific property.
{% endhint %}

Note: **NetworkClient/Server.Send()** has a Channel parameter that will be forwarded to the Transport. Some transports (like TCP) ignore channels.

### Implementing a Transport

Inherit from `TransportClientSystem` and `TransportServerSystem` to implement your own transport.

Note that `Send` and `OnData` always send and receive exactly one message. So if a client sends 3 and then 2 bytes, the server should receive exactly 3 and then 2 bytes instead of 5 bytes.

Note that all **TransportServer/ClientSystem** functions are not thread safe and should only be called and handled in the main thread. If you want more threads, you will have to add them yourself in the background.

## TransportClientSystem API

| Event:                       | Description:                                                                                                                                                                                                                                                                                                                                                                                            |
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `OnConnected`                | <p>Call this after successfully connecting to the server.</p><p>Only call this from the main thread!</p>                                                                                                                                                                                                                                                                                                |
| `OnData<ArraySegment<byte>>` | <p>Call this after receiving a message from the server.<br></p><p>Only call this with exactly the message bytes. Not more, not less.<br></p><p>Pass an ArraySegment for allocation free message processing.<br></p><p>The client will handle the message immediately, so the ArraySegment’s array can be modified again immediately after returning.<br></p><p>Only call this from the main thread!</p> |
| `OnDisconnected`             | <p>Call this after disconnecting from the server.</p><p>Only call this from the main thread!</p>                                                                                                                                                                                                                                                                                                        |

| Function:                  | Description:                                                                                                                                                                                                                            |
| -------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `IsConnected`              | Return true if the client is currently successfully connected to the server.                                                                                                                                                            |
| `Connect(address)`         | Start connecting the client to the server. Should not be blocking, and only initiate the connect process in the background.                                                                                                             |
| `Send(ArraySegment<byte>)` | <p>Send bytes to the server. Only called with exactly the message bytes. Not more, not less.</p><p>The ArraySegment’s internal array is only valid until returning, so either send it directly, or copy it into an internal buffer.</p> |
| `Disconnect`               | Disconnect from the server.                                                                                                                                                                                                             |

## TransportServerSystem API

| Event:                                     | Description:                                                                                                                                                                                                                                                                                                                                                                                                     |
| ------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `OnConnected<connectionId>`                | <p>Call this after a new client connected.</p><p>Only call this from the main thread!</p>                                                                                                                                                                                                                                                                                                                        |
| `OnData<connectionId, ArraySegment<byte>>` | <p>Call this after receiving a message from a client connection.<br></p><p>Only call this with exactly the message bytes. Not more, not less.<br></p><p>Pass an ArraySegment for allocation free message processing.<br></p><p>The server will handle the message immediately, so the ArraySegment’s array can be modified again immediately after returning.<br></p><p>Only call this from the main thread!</p> |
| `OnDisconnected<connectionId>`             | <p>Call this after a client disconnected from the server.</p><p>Only call this from the main thread!</p>                                                                                                                                                                                                                                                                                                         |

| Function:                                | Description:                                                                                                                                                                                                                            |
| ---------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `IsActive`                               | Return true if the server is still listening to incoming connections.                                                                                                                                                                   |
| `Start`                                  | Start listening to incoming connections.                                                                                                                                                                                                |
| `Send(connectionId, ArraySegment<byte>)` | <p>Send bytes to the client. Only called with exactly the message bytes. Not more, not less.</p><p>The ArraySegment’s internal array is only valid until returning, so either send it directly, or copy it into an internal buffer.</p> |
| `Disconnect(connectionId)`               | Disconnect the connection from the server.                                                                                                                                                                                              |
| `GetAddress`                             | Get a connection’s IP address. Can be useful for IP bans etc.                                                                                                                                                                           |
| `Stop`                                   | Stop listening to incoming connections.                                                                                                                                                                                                 |


---

# Agent Instructions: 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:

```
GET https://dotsnet.gitbook.io/docs/user-manual/transports.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
