Click or drag to resize

TcpServer Class

Represents a TCP-based communication server.
Inheritance Hierarchy
SystemObject
  SystemMarshalByRefObject
    System.ComponentModelComponent
      GSF.CommunicationServerBase
        GSF.CommunicationTcpServer

Namespace: GSF.Communication
Assembly: GSF.Communication (in GSF.Communication.dll) Version: 2.4.181-beta
Syntax
public class TcpServer : ServerBase
View Source

The TcpServer type exposes the following members.

Constructors
 NameDescription
Public methodTcpServer Initializes a new instance of the TcpServer class.
Public methodTcpServer(IContainer) Initializes a new instance of the TcpServer class.
Public methodTcpServer(String) Initializes a new instance of the TcpServer class.
Top
Properties
 NameDescription
Public propertyAllowDualStackSocket Gets or sets a boolean value that determines if dual-mode socket is allowed when endpoint address is IPv6.
Protected propertyCanRaiseEventsGets a value indicating whether the component can raise an event.
(Inherited from Component)
Public propertyClientIDs Gets the IDs of clients connected to the server.
(Inherited from ServerBase)
Public propertyConfigurationString Gets or sets the data required by the server to initialize.
(Inherited from ServerBase)
Public propertyContainerGets the IContainer that contains the Component.
(Inherited from Component)
Public propertyCurrentState Gets the current ServerState.
(Inherited from ServerBase)
Protected propertyDesignModeGets a value that indicates whether the Component is currently in design mode.
(Inherited from Component)
Public propertyEnabled Gets or sets a boolean value that indicates whether the server is currently enabled.
(Inherited from ServerBase)
Protected propertyEventsGets the list of event handlers that are attached to this Component.
(Inherited from Component)
Public propertyIgnoreInvalidCredentials Gets or sets a boolean value that indicates whether the server should ignore errors when the client's credentials are invalid.
Protected propertyInitialized Gets a boolean value that indicates whether the server has been initialized.
(Inherited from ServerBase)
Public propertyIntegratedSecurity Gets or sets a boolean value that indicates whether the client Windows account credentials are used for authentication.
Public propertyIsDisposed Gets a flag that indicates whether the object has been disposed.
(Inherited from ServerBase)
Public propertyMaxClientConnections Gets or sets the maximum number of clients that can connect to the server.
(Inherited from ServerBase)
Public propertyMaxSendQueueSize Gets or sets the maximum size for the send queue before payloads are dumped from the queue.
Public propertyName Gets the unique identifier of the server.
(Inherited from ServerBase)
Public propertyNoDelay Gets or sets a boolean value that determines if small packets are delivered to the remote host without delay.
Public propertyPayloadAware Gets or sets a boolean value that indicates whether the payload boundaries are to be preserved during transmission.
Public propertyPayloadEndianOrder Gets or sets the endian order to apply for encoding and decoding payload size in a PayloadAware transmission.
Public propertyPayloadMarker Gets or sets the byte sequence used to mark the beginning of a payload in a PayloadAware transmission.
Public propertyPersistSettings Gets or sets a boolean value that indicates whether the server settings are to be saved to the config file.
(Inherited from ServerBase)
Protected propertyReadIndicies Gets current read indices for received data buffers incremented at each Read(Guid, Byte, Int32, Int32) call.
(Inherited from ServerBase)
Public propertyReceiveBufferSize Gets or sets the size of the buffer used by the server for receiving data from the clients.
(Inherited from ServerBase)
Public propertyRunTime Gets the Time for which the server has been running.
(Inherited from ServerBase)
Public propertySendBufferSize Gets or sets the size of the buffer used by the server for sending data to the clients.
(Inherited from ServerBase)
Public propertyServer Gets the Socket object for the TcpServer.
Public propertyServerID Gets the server's ID.
(Inherited from ServerBase)
Public propertySettingsCategory Gets or sets the category under which the server settings are to be saved to the config file if the PersistSettings property is set to true.
(Inherited from ServerBase)
Public propertySiteGets or sets the ISite of the Component.
(Inherited from Component)
Public propertyStatus Gets the descriptive status of the server.
(Overrides ServerBaseStatus)
Public propertyTextEncoding Gets or sets the Encoding to be used for the text sent to the connected clients.
(Inherited from ServerBase)
Public propertyTransportProtocol Gets the TransportProtocol used by the server for the transportation of data with the clients.
(Inherited from ServerBase)
Top
Methods
 NameDescription
Public methodBeginInit Performs necessary operations before the server properties are initialized.
(Inherited from ServerBase)
Public methodCreateObjRefCreates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
(Inherited from MarshalByRefObject)
Public methodDisconnectAll Disconnects all of the connected clients.
(Inherited from ServerBase)
Public methodDisconnectOne Disconnects the specified connected client.
(Overrides ServerBaseDisconnectOne(Guid))
Public methodDisposeReleases all resources used by the Component.
(Inherited from Component)
Protected methodDispose(Boolean) Releases the unmanaged resources used by the server and optionally releases the managed resources.
(Inherited from ServerBase)
Public methodEndInit Performs necessary operations after the server properties are initialized.
(Inherited from ServerBase)
Public methodEqualsDetermines whether the specified object is equal to the current object.
(Inherited from Object)
Protected methodFinalizeReleases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection.
(Inherited from Component)
Public methodGetHashCodeServes as the default hash function.
(Inherited from Object)
Public methodGetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance.
(Inherited from MarshalByRefObject)
Protected methodGetServiceReturns an object that represents a service provided by the Component or by its Container.
(Inherited from Component)
Public methodGetTypeGets the Type of the current instance.
(Inherited from Object)
Public methodInitialize Initializes the server.
(Inherited from ServerBase)
Public methodInitializeLifetimeServiceObtains a lifetime service object to control the lifetime policy for this instance.
(Inherited from MarshalByRefObject)
Public methodIsClientConnected Determines whether the given client is currently connected to the server.
(Inherited from ServerBase)
Public methodLoadSettings Loads saved TcpServer settings from the config file if the PersistSettings property is set to true.
(Overrides ServerBaseLoadSettings)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object.
(Inherited from Object)
Protected methodMemberwiseClone(Boolean)Creates a shallow copy of the current MarshalByRefObject object.
(Inherited from MarshalByRefObject)
Public methodMulticast(Byte) Sends data to all of the connected clients synchronously.
(Inherited from ServerBase)
Public methodMulticast(Object) Sends data to all of the connected clients synchronously.
(Inherited from ServerBase)
Public methodMulticast(String) Sends data to all of the connected clients synchronously.
(Inherited from ServerBase)
Public methodMulticast(Byte, Int32, Int32) Sends data to all of the connected clients synchronously.
(Inherited from ServerBase)
Public methodMulticastAsync(Byte) Sends data to all of the connected clients asynchronously.
(Inherited from ServerBase)
Public methodMulticastAsync(Object) Sends data to all of the connected clients asynchronously.
(Inherited from ServerBase)
Public methodMulticastAsync(String) Sends data to all of the connected clients asynchronously.
(Inherited from ServerBase)
Public methodMulticastAsync(Byte, Int32, Int32) Sends data to all of the connected clients asynchronously.
(Inherited from ServerBase)
Protected methodOnClientConnected Raises the ClientConnected event.
(Inherited from ServerBase)
Protected methodOnClientConnectingException Raises the ClientConnectingException event.
(Inherited from ServerBase)
Protected methodOnClientDisconnected Raises the ClientDisconnected event.
(Inherited from ServerBase)
Protected methodOnReceiveClientData Raises the ReceiveClientData event.
(Inherited from ServerBase)
Protected methodOnReceiveClientDataComplete Raises the ReceiveClientDataComplete event.
(Inherited from ServerBase)
Protected methodOnReceiveClientDataException(Guid, Exception) Raises the ReceiveClientDataException event.
(Overrides ServerBaseOnReceiveClientDataException(Guid, Exception))
Protected methodOnReceiveClientDataException(Guid, SocketException) Raises the ReceiveClientDataException event.
Protected methodOnSendClientDataComplete Raises the SendClientDataComplete event.
(Inherited from ServerBase)
Protected methodOnSendClientDataException Raises the SendClientDataException event.
(Overrides ServerBaseOnSendClientDataException(Guid, Exception))
Protected methodOnSendClientDataStart Raises the SendClientDataStart event.
(Inherited from ServerBase)
Protected methodOnServerStarted Raises the ServerStarted event.
(Inherited from ServerBase)
Protected methodOnServerStopped Raises the ServerStopped event.
(Inherited from ServerBase)
Protected methodOnUnhandledUserException Raises the UnhandledUserException event.
(Inherited from ServerBase)
Public methodRead Reads a number of bytes from the current received data buffer and writes those bytes into a byte array at the specified offset.
(Overrides ServerBaseRead(Guid, Byte, Int32, Int32))
Protected methodReStart Re-starts the server if currently running.
(Inherited from ServerBase)
Public methodSaveSettings Saves TcpServer settings to the config file if the PersistSettings property is set to true.
(Overrides ServerBaseSaveSettings)
Protected methodSendDataToAsync Sends data to the specified client asynchronously.
(Overrides ServerBaseSendDataToAsync(Guid, Byte, Int32, Int32))
Public methodSendTo(Guid, Byte) Sends data to the specified client synchronously.
(Inherited from ServerBase)
Public methodSendTo(Guid, Object) Sends data to the specified client synchronously.
(Inherited from ServerBase)
Public methodSendTo(Guid, String) Sends data to the specified client synchronously.
(Inherited from ServerBase)
Public methodSendTo(Guid, Byte, Int32, Int32) Sends data to the specified client synchronously.
(Inherited from ServerBase)
Public methodSendToAsync(Guid, Byte) Sends data to the specified client asynchronously.
(Inherited from ServerBase)
Public methodSendToAsync(Guid, Object) Sends data to the specified client asynchronously.
(Inherited from ServerBase)
Public methodSendToAsync(Guid, String) Sends data to the specified client asynchronously.
(Inherited from ServerBase)
Public methodSendToAsync(Guid, Byte, Int32, Int32) Sends data to the specified client asynchronously.
(Inherited from ServerBase)
Public methodStart Starts the TcpServer synchronously and begins accepting client connections asynchronously.
(Overrides ServerBaseStart)
Public methodStop Stops the TcpServer synchronously and disconnects all connected clients.
(Overrides ServerBaseStop)
Public methodToStringReturns a String containing the name of the Component, if any. This method should not be overridden.
(Inherited from Component)
Public methodTryGetClient Gets the TransportProviderT object associated with the specified client ID.
Public methodTryGetClientPrincipal Gets the WindowsPrincipal object associated with the specified client ID.
Protected methodValidateConfigurationString Validates the specified configurationString.
(Overrides ServerBaseValidateConfigurationString(String))
Top
Events
 NameDescription
Public eventClientConnected Occurs when a client connects to the server.
(Inherited from ServerBase)
Public eventClientConnectingException Occurs when an exception is encountered while a client is connecting.
(Inherited from ServerBase)
Public eventClientDisconnected Occurs when a client disconnects from the server.
(Inherited from ServerBase)
Public eventDisposedOccurs when the component is disposed by a call to the Dispose method.
(Inherited from Component)
Public eventReceiveClientData Occurs when unprocessed data has been received from a client.
(Inherited from ServerBase)
Public eventReceiveClientDataComplete Occurs when data received from a client has been processed and is ready for consumption.
(Inherited from ServerBase)
Public eventReceiveClientDataException Occurs when an Exception is encountered when receiving data from a client.
(Inherited from ServerBase)
Public eventSendClientDataComplete Occurs when data has been sent to a client.
(Inherited from ServerBase)
Public eventSendClientDataException Occurs when an Exception is encountered when sending data to a client.
(Inherited from ServerBase)
Public eventSendClientDataStart Occurs when data is being sent to a client.
(Inherited from ServerBase)
Public eventServerStarted Occurs when the server is started.
(Inherited from ServerBase)
Public eventServerStopped Occurs when the server is stopped.
(Inherited from ServerBase)
Public eventUnhandledUserException Occurs when an Exception is encountered in a user-defined function via an event dispatch.
(Inherited from ServerBase)
Top
Fields
 NameDescription
Public fieldStatic memberDefaultAllowDualStackSocket Specifies the default value for the AllowDualStackSocket property.
Public fieldStatic memberDefaultConfigurationString Specifies the default value for the ConfigurationString property.
Public fieldStatic memberDefaultIgnoreInvalidCredentials Specifies the default value for the IgnoreInvalidCredentials property.
Public fieldStatic memberDefaultIntegratedSecurity Specifies the default value for the IntegratedSecurity property.
Public fieldStatic memberDefaultMaxSendQueueSize Specifies the default value for the MaxSendQueueSize property.
Public fieldStatic memberDefaultNoDelay Specifies the default value for the NoDelay property.
Public fieldStatic memberDefaultPayloadAware Specifies the default value for the PayloadAware property.
Top
Extension Methods
 NameDescription
Public Extension MethodGetEnumValueOrDefault Gets the enumeration constant for value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions)
Public Extension MethodGetEnumValueOrDefaultT Gets the enumeration constant for this value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions)
Top
Remarks
The Server socket can be bound to a specified interface on a machine with multiple interfaces by specifying the interface in the ConfigurationString (Example: "Port=8888; Interface=127.0.0.1")
Example
This example shows how to use the TcpServer component:
C#
using System;
using GSF;
using GSF.Communication;
using GSF.Security.Cryptography;
using GSF.IO.Compression;

class Program
{
    static TcpServer m_server;

    static void Main(string[] args)
    {
        // Initialize the server.
        m_server = new TcpServer("Port=8888");
        m_server.Handshake = false;
        m_server.PayloadAware = false;
        m_server.ReceiveTimeout = -1;
        m_server.Encryption = CipherStrength.None;
        m_server.Compression = CompressionStrength.NoCompression;
        m_server.SecureSession = false;
        m_server.Initialize();
        // Register event handlers.
        m_server.ServerStarted += m_server_ServerStarted;
        m_server.ServerStopped += m_server_ServerStopped;
        m_server.ClientConnected += m_server_ClientConnected;
        m_server.ClientDisconnected += m_server_ClientDisconnected;
        m_server.ReceiveClientDataComplete += m_server_ReceiveClientDataComplete;
        // Start the server.
        m_server.Start();

        // Multicast user input to all connected clients.
        string input;
        while (string.Compare(input = Console.ReadLine(), "Exit", true) != 0)
        {
            m_server.Multicast(input);
        }

        // Stop the server on shutdown.
        m_server.Stop();
    }

    static void m_server_ServerStarted(object sender, EventArgs e)
    {
        Console.WriteLine("Server has been started!");
    }

    static void m_server_ServerStopped(object sender, EventArgs e)
    {
        Console.WriteLine("Server has been stopped!");
    }

    static void m_server_ClientConnected(object sender, EventArgs<Guid> e)
    {
        Console.WriteLine(string.Format("Client connected - {0}.", e.Argument));
    }

    static void m_server_ClientDisconnected(object sender, EventArgs<Guid> e)
    {
        Console.WriteLine(string.Format("Client disconnected - {0}.", e.Argument));
    }

    static void m_server_ReceiveClientDataComplete(object sender, EventArgs<Guid, byte[], int> e)
    {
        Console.WriteLine(string.Format("Received data from {0} - {1}.", e.Argument1, m_server.TextEncoding.GetString(e.Argument2, 0, e.Argument3)));
    }
}
See Also