Glossary Item Box

PowerTCP SSL Sockets for .NET

Creating A Simple Echo Server

This walk-through demonstrates how to create a simple, multi-threaded echo server with the Server component. The server will simply accept all connections, receive data, and send the data back to the client.

 

To create a simple echo server.

  1. Add the Server component to a new form. For instructions on how to do this see Placing Components on a Form .
  2. Add a button to the form.
  3. In the button's onClick event place the following code to "start" the server.

    [Visual Basic, C#]

    [C#]
    // Begin listening on port 7 (the well-known port for echo)
    server1.Listen(7);
    
    [Visual Basic]
    ' Begin listening on port 7 (the well-known port for echo)
    Server1.Listen(7)
    
  4. Implement the Connection event. For instructions on how to implement events see "Using Events in TCP". When a connection is made to the server, the Connection event will be raised on a new thread. A Tcp object will be made accessible in this event. Use this object to receive data from the client and resend ("echo") the data to the client. When complete, the Connection event should look like the following.

    [Visual Basic, C#]

    [C#]
    private void server1_Connection(object sender, Dart.PowerTCP.SslSockets.ConnectionEventArgs e)
    {
       // The Tcp instance used to communicate with the client is
       // found in e.Tcp.
       try
       { 
          // Keep receiving and echoing until the client closes the connection
          while (e.Tcp.Connected); // This is true until client closes the connection.
          {
             // Receive the data from the client.
             string s = e. Tcp.Receive ().ToString();
    
             // Send the data back to the client
             e.Tcp.Send(s);
          }
       }
       catch (Exception ex)
       {
          // just eat any exceptions
       }
    }
    
    [Visual Basic]
    Private Sub Server1_Connection(ByVal sender As Object, ByVal e As Dart.PowerTCP.SslSockets.ConnectionEventArgs) Handles Server1.Connection
       ' The Tcp instance used to communicate with the client is
       ' found in e.Tcp.
       Try
          ' Keep receiving and echoing until the client closes the connection
          Do While (e.Tcp.Connected) ' This is true until client closes the connection.
             ' Receive the data from the client.
             Dim s As String = e. Tcp.Receive ().ToString()
    
             ' Send the data back to the client
             e.Tcp.Send(s)
          Loop
       Catch ex As Exception
          ' just eat any exceptions
       End Try
    End Sub
    
  5. Compile and run the application. Click the button to start the server.
  6. Connect to the server using a TCP client. Send some data. The data should be echoed back.

 

In This Section

Basic Sending and Receiving of Data
Demonstrates the simplest usage examples for the Tcp component.
Receiving All Data
Demonstrates how to receive all data from the server.
Receiving Fixed Chunks of Data
Demonstrates how to receive fixed size blocks of data from the server.
Receiving to a Delimiter
Demonstrates how to receive data from the server until a delimiter is reached.
Asynchronous Use
Demonstrates how to use the Tcp component to send or receive data asynchronously from the server.
Creating a Simple Echo Server Application
Provides a step-by-step walkthrough demonstrating how to create a simple server application which simply echoes back all data it receives.
Creating a Server Which Handles Commands
Provides a step-by-step walkthrough demonstrating how to create a simple server application which understands and replies to commands.

 

 


Send comments on this topic.

Documentation version 1.1.2.0.

© 2008 Dart Communications.  All rights reserved.