Glossary Item Box

PowerTCP SSL Sockets for .NET

Creating A Server Which Handles Commands

As the topic Creating A Simple Echo Server demonstrates, it is quite easy to create a server application using the Server component. With a few modifications to the echo server, you can create a server application which responds to commands. This is useful if you are implementing a protocol.

This walk-through won't attempt to demonstrate implementing an actual protocol, but instead it demonstrates a server that has three conditions:

 

To create a server which handles commands

  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 8888
    server1.Listen(8888);
    
    [Visual Basic]
    ' Begin listening on port 8888
    Server1.Listen(8888)
    
  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 the data from the server. Check the data to see which of the three conditions it meets ("COUNT", "QUIT", or other) and send an appropriate response. When complete, the Connection event should look similar 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
       {
          while(e.Tcp.Connected)
          {
             // Receive data until a CRLF is found or 1024 bytes are read using the stream-interface.
             bool found = false;
             string s = e.Tcp.Stream.Read("\r\n", 1024, ref found);
    
             // Trim any whitespace
             s = s.Trim();
                                                    
             // Take action on the command
             switch(s.ToUpper())
             {
                case "COUNT":
                   // Send connection count
                   e.Tcp.Stream.Write("Current connection(s): " + server1.Connections.Count + "\r\n");
                   break;
                case "QUIT":
                   // Send goodbye greeting and disconnect
                   e.Tcp.Stream.Write("Goodbye\r\n");
                   e.Tcp.Close();
                   break;
                default:
                   // Send error message
                   e.Tcp.Stream.Write("Command " + s + " not understood.\r\n");
                   break;
             }
          }
       }
       catch(Exception ex)
       {
          // Just eat the exception
       }
    }
    
    [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
          Do While (e.Tcp.Connected)
    
             ' Receive data until a CRLF is found or 1024 bytes are read using the stream-interface.
             Dim found As Boolean = False
             Dim s As String = e.Tcp.Stream.Read(vbCrLf, 1024, found)
    
             ' Trim any whitespace
             s = s.Trim()
     
             ' Take action on the command
             Select Case (s.ToUpper())
                Case "COUNT"
                   ' Send connection count
                   e.Tcp.Stream.Write("Current connection(s): " + Server1.Connections.Count.ToString() + vbCrLf)
                Case "QUIT"
                   ' Send goodbye greeting and disconnect
                   e.Tcp.Stream.Write("Goodbye" + vbCrLf)
                   e.Tcp.Close()
                Case Else
                   ' Send error message
                   e.Tcp.Stream.Write("Command " + s + " not understood." + vbCrLf)
             End Select
          Loop
       Catch ex As Exception
          ' Just eat the exception
       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 the "COUNT" and "QUIT" commands to see if the server is responding properly. You could easily modify this server to implement a protocol such as FTP, SMTP, POP, or any other TCP-based protocol.

 

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.