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 message typed by the user in the HMI.

Output

The client should connect to the server and send the typed message 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 the TcpIp_WriteText filter to send one text message and the 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 the 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 the Not filter to negate the condition).

Add another button, "Send" to send text messages only on a user request. Use the MakeConditional 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 the 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 the output outSocket over the SendingLoop instance to create a new input. 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_WriteText filter inside the SendingLoop macrofilter.

  6. Connect inSocket with 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 window.

  8. Go back to the Program Editor and create the MakeConditional filter of type String above TcpIp_WriteText.

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

  10. Connect the output outConditionalObject to the 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 the Not and the Loop filters, and connect them together as well as with the "Exit" button.

  12. Add the DelayByPeriod filter to suspend the program workflow for inTime milliseconds relative to the end of the filter's last invoke time.

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

Further Readings