You are here: Start » Tutorial Exercises » TCP/IP: Shoutbox Server (tcpip_shoutbox_server)

TCP/IP: Shoutbox Server (tcpip_shoutbox_server)

Aim

Create a TCP server which receives text messages and displays them in the HMI.

Input

There will be an incoming connection on the TCP port 12345 and consecutive text messages will be sent as lines of text.

At this stage of the training program we have no client application, so for testing purposes we will use the command-line tool "telnet".

Please, turn it on at Control Panel > Programs > Turn Windows features on or off > Telnet Client.

When the server is ready, we will open the Windows Command Prompt and issue the following command:

telnet localhost 12345

or if accessing the server from another computer:

telnet <ip-address> 12345

Then we will type consecutive text messages.

Output

The server should accept the connection and then display received text messages in the HMI.

Hints

Use the TcpIp_Accept filter to accept the connection from a client. In the default configuration it will wait forever until the client is connected.

Create a Task macrofilter with the Loop filter to handle multiple interactions with the client. Use the TcpIp_ReadLine filter to receive one text message. Its input must be connected with the output of the TcpIp_Accept filter (via the input of the Task macrofilter).

Solution (AVS)

  1. Make sure you have "telnet" command installed.

  2. Add the TcpIp_Accept filter to accept the connection from the client.

  3. Create a Task macrofilter, name it "ReceivingLoop" and add an instance of it to the Main macrofilter.

  4. Drag a connection from outSocket over the instance of the ReceivingLoop macrofilter to create a new input and the connection. Change the input type from "SocketId?" to "SocketId", so that the entire macrofilter is not executed if the connection is not successful.

  5. Add the TcpIp_ReadLine filter and the Loop filter inside the macrofilter.

  6. Connect the inSocket input with the input of the macrofilter.

  7. Create a TextBox control in the HMI and connect it with the outText output.

  8. Add the Not filter and connect the outEof output of the previous filter with inValue.

  9. Add the Loop filter and connect outValue to its input.

  10. Go back to the Main macrofilter and add a TcpIp_Close filter to close the connection in case the program gets to this point.

Further Readings