# When TCP only receives data and does not send data, the FreeRTOS-recv function cannot return for a long time

**URL:** <https://forums.freertos.org/t/when-tcp-only-receives-data-and-does-not-send-data-the-freertos-recv-function-cannot-return-for-a-long-time/19655>\
**Category:** Libraries\
**Created:** [March 27, 2024, 1:43pm UTC](https://forums.freertos.org/t/when-tcp-only-receives-data-and-does-not-send-data-the-freertos-recv-function-cannot-return-for-a-long-time/19655 "2024-03-27T13:43:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hane\_vv](https://avatars.discourse-cdn.com/v4/letter/h/e8c25b/32.png) [@hane\_vv](https://forums.freertos.org/u/hane_vv)\
**Post date:** [March 27, 2024, 1:43pm UTC](https://forums.freertos.org/t/when-tcp-only-receives-data-and-does-not-send-data-the-freertos-recv-function-cannot-return-for-a-long-time/19655/1 "2024-03-27T13:43:38Z")

</div>

FreeRTOS Kernel V10.4.6，FreeRTOS Plus TCPIP v3.1.0.  
When TCP only receives data and does not send data, the FreeRTOS-recv function cannot return for a long time.  
When I establish a TCP client or server and successfully connect to the computer, there is no problem when I perform a loopback test (when the computer sends it to FreeRTOS, FreeRTOS receives it and returns it to the computer),every millisecond when the computer sends 8000 bytes of FreeRTOS, it can return it to the computer. But if FreeRTOS only receives but does not send to the computer, then it is not a problem for the computer to keep sending small packets (such as sending 10 bytes every 20 milliseconds); If larger packets are sent, the FreeRTOS-recv function may require a delay to return. For example, if a computer sends 600 bytes per second to FreeRTOS for a total of 10 seconds (6000 bytes in total), it may receive 600, 1200, and later receive 3000, 6000, with a delay time of several seconds. At this point, FreeRTOS does not respond every time it receives a TCP segment. I want to know how to respond every frame received.  
Is it related to the Nagle algorithm and ACK delay?  
Or is it related to random numbers? The STM32F103VET6 I used does not have hardware random numbers, so it always returns 10000+138 \* n (++) each time;

My current settings are as follows:  
#Define ipconfigUSE.TCP\_WIN 0  
#Define ipconfigTCP\_TX-BUFFER-LENGTH 1024  
#Define ipconfigTCP\_RX-BUFFER-LENGTH 1024  
#Define ipconfigNUM-NETWORK BUFFER-DESCRIPTORS 16  
#Define ipconfigEVEN\_QUEUE-LENGTH (ipconfigNUM-NETWORK BUFFER-DESCRIPTORS+8)  
#Define ipconfigTCP MSS ( ipconfigNETWORK\_MTU - ipSIZE\_OF\_IPv4\_HEADER - ipSIZE\_OF\_TCP\_HEADER )  
#Define ipconfigTCP\_KEEP-ALIVE 1  
#Define ipconfigTCP\_KEEP-ALIVE-INTERVAL 20  
#Define ipconfigTCP\_HANG-PROTATION 1  
#Define ipconfigTCP\_HANG-PROTATION-TIME 30  
#Define ipconfigIGNORE-UNKNOWN-PACKETS 0  
#Define ipconfigSOCK-DEFAULT-RECEIVE-Block-TIME pdMSTO-TICKS (5000)  
#Define ipconfigSOCK-DEFAULT-SEND-BLOCK-TIME pdMS-TO-TICKS (5000)

---

<div class="post-metadata">

**Author:** ![hane\_vv](https://avatars.discourse-cdn.com/v4/letter/h/e8c25b/32.png) [@hane\_vv](https://forums.freertos.org/u/hane_vv)\
**Post date:** [March 28, 2024, 2:22am UTC](https://forums.freertos.org/t/when-tcp-only-receives-data-and-does-not-send-data-the-freertos-recv-function-cannot-return-for-a-long-time/19655/2 "2024-03-28T02:22:50Z")

</div>

It should be that ipconfigTCP\_TX-BUFFER-LENGTH and ipconfigTCP\_RX-BUFFER-LENGTH are set incorrectly. Both of them are 1024 less than ipconfigTCP MSS (1500- ipSIZE-OF-IPv4\_HEADER - ipSIZE-OF-TCP\_HEADER). I now set ipconfigTCP MSS to 536, and set ipconfigTCP\_TX-BUFFER-LENGTH and ipconfigTCP\_RX-BUFFER-LENGTH to (2 \* ipconfigTCP MSS), which can work normally when only receiving but not sending.  
Will the connection socket created by TCP Server inherit the size of the receiving and sending buffer of the listening socket?

---

<div class="post-metadata">

**Author:** ![tony-josi-aws](https://avatars.discourse-cdn.com/v4/letter/t/a6a055/32.png) [@tony-josi-aws](https://forums.freertos.org/u/tony-josi-aws)\
**Post date:** [March 28, 2024, 4:53am UTC](https://forums.freertos.org/t/when-tcp-only-receives-data-and-does-not-send-data-the-freertos-recv-function-cannot-return-for-a-long-time/19655/3 "2024-03-28T04:53:56Z")

</div>

> Will the connection socket created by TCP Server inherit the size of the receiving and sending buffer of the listening socket?

Yes, the newly connected socket will inherit the buffer sizes of the listening socket among many other things (refer: `prvTCPSocketCopy`)

Could you share the priorities of tasks in your system? Is IP task running at a higher priority than application tasks?

---

<div class="post-metadata">

**Author:** ![hane\_vv](https://avatars.discourse-cdn.com/v4/letter/h/e8c25b/32.png) [@hane\_vv](https://forums.freertos.org/u/hane_vv)\
**Post date:** [March 28, 2024, 1:25pm UTC](https://forums.freertos.org/t/when-tcp-only-receives-data-and-does-not-send-data-the-freertos-recv-function-cannot-return-for-a-long-time/19655/4 "2024-03-28T13:25:12Z")

</div>

In my system, all tasks have the same priority and there is no time slice rotation scheduling enabled. Each task will abandon CPU usage through blocking events, and no task will occupy CPU for a long time

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [March 28, 2024, 3:07pm UTC](https://forums.freertos.org/t/when-tcp-only-receives-data-and-does-not-send-data-the-freertos-recv-function-cannot-return-for-a-long-time/19655/5 "2024-03-28T15:07:59Z")

</div>

> [@hane\_vv](#):
>
> all tasks have the same priority and there is no time slice rotation scheduling enabled.

As @tony-josi-aws asked, can you try running IP task at a higher priority than other tasks?

> [@hane\_vv](#):
>
> which can work normally when only receiving but not sending.

What behavior do you observe when the device is only sending data? Is the application at the other end receiving data? Can you capture network traffic?

---

<div class="post-metadata">

**Author:** ![hane\_vv](https://avatars.discourse-cdn.com/v4/letter/h/e8c25b/32.png) [@hane\_vv](https://forums.freertos.org/u/hane_vv)\
**Post date:** [March 29, 2024, 1:00am UTC](https://forums.freertos.org/t/when-tcp-only-receives-data-and-does-not-send-data-the-freertos-recv-function-cannot-return-for-a-long-time/19655/6 "2024-03-29T01:00:13Z")

</div>

I think I have solved this problem, which is ipconfigTCP\_TX-BUFFER-LENGTH and ipconfigTCP\_RX-BUFFER-LENGTH are set incorrectly. Both of them are 1024 less than ipconfigTCP MSS (1500 - ipSIZE OF IPv4\_HEADER - ipSIZE OF TCP HEADER)，

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [March 29, 2024, 5:31am UTC](https://forums.freertos.org/t/when-tcp-only-receives-data-and-does-not-send-data-the-freertos-recv-function-cannot-return-for-a-long-time/19655/7 "2024-03-29T05:31:42Z")

</div>

Thank you for sharing your solution!
