Sample03: “Echo” client/server
This sample uses the functions of the former TcSocketHelper.Lib, which is now integrated into the Tc2_TcpIp library. It shows a client/server PLC application based on the functions of the former SocketHelper library.
The client cyclically sends a test string (sToServer) to the remote server. The server returns this string unchanged to the client (sFromServer). The difference between this sample and Sample02 is that the server can establish up to five connections and the client application can start up to five client instances. Each instance establishes a connection to the server.

System requirements
- TwinCAT 3 Build 3093 or higher
- TwinCAT 3 Function TF6310 TCP/IP
- If two computers are used to execute the sample (one client and one server), the Function TF6310 needs to be installed on both computers
- If one computer is used to execute the sample, e.g. client and server running in two separate PLC runtimes, both PLC runtimes need to run in separate tasks
Project information
The default communication settings used in the above samples are as follows:
- PLC client application: Port and IP address of the remote server: 200, '127.0.0.1'
- PLC server application: Port and IP address of the local server: 200, '127.0.0.1'
To test the client and server application on two different PCs, you have to adjust the port and the IP address accordingly.
However, you can also test the client and server samples with the default values on a single computer by loading the client application into the first PLC runtime system and the server application into the second PLC runtime system.
The behavior of the PLC project sample is determined by the following global variables/constants.
Constant | Value | Description |
---|---|---|
PLCPRJ_MAX_CONNECTIONS | 5 | Max. number of server->client connections. A server can establish connections to more than one client. A client can establish a connection to only one server at a time. |
PLCPRJ_SERVER_RESPONSE_TIMEOUT | T#10s | Max. delay time (timeout time) after which a server should send a response to the client. |
PLCPRJ_CLIENT_SEND_CYCLE_TIME | T#1s | Cycle time based on which a client sends send data (TX) to the server. |
PLCPRJ_RECEIVER_POLLING_CYCLE_TIME | T#200ms | Cycle time based on which a client or server polls for receive data (RX). |
PLCPRJ_BUFFER_SIZE | 10000 | Max. internal buffer size for RX/TX data. |
The PLC samples define and use the following internal error codes:
Error code | Value | Description |
---|---|---|
PLCPRJ_ERROR_RECEIVE_BUFFER_OVERFLOW | 16#8101 | The internal receive buffer reports an overflow. |
PLCPRJ_ERROR_SEND_BUFFER_OVERFLOW | 16#8102 | The internal send buffer reports an overflow. |
PLCPRJ_ERROR_RESPONSE_TIMEOUT | 16#8103 | The server has not sent the response within the specified timeout time. |
PLCPRJ_ERROR_INVALID_FRAME_FORMAT | 16#8104 | The telegram formatting is incorrect (size, faulty data bytes etc.). |
The client and server applications (FB_ServerApplication, FB_ClientApplication) were implemented as function blocks. The application and the connection can thus be instanced repeatedly.