Server Using Worker Threads Code Example

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];
        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.