Click or drag to resize

TcpServer Class

A TCP server.
Inheritance Hierarchy
SystemObject
  Demo3D.Net.ProtocolsTcpServer

Namespace: Demo3D.Net.Protocols
Assembly: Demo3D.IO (in Demo3D.IO.dll) Version: 19.00.00
Syntax
C#
public static class TcpServer

The TcpServer type exposes the following members.

Methods
 NameDescription
Public methodStatic memberCode exampleOpen(Int32, NotifyDataChangedEventHandler) Opens a TCP server. Creates a server and starts accepting connections, calling 'dataChangedHandler' with data from each connection as it arrives. This method returns after the server has been established, leaving the accepting and servicing of connections to a background thread.
Public methodStatic memberCode exampleOpen(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.
Public methodStatic memberCode exampleOpen(Int32, ServiceClientAsyncIPacketIOService) 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.
Public methodStatic memberCode exampleOpenAsync(Int32, NotifyDataChangedEventHandler) Opens a TCP server. Creates a server and starts accepting connections, calling 'dataChangedHandler' with data from each connection as it arrives. This method returns after the server has been established, leaving the accepting and servicing of connections to a background thread.
Public methodStatic memberCode exampleOpenAsync(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.
Public methodStatic memberCode exampleOpenAsync(Int32, ServiceClientAsyncIPacketIOService) 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.
Public methodStatic memberCode exampleOpenAsync(Boolean, Int32, NotifyDataChangedEventHandler) Opens a TCP server. Creates a server and starts accepting connections, calling 'dataChangedHandler' with data from each connection as it arrives. This method returns after the server has been established, leaving the accepting and servicing of connections to a background thread.
Public methodStatic memberCode exampleOpenAsync(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.
Public methodStatic memberCode exampleOpenAsync(Boolean, Int32, ServiceClientAsyncIPacketIOService) 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.
Top
Remarks
This class is a wrapper around ProtocolServer.Open{IPacketIOService} and ProtocolAddressBuilder. See ProtocolServer and ProtocolAddressBuilder for more advanced usage.
Example

This example shows a simple TCP server that accepts connections, calling a function for each connection accepted. The function reads data directly from the underlying Stream.

See TcpClient for the implementation of the client that works with this server.

C#
using System;
using System.Threading.Tasks;
using Demo3D.Native;
using Demo3D.Net;
using Demo3D.Net.Protocols;
using Demo3D.Visuals;

namespace Examples.Net.TcpStreamServerExample {
    [Auto] public class Server {
        [Auto] IBuilder                 app;
        [Auto] Document                 doc;
        [Auto] PrintDelegate            print;
        [Auto] SimplePropertyValue<int> tcpPort;     // The port to run the server on.

        [Auto] void OnInitialize(Visual sender) {
            // Open a server on the configured tcpPort.
            // This variant of TcpServer.Open will open the server, and call ServiceConnectionAsync
            // for each connection accepted.
            TcpServer.Open(tcpPort, ServiceConnectionAsync);
        }

        async Task ServiceConnectionAsync(ServerClient<IByteStreamService> socket) {
            Console.WriteLine("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 numRead = await stream.ReadAsync(buffer.AsMemory(0, 4));
                Console.WriteLine("Message received: " + numRead);
                if (numRead == 0) break;
            }
        }
    }
}
See Also