TCP client with connection control

Hi! I ask for tips in writing a client with connection control. The server device accepts and responds to commands from the client, but sometimes the server can send data without a request from the client. As I understand it, the FreeRTOS_issocketconnected function simply checks the flags and not the actual presence of a connection (if, for example, the connection was physically interrupted).
The code examples usually work opening - sending-closing, but I need to keep the connection open and check that it is physically established.

You could use keep alive probing (when connection is idle)
See ipconfigTCP_KEEP_ALIVE(_INTERVAL) here:

This is a common feature of (other) TCP stacks.
Or implement your own heartbeat mechanism.

3 Likes

Thankk you! Will this function change the socket flag in case the connection is lost? And will the issocketconnected function return the correct value?

I think so. See the documentation:
If FreeRTOS-Plus-TCP does not receive a reply to a keep alive message then the connection will be broken and the socket will be marked as closed.
Subsequent FreeRTOS_recv() calls on the socket will return -pdFREERTOS_ERRNO_ENOTCONN.

2 Likes

The function FreeRTOS_issocketconnected() indeed only checks the flags.

When keep-alive is enabled, the socket will send a standard keep-alive packet at a regular interval. When not answered, the connection will be broken with a shutdown.

Then it is good to also call FreeRTOS_recv()and see what it returns.
If you use any of the application hooks (eg. FREERTOS_SO_TCP_CONN_HANDLER) or select(), your application will be woken up.

It is also good to enable ipconfigTCP_HANG_PROTECTION(_TIME).

I’m also a fan of leaving TCP connections open continuously, and send/receive packets asynchronously.

2 Likes

Thanks a lot! Would you recommend a windows application that can send raw tcp SYN FIN ACK packets for testing