S04: TCP Server Demo (C++)
This sample describes a TCP server that is implemented in C++.
It accepts a TCP connection, receives TCP packets and returns them to the sender (“echo server”).
Download
Download the sample: https://github.com/Beckhoff/TF6311_Samples/tree/main/S04-TCPServer
- 1. Get the sample from GitHub, unzip the downloaded ZIP file if necessary
- 2. Open the project with TwinCAT XAE
- 3. Select your target system
- 4. Configure the network card (see below) for the target system
- 5. Build the sample on your local machine (e.g. Build->Build Solution)
- 6. Activate the configuration
Description
The interface ITcIoTcpProtocolRecv is implemented and a pointer to a ITcIoTcpProtocol is used analogous to the Quick Starts in this sample.
The “CheckReceived” method is called in the “CycleUpdate” method. In this way the TCP/UDP RT module is enabled to process incoming packets and transmit callbacks on the “ReceiveEvent” und “ReceiveData” methods to the module.
In order to take into account incoming connections, a port is opened in “CycleUpdate” via “AllocSocket” and “Listen”. “Accept” is called in the “ReceiveEvent”, if an event to establish a connection has occurred.
In this sample the “ReceiveData” method uses the “SendData” method to return the data to the sender (“echo server”).
Preparing the network card
For the TCP/UDP RT module, make sure that the RT Ethernet adapter in the TwinCAT solution is connected with the correct network card (with TwinCAT driver).
![]() | Local configuration only Installation of the driver on compatible network cards via the button "Compatible Devices" always takes place locally. On a controller with TwinCAT XAR, the program TcRteInstall.exe can be used. It is included in the installation (usually under C:TwinCAT\3.1\System). |
