UdpServer Class

Represents a UDP-based communication server.

Definition

Namespace: Gemstone.Communication
Assembly: Gemstone.Communication (in Gemstone.Communication.dll) Version: 1.0.110 -- Release Build+0c64c8391bfe7dd5190df3a4b323a05770415384
public class UdpServer : ServerBase
Inheritance
Object    ServerBase    UdpServer

Remarks

Use UdpServer when the primary purpose is to transmit data.

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; Clients=localhost:8989; Interface=127.0.0.1")

The Server socket can be used just for transmitting data without being bound to a local interface by specifying -1 for the port number in the ConfigurationString (Example: "Port=-1; Clients=localhost:8989")

Example

This example shows how to use the UdpServer component:
C#
using System;
using GSF;
using GSF.Communication;
using GSF.Security.Cryptography;
using GSF.IO.Compression;

class Program
{
    static UdpServer m_server;

    static void Main(string[] args)
    {
        // Initialize the server.
        m_server = new UdpServer("Port=8888; Clients=localhost:8989");
        m_server.Handshake = 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)));
    }
}

Constructors

UdpServer Initializes a new instance of the UdpServer class.
UdpServer(String) Initializes a new instance of the UdpServer class.

Properties

AllowDualStackSocket Gets or sets a boolean value that determines if dual-mode socket is allowed when endpoint address is IPv6.
ClientIdentificationMode Gets or sets the mode by which the UDP server will identify its clients when receiving messages.
ClientIDs Gets the IDs of clients connected to the server.
(Inherited from ServerBase)
ConfigurationString Gets or sets the data required by the server to initialize.
(Inherited from ServerBase)
CurrentState Gets the current ServerState.
(Inherited from ServerBase)
DynamicClientEndPoints Gets or sets a boolean value that determines if UDP server should always send responses to clients on the port that data is received from the client.
Enabled Gets or sets a boolean value that indicates whether the server is currently enabled.
(Inherited from ServerBase)
Initialized Gets a boolean value that indicates whether the server has been initialized.
(Inherited from ServerBase)
IsDisposed Gets a flag that indicates whether the object has been disposed.
(Inherited from ServerBase)
MaxClientConnections Gets or sets the maximum number of clients that can connect to the server.
(Inherited from ServerBase)
MaxSendQueueSize Gets or sets the maximum size for the send queue before payloads are dumped from the queue.
Name Gets or sets the unique identifier of the server.
(Inherited from ServerBase)
ReadIndicies Gets current read indices for received data buffers incremented at each Read(Guid, Byte, Int32, Int32) call.
(Inherited from ServerBase)
ReceiveBufferSize Gets or sets the size of the buffer used by the server for receiving data from the clients.
(Inherited from ServerBase)
RunTime Gets the Time for which the server has been running.
(Inherited from ServerBase)
SendBufferSize Gets or sets the size of the buffer used by the server for sending data to the clients.
(Inherited from ServerBase)
Server Gets the Socket object for the UdpServer.
ServerID Gets the server's ID.
(Inherited from ServerBase)
Status Gets the descriptive status of the client.
(Overrides ServerBaseStatus)
TextEncoding Gets or sets the Encoding to be used for the text sent to the connected clients.
(Inherited from ServerBase)
TransportProtocol Gets the TransportProtocol used by the server for the transportation of data with the clients.
(Inherited from ServerBase)

Methods

DisconnectAll Disconnects all of the connected clients.
(Inherited from ServerBase)
DisconnectOne Disconnects the specified connected client.
(Overrides ServerBaseDisconnectOne(Guid))
Dispose Releases all the resources used by the ServerBase object.
(Inherited from ServerBase)
Dispose(Boolean) Releases the unmanaged resources used by the ServerBase object and optionally releases the managed resources.
(Inherited from ServerBase)
EqualsDetermines whether the specified object is equal to the current object.
(Inherited from Object)
FinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object)
GetHashCodeServes as the default hash function.
(Inherited from Object)
GetTypeGets the Type of the current instance.
(Inherited from Object)
Initialize Initializes the server.
(Inherited from ServerBase)
IsClientConnected Determines whether the given client is currently connected to the server.
(Inherited from ServerBase)
MemberwiseCloneCreates a shallow copy of the current Object.
(Inherited from Object)
Multicast(Byte) Sends data to all of the connected clients synchronously.
(Inherited from ServerBase)
Multicast(String) Sends data to all of the connected clients synchronously.
(Inherited from ServerBase)
Multicast(Byte, Int32, Int32) Sends data to all of the connected clients synchronously.
(Inherited from ServerBase)
MulticastAsync(Byte) Sends data to all of the connected clients asynchronously.
(Inherited from ServerBase)
MulticastAsync(String) Sends data to all of the connected clients asynchronously.
(Inherited from ServerBase)
MulticastAsync(Byte, Int32, Int32) Sends data to all of the connected clients asynchronously.
(Inherited from ServerBase)
OnClientConnected Raises the ClientConnected event.
(Inherited from ServerBase)
OnClientConnectingException Raises the ClientConnectingException event.
(Inherited from ServerBase)
OnClientDisconnected Raises the ClientDisconnected event.
(Inherited from ServerBase)
OnReceiveClientData Raises the ReceiveClientData event.
(Inherited from ServerBase)
OnReceiveClientDataComplete Raises the ReceiveClientDataComplete event.
(Inherited from ServerBase)
OnReceiveClientDataException Raises the ReceiveClientDataException event.
(Overrides ServerBaseOnReceiveClientDataException(Guid, Exception))
OnSendClientDataComplete Raises the SendClientDataComplete event.
(Inherited from ServerBase)
OnSendClientDataException Raises the SendClientDataException event.
(Overrides ServerBaseOnSendClientDataException(Guid, Exception))
OnSendClientDataStart Raises the SendClientDataStart event.
(Inherited from ServerBase)
OnServerStarted Raises the ServerStarted event.
(Inherited from ServerBase)
OnServerStopped Raises the ServerStopped event.
(Inherited from ServerBase)
Read 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))
ReStart Re-starts the server if currently running.
(Inherited from ServerBase)
SendDataToAsync Sends data to the specified client asynchronously.
(Overrides ServerBaseSendDataToAsync(Guid, Byte, Int32, Int32))
SendTo(Guid, Byte) Sends data to the specified client synchronously.
(Inherited from ServerBase)
SendTo(Guid, String) Sends data to the specified client synchronously.
(Inherited from ServerBase)
SendTo(Guid, Byte, Int32, Int32) Sends data to the specified client synchronously.
(Inherited from ServerBase)
SendToAsync(Guid, Byte) Sends data to the specified client asynchronously.
(Inherited from ServerBase)
SendToAsync(Guid, String) Sends data to the specified client asynchronously.
(Inherited from ServerBase)
SendToAsync(Guid, Byte, Int32, Int32) Sends data to the specified client asynchronously.
(Inherited from ServerBase)
Start Starts the UdpServer synchronously and begins accepting client connections asynchronously.
(Overrides ServerBaseStart)
Stop Stops the UdpServer synchronously and disconnects all connected clients.
(Overrides ServerBaseStop)
ToStringReturns a string that represents the current object.
(Inherited from Object)
TryGetClient Gets the TransportProviderT object associated with the specified client ID.
ValidateConfigurationString Validates the specified configurationString.
(Overrides ServerBaseValidateConfigurationString(String))

Events

ClientConnected Occurs when a client connects to the server.
(Inherited from ServerBase)
ClientConnectingException Occurs when an exception is encountered while a client is connecting.
(Inherited from ServerBase)
ClientDisconnected Occurs when a client disconnects from the server.
(Inherited from ServerBase)
Disposed Occurs when the ServerBase has been disposed.
(Inherited from ServerBase)
ReceiveClientData Occurs when unprocessed data has been received from a client.
(Inherited from ServerBase)
ReceiveClientDataComplete Occurs when data received from a client has been processed and is ready for consumption.
(Inherited from ServerBase)
ReceiveClientDataException Occurs when an Exception is encountered when receiving data from a client.
(Inherited from ServerBase)
SendClientDataComplete Occurs when data has been sent to a client.
(Inherited from ServerBase)
SendClientDataException Occurs when an Exception is encountered when sending data to a client.
(Inherited from ServerBase)
SendClientDataStart Occurs when data is being sent to a client.
(Inherited from ServerBase)
ServerStarted Occurs when the server is started.
(Inherited from ServerBase)
ServerStopped Occurs when the server is stopped.
(Inherited from ServerBase)

Fields

DefaultAllowDualStackSocket Specifies the default value for the AllowDualStackSocket property.
DefaultClientIdentificationMode Specifies the default value for the ClientIdentificationMode property.
DefaultConfigurationString Specifies the default value for the ConfigurationString property.
DefaultDynamicClientEndPoints Specifies the default value for the DynamicClientEndPoints property.
DefaultMaxSendQueueSize Specifies the default value for the MaxSendQueueSize property.

See Also