Class TCPServer
A TCP server listens on a configurable port and dispatches length-framed netstring messages to registered callbacks, enabling the remote-control operator tool to communicate with the simulation at runtime.
public class TCPServer : MonoBehaviour
Inheritance
Remarks
Configuration is available in Project Settings -> RemoteControlConnectionSetting?text=Remote+Control.
Communication between the client and server uses the netstring format.
All outgoing messages are encoded in netstring format before being sent to clients.
Incoming messages that are not valid netstrings will be ignored and discarded.
The server also exposes the following callbacks for event-driven handling:
- OnTCPMessageReceived � Invoked when a valid netstring-formatted message is received from a connected client. The message is decoded and passed as a plain string.
- OnTCPConnectionEstablished � Invoked when a new client successfully connects to the server.
Provides a
TcpClientreference representing the connection. - OnTCPConnectionDisconnected � Invoked when an existing TCP client disconnects or the connection is closed. This is useful for cleanup and status tracking.
Fields
OnTCPConnectionDisconnected
Event invoked when a TCP client disconnects.
public Action<TcpClient> OnTCPConnectionDisconnected
Field Value
Action<TcpClient>
Remarks
This event is triggered when an active TCP client disconnects from the server or the connection is closed.
Example (Subscribe / Unsubscribe):
void OnEnable()
{
server.OnTCPConnectionDisconnected += OnClientDisconnected;
}
void OnDisable()
{
server.OnTCPConnectionDisconnected -= OnClientDisconnected;
}
void OnClientDisconnected()
{
Debug.Log("Client disconnected");
}
OnTCPConnectionEstablished
Event invoked when a TCP connection to a client is successfully established.
public Action<TcpClient> OnTCPConnectionEstablished
Field Value
Action<TcpClient>
Remarks
This event is triggered when a new client successfully connects to the TCP server.
The connected TcpClient instance is provided to the callback.
Example (Subscribe / Unsubscribe):
void OnEnable()
{
server.OnTCPConnectionEstablished += OnClientConnected;
}
void OnDisable()
{
server.OnTCPConnectionEstablished -= OnClientConnected;
}
void OnClientConnected(TcpClient client)
{
Debug.Log("Client connected: " + client.Client.RemoteEndPoint);
}
OnTCPMessageReceived
Event invoked when a TCP message is received from a client.
public Action<string> OnTCPMessageReceived
Field Value
Action<string>
Remarks
This event is triggered whenever a valid message, formatted using the netstring protocol, is received from a connected TCP client. The message string passed to the callback is already decoded and ready for processing.
If the incoming message is not in a valid netstring format, it will be ignored, and this event will not be invoked.
Example (Subscribe / Unsubscribe):
void OnEnable()
{
server.OnTCPMessageReceived += HandleMessage;
}
void OnDisable()
{
server.OnTCPMessageReceived -= HandleMessage;
}
void HandleMessage(string message)
{
Debug.Log("Received: " + message);
}
Properties
ConnectedTcpClients
A list of currently connected TCP clients.
public List<TcpClient> ConnectedTcpClients { get; }
Property Value
List<TcpClient>
Methods
SendTCPMessage(TcpClient, string)
Encodes message as a netstring and sends it to the connected TCP client.
public void SendTCPMessage(TcpClient client, string message)
Parameters
| Name | Type | Description |
|---|---|---|
client | TcpClient | The target TCP client. |
message | string | The message to send. |
Remarks
ASCII is used deliberately. Operator commands are simple
ASCII control strings (scenario identifiers, start/stop tokens), and the
netstring framing relies on a byte-accurate length prefix. Using UTF-8 would be
safe for pure-ASCII content, but if a non-ASCII character were accidentally
included, the multi-byte UTF-8 encoding would silently make the byte count in
the length prefix incorrect, corrupting the netstring frame and causing the
client to discard or misparse the message. ASCII encoding fails loudly on any
non-ASCII input, making the error detectable during development rather than
in the field.