TcpServer Class |
Namespace: Demo3D.Net.Protocols
public static class TcpServer
| Name | Description | |
|---|---|---|
| Open(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.
| |
| Open(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.
| |
| OpenAsync(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.
| |
| OpenAsync(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.
|
The following example shows a simple TCP server that accepts connections, waits for data to be received and then calls a method to handle the messages read from the each of the clients.
See TcpClient for the implementation of the client that works with this server.
using System; using Demo3D.Native; using Demo3D.Net; using Demo3D.Net.Protocols; using Demo3D.Visuals; namespace Examples.Net.TcpServerExample { [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, accept incoming connections and // wait for data to arrive on each of the connections. It will then call ReceiveMessage // when data is received on any of the clients. TcpServer.Open(tcpPort, ReceiveMessage); } void ReceiveMessage(ProtocolSocket socket, object service, NotifyDataChangedEventArgs e) { try { var args = (PacketChangedEventArgs)e; // A TCP server uses PacketChangedEventArgs. int message; // Read from the packet. using (var packet = args.GetPacket()) { // Read the message from the client - in this case a single integer. message = packet.ReadInt32(); } // Handle the message. HandleMessage(message); } catch (Exception x) { socket.Close(x); } } void HandleMessage(int message) { print("Message received " + message); } } }