Click or drag to resize

TcpServerOpenAsync Method (Boolean, Int32, ServiceClientAsyncIByteStreamService)

Opens a TCP server. Creates a server and starts accepting connections, calling 'serviceConnection' with each connection established. This method returns after the server has been established, leaving the accepting and servicing of connections to a background thread.

Namespace:  Demo3D.Net.Protocols
Assembly:  Demo3D.IO (in Demo3D.IO.dll) Version: 15.0.2.11458
Syntax
C#
public static Task<ServerSocket> OpenAsync(
	bool sync,
	int port,
	ServiceClientAsync<IByteStreamService> serviceConnection
)

Parameters

sync
Type: SystemBoolean
If true, the Task returned is guaranteed to be complete.
port
Type: SystemInt32
The port to open the server on.
serviceConnection
Type: Demo3D.NetServiceClientAsyncIByteStreamService
Delegate for servicing a new connection.

Return Value

Type: TaskServerSocket
The TCP protocol server socket.
Examples
C#
// Start a new server, calling ServiceConnectionAsync on every connection established.
public async Task StartServerAsync(int port) {
    await TcpServer.OpenAsync(sync: false, port, ServiceConnectionAsync);
}

// Services a client connection.
async Task ServiceConnectionAsync(ServerClient<IByteStreamService> socket) {
    Logger.Log("Connection from " + socket.Address);

    var stream = socket.IO.Stream;

    // Simple server loops forever receiving messages and printing them to the log.
    for (;;) {
        var buffer   = new byte[4];
        var numBytes = await stream.ReadAsync(buffer, 0, buffer.Length);
        if (numBytes == 0) break;
        Logger.Log("Message received: " + numBytes);
    }
}
See Also