The program SR_TcpServer implements the program code for the TCP server functionality. For controlling and monitoring this functionality, the visualization screen VisuTcpServer is provided in the example project.
The variables which link the visualization with the program code are defined in the global variable list GVL_VisuTcpServer.
In the visualization, enter the number of the TCP port you want to open and the IP address of the corresponding Ethernet interface. To open this TCP port, click the Listen button in the visualization.
You can close an open port by clicking the Close button.
Accepting Incoming Connections
After the port has been opened, it is monitored by the TCP server for incoming connections and receiving data from connected clients.
Each incoming client connection is accepted. The number of connected clients is shown in the visualization screen VisuTcpServer.
Whether a connection to the TCP client is encrypted using TLS (Transport Layer Security) depends on the controller used.
From the visualization screen VisuTcpServer, you can disconnect a selected client or all clients at once.
The program provides a function for disconnecting clients after a specified time of inactivity. If no data has been received from a client for a certain time, this client is disconnected automatically. You can configure this maximum time of inactivity in the visualization screen VisuTcpServer.
If the TCP server detects that data has been received from a TCP client, the data is read and evaluated by the application. It is verified whether the data applies to the format of the defined application protocol.
Depending on the result of the evaluation, one of the following actions is executed by the SR_TcpServer program:
If |
Then |
---|---|
If the value received for the length of the text message meets the size of the text received and is not greater than the size of the receive buffer provided by the application (84 bytes) (expected condition) |
The data is read and is processed based on the expected data format. |
If the value received for the length of the text message does not meet the size of received data and is not greater than the size of the receive buffer provided by the application (84 bytes) |
The data is read and is processed based on the expected data format. A diagnostic message indicating the inconsistency of the given length is generated. |
If the value received for the length of the text message does not meet the size of received data and is greater than the size of the receive buffer |
The data is not read and the connection to this client is terminated. |
Whenever valid data is received from the TCP client, a response is sent.
The response is created depending on the received data. If the text message GetTimestamp has been received, the response data contains the time stamp of the system (RTC of the controller) as text message. If a text message other than GetTimestamp has been received, the response data contains the text message UnknownCommand.