PowerTCP Sockets for .NET

from $349.00
Available Platforms
<

Worker Thread Server Code Example

See all PowerTCP Sockets for .NET Samples and Code Examples

 

The following example demonstrates an Echo Server in a console environment. Each client is processed on a dedicated worker thread.

 

static void Main(string[] args)
{
     Server server1 = new Server();

     //Start the echo server on port 7.
     //Each client is processed on its own worker thread.
     server1.Start(new ConnectThreadStart(acceptConnection), 7, null);

     Console.WriteLine("Server started. Press <Enter> to stop.");
     Console.ReadLine();
     server1.Close();
}

static void acceptConnection(TcpBase connection, object state)
{
     //While connection is open, echo data received back to the client.
     //This function executes on a worker thread.
     byte[] buffer = new byte[1024];
     do
     {
         Data data = connection.Read(buffer);
         if (data != null)
         connection.Write(data.Buffer, data.Offset, data.Count);
     } while (connection.State == Dart.Sockets.ConnectionState.Connected);
}

 

To download a trial please visit the PowerTCP Sockets for .NET product page.