Вы находитесь на странице: 1из 7

2010

Carbonum Labs
www.carbonum.com
Copyright (c) 2009-2014

[ECHO SERVER]
Creating an Echo Server application with Port Manager 2010.
Echo server

Echo Server
In this example we are going to create the mandatory EchoServer example and perform tests using
the “Port Monitoring and Testing” tool provided alongside with your component.

To begin with, create a new Windows Form project named EchoServer. Then add a few controls to
your main form to match the layout shown in figure 1.

Figure 1. Layout of our EchoServer Main Form

Notice we have already added a PortManager component to this form. Right click this component
to open the contextual menu, and select the “Port Configuration” option. In the configuration
form, add a new port. You can give this port whatever ID you want and configure it as a server
port, using the TcpServer interface. In this example we have chosen to use port 31000 to listen for
incoming connections, there is no need to change the receive buffer size nor to add any message
delimiters. Your screen should look like that shown in figure 2.

Page 2
Echo server

Figure 2. Add a new Server Port and add the OnReceiveData event handler.

Once the port has been saved, click on the “OnReceiveData” button (highlighted in figure 2) to add
the corresponding event handler for this port. Fill the event handler with the following code:

private void EchoPort_OnReceiveData(IComPort sender, ReceiveEventArgs e)


{
//whatever we receive, we send it back...
e.Connection.Send(e.Buffer, e.Offset, e.Count);

//Also we display it on the screen.


string s = sender.Encoding.GetString(e.Buffer, e.Offset, e.Count);
txtRecv.AppendText("Received: ");
txtRecv.AppendText(s);
txtRecv.AppendText("\r\n");
}

Page 3
Echo server

We also need to write the event handlers for the start and stop buttons as follow:

private void btnStart_Click(object sender, EventArgs e)


{
btnStart.Enabled = false;
portManager1.Start();
btnStop.Enabled = true;
lStatus.Text = "Started...";
}

private void btnStop_Click(object sender, EventArgs e)


{
btnStop.Enabled = false;
portManager1.Stop();
btnStart.Enabled = true;
lStatus.Text = "Stoped...";
}

Finally before we can test the application, we need to configure the SyncControl property of the
PortManager in order to prevent a Cross-Thread exception to occur in our event handlers. This is
because all events handlers associated to the PortManager component are executed in a
separated thread from the thread pool; by setting the SyncControl property we are asking the
PortManager to execute the event handlers in the GUI thread. See figure 3.

Page 4
Echo server

Figure 3. Set the SyncControl property to avoid Cross-Thread exceptions.

You must avoid setting this property when there is not going to be any interaction between your
event handlers and the GUI or when you are going to handle the thread synchronization yourself.

Testing
Compile and run the application and hit the start button. The service is now running. In order to
verify that everything is working as expected we can start a new instance of the PortMonitor
application found in your installation folder (see figure 4).

Figure 4. PortMonitor Application.

Page 5
Echo server

Use the “Testing/Tcp Client” option from the main menu to start a new tcp client tab. In this tab
type “localhost” in the IP field, and 31000 in the port field. Then click the connect button. At this
point we should be connected to our echo service (see figure 5).

Figure 5. Create a new Tcp Client tab and connect to the EchoServer.

Now you can type whatever you like and hit the send button, you must get an echo of whatever
you sent, and your service form should display it too.

Feel free to create more Tcp client tabs in the Port Monitor application to verify that we can
handle several concurrent connections without problems (figure 6).

Page 6
Echo server

Figure 6. Handling multiple client connections.

Note: The complete Visual Studio solution for this example can be found in your installation route
under the examples folder, look for the EchoServer.

Page 7

Вам также может понравиться