Failed to connect to server: FreeRTOS_Connect failed: ReturnCode= -116 FreeRToS+TCP

Hi, i am using FreeRTOS+TCP library for MQTT Mutual Authentication application.
I am using socket wrapper layer given by Free RTOS demo application. My application is giving socket connection error and it is not consistent but connects once in a while, I tried to close socket when it fails and used FreeRTOS_connect() to try connection again, but this gives hardfault exception. can anyone suggest what can be the possible ways to fix this.

Error message:
"Failed to connect to server: FreeRTOS_Connect failed: ReturnCode= -116
Attaching screenshot below.

Which hardware are you using? Which project?

I’m not sure what I’m meant to be looking at in the screenshot - but I can see you using the tickless mode and semihosting. For debugging I would recommend setting configUSE_TICKLESS_IDLE to 0 to prevent the MCU going into a sleep mode, and turn off semihosting as it conflicts with FreeRTOS’s use of exceptions.

Thankyou for the response.
I am using cortexM55 based hardware which uses ethernet for network,
I am working on development of MQTT Mutual Authentication application where i am using mbedTLS and PKCS modules.
For socket wrapper layer(for socket connection), i am using FreeRTOS pkcs11_MQTT_Mutual_Authentication as reference and following the same.
As the socket connection was not getting established each time i try to run application on the board. I am not very sure what exactly i am missing.
It would be gret if you can enlighten me with sometips to achieve this. THANKS!

If you can capture network traffic that would be very useful to find out the cause of connection failure.
Also, turn off tickelsss idle as Richard suggested.

sure will check network traffic, i have changed configUSE_TICKLESS_IDLE to 0, but still socket connection is established few times when i run the application and not getting connected for sometime. attching logs below. THANKS!

It seems you are not getting a SYN-ACK from the other end. Please capture the network traffic and share.

You must have come across the following:

#define ETIMEDOUT  116 /* Connection timed out */

The actual “timeout” depends on what you have defined for ipconfigSOCK_DEFAULT_SEND_BLOCK_TIME.

Or you may have overruled this value by setting the socket option FREERTOS_SO_SNDTIMEO.
Both macros define a value in number of clock ticks.

When creating a TCP server calling accept(), you can set the maximum blocking time by defining ipconfigSOCK_DEFAULT_RECEIVE_BLOCK_TIME. Or by setting the socket option FREERTOS_SO_RCVTIMEO.

@aggarg wrote:

Please capture the network traffic and share.

It would indeed be helpful if you can make a capture of the network traffic with a program like tcpdump or WireShark.

About the crash: could you show the complete code that is calling FreeRTOS_connect() ? You can attach the code to your post using the Upload button.

@sruthiA, any news on this? Did you find the cause of the problem?

Hi, Thanks for the reply. I didn’t work on this for sometime, hence couldn’t reply you. I restarted working on it. I am attaching file below. It would be helpful if you can check and let me know.
Sockets_Wrapper.c (5.1 KB)

below are the timeout values in my project.
#define ipconfigSOCK_DEFAULT_SEND_BLOCK_TIME ( 5000 )
#define FREERTOS_SO_RCVTIMEO ( 0 ) /* Used to set the receive time out. /
#define FREERTOS_SO_SNDTIMEO ( 1 ) /
Used to set the send time out. */
#define ipconfigSOCK_DEFAULT_RECEIVE_BLOCK_TIME ( 5000 )

@sruthiA , your sockets_wrapper.c implementation looks reasonable.

Can you double check that the correct hostname and ip address are used?

Are you able to generate a packet capture of your device?

Have you checked if FreeRTOS_gethostbyname() always succeeds?

For a DNS lookup, it is important to set the following config items:

#define	ipconfigDNS_RECEIVE_BLOCK_TIME_TICKS    pdMS_TO_TICKS( 2500U )
#define	ipconfigDNS_SEND_BLOCK_TIME_TICKS       pdMS_TO_TICKS( 2500U )

This example puts them to 2.5 seconds, but you may choose a longer period.

Can you tell the actual value of the parameter receiveTimeoutMs?

As I wrote earlier, FreeRTOS_connect() uses FREERTOS_SO_SNDTIMEO as its timeout, whereas FreeRTOS_accept() takes the value of FREERTOS_SO_RCVTIMEO.

Although most TCP connections to a host on the Internet are established very quickly, you better use large timeouts, up to 20 seconds.

Note that FreeRTOS_shutdown() should only be called as long as the socket is in a connected state, although FreeRTOS_recv() will return an error if the socket is not connected anymore.

Hi @htibosch, thanks for the information.
I have increased the timeout of FreeRTOS_connect() to 10seconds. Now i am not getting any socket connection error. My MQTT application is working.
Thanks !!

Great, and thank you for reporting back.

Within Ubuntu and Windows, I observed that a TCP connect makes up to 4 attempts. Only after 20 seconds the connect times out.