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 Windows Forms environment. Each client is processed on a dedicated worker thread.

 

private void buttonStart_Click(object sender, EventArgs e)
{
    //Start the echo server on port 7.
    //Each client is processed on its own worker thread.
    server1.Start(new ConnectThreadStart(acceptConnection), 7, null);
}

private 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);
}

private void buttonStop_Click(object sender, EventArgs e)
{
    //Gracefully stop the echo server.
    server1.Close();
}

 

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