You are here: Start » Tutorial Exercises » TCP/IP: Shoutbox Client (tcpip_shoutbox_client)

TCP/IP: Shoutbox Client (tcpip_shoutbox_client)

Aim

Create a TCP client which connects to a server and sends text messages typed by the user in the HMI.

Input

The input is the text messages typed by the user in the HMI.

Output

The client should connect to a server and send the typed messages whenever the user clicks a "Send" button.

The client should disconnect and close when the user clicks an "Exit" button.

Hints

Use the TcpIp_Connect filter to connect to the server.

Create a Task macrofilter to handle multiple interactions. Add a TcpIp_WriteText filter to send one text message and a Loop filter to create a loop in the macrofilter. The input of the TcpIp_WriteText filter has to be connected with the output of the TcpIp_Connect filter (via an input of the task macrofilter).

Create an "Exit" button in the HMI (with ImpulseButton control) and connect it to the Loop filter in such a way, that the loop breaks when the button is pressed (you should use a Not filter to negate the condition).

Add another button, "Send" to send text messages only on a user request. Use an MakeConditional<String> filter to create a conditional String (a copy of the text-box content) only when the button is pressed.

Solution (AVS)

  1. Make sure that you have the server application already created.

  2. Add TcpIp_Connect filter and configure its inPort input.

  3. Create a task macrofilter, name it "SendingLoop" and place one instance of it in the Main macrofilter.

  4. Drag a connection from TcpIp_Connect.outSocket over the SendingLoop instance to create a new input and a 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 a TcpIp_WriteText filter inside the SendingLoop macrofilter.

  6. Connect the TcpIp_WriteText.inSocket input from the input of the macrofilter.

  7. Create an HMI with one TextBox and two ImpulseButton controls. Name the buttons "Send" and "Exit" in the Properties control.

  8. Go back to the Program Editor and create a MakeConditional<String> filter above TcpIp_WriteText.

  9. Connect the MakeConditional.inObject input to the output of the TextBox control and the inCondition input to the output of the "Exit" button control.

  10. Connect the output of the MakeConditional<String> filter to the TcpIp_WriteText.inText input, so that the latter filter is executed only when the button is pressed.

  11. To create a loop that breaks when the "Exit" button is pressed, add a Not and a Loop filter, and connect them together as well as with the "Exit" button.

  12. Go back to the Main macrofilter and add a TcpIp_Close filter to close the connection on exit.