FreeRTOS Demo - Problem with Creating TCP Socket

I have a problem with creating “FreeRTOS_socket”. I download FreeRTOS Source codes and I open project in the path below.

Here is my main method:

int main(void)

Project Successfully build. Also, FreeRTOS_IPInit() return 1 (pdTRUE). Then, I use simple TCP socket code in my main scope. I set my ucIPAddress, ucNetMask, ucGatewayAddress, ucDNSServerAddress, ucMACAddres values correctly.

FreeRTOS_debug_printf( ( "FreeRTOS_IPInit\n" ) );
FreeRTOS_IPInit( ucIPAddress, ucNetMask, ucGatewayAddress, ucDNSServerAddress, ucMACAddress );
xSocket_t xSocket;

/* Create the socket. */

/* Check the socket was created successfully. */
		xBindAddress.sin_port = FreeRTOS_htons(9999);
		if (FreeRTOS_bind(xSocket, &xBindAddress, sizeof(&xBindAddress)) == 0)
			/* The bind was successful. */
	//Socket is not created.
       //There was insufficient FreeRTOS heap memory available for the socket
       // to be created. 


Official TCP Socket creation code says heap memory is not available if it is going in else. Then I increased heap memory in FreeRTOSConfig.h but still is not creating socket.

I know I can use winsocket properly but I want to use FreeRTOS_Socket.

Could you please notify me about what is going wrong with my approach?

Thanks and Regards,


[edited for code formatting and moved to Libraries category]

Which memory allocation scheme are you using?
Which TCP buffer allocation scheme are you using?

Have you waited for the IP stack initialisation to complete before trying to create a socket? The code in your post does not do that, but it might just be representative code.

When you step into the function to the point where memory is being allocated, and in particular where the memory allocation fails, what is the size of the memory being requested? If it is too large it might be configuration file problem.

1 Like

While you answer Richard’s questions, could you can also attach the file in which main() is declared?

Note that in a FreeRTOS project, the function main() normally ends with a call to vTaskStartScheduler(). After that, the IP-task will be running and it can provide the TCP/IP services.

Local variables declared in the main() function should not be accessed once the scheduler has started. See declaration xSocket_t xSocket.

1 Like

Dear Richard, thanks for your quick reply.

Question Scope 1
Yes I waited for IP stack initialisation to complete and it returns 1 (pdTRUE). But, I realized that although it was successful, it did not print the correct value
Question Scope 1

Question Scope 2

Firstly, I am curious about is there any spesifical configuration after I download official FreeRTOS files and using Demos in Visual Studio. In project that I download (attached below) uses BufferAllocation_2.c and heap_4.c when I build the project. Should I do a spesifical configuration for including “mem-alloc-scheme” or “buf-alloc-scheme” in my project build process?

Here is my build process log:

  1. ------ Rebuild All started: Project: RTOSDemo, Configuration: Debug Win32 ------

  2. cl : Command line warning D9035: option ‘Gm’ has been deprecated and will be removed in a future release

  3. main.c

  4. c:\users\AshinesPC\downloads\freertosv10.3.1_winsoc\freertosv10.3.1\freertosv10.3.1\freertos-plus\demo\freertos_plus_tcp_minimal_windows_simulator\main.c(171): warning C4996: ‘inet_addr’: Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings

  5. c:\program files (x86)\windows kits\10\include\10.0.17763.0\um\winsock2.h(1831): note: see declaration of ‘inet_addr’

  6. c:\users\AshinesPC\downloads\freertosv10.3.1_winsoc\freertosv10.3.1\freertosv10.3.1\freertos-plus\demo\freertos_plus_tcp_minimal_windows_simulator\main.c(181): warning C4189: ‘a’: local variable is initialized but not referenced

  7. demo_logging.c

  8. SimpleUDPClientAndServer.c

  9. TCPEchoClient_SingleTasks.c

  10. SimpleTCPEchoServer.c

  11. NetworkInterface.c

  12. BufferAllocation_2.c

  13. FreeRTOS_UDP_IP.c

  14. FreeRTOS_TCP_WIN.c

  15. FreeRTOS_TCP_IP.c

  16. FreeRTOS_Stream_Buffer.c

  17. FreeRTOS_Sockets.c

  18. FreeRTOS_IP.c

  19. FreeRTOS_DNS.c

  20. FreeRTOS_DHCP.c

  21. FreeRTOS_ARP.c

  22. timers.c

  23. tasks.c

  24. queue.c

  25. port.c

  26. Generating Code…

  27. Compiling…

  28. heap_4.c

  29. list.c

  30. event_groups.c

  31. Generating Code…

  32. event_groups.obj : warning LNK4075: ignoring ‘/EDITANDCONTINUE’ due to ‘/SAFESEH’ specification

  33. WIN32.vcxproj -> C:\Users\AshinesPC\Downloads\FreeRTOSv10.3.1_WinSoc\FreeRTOSv10.3.1\FreeRTOSv10.3.1\FreeRTOS-Plus\Demo\FreeRTOS_Plus_TCP_Minimal_Windows_Simulator.\Debug\RTOSDemo.exe

  34. Done building project “WIN32.vcxproj”.

  35. ========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

Question Scope 2

Question Scope 3

The problem that I understand from the debug process is : There is not a failure in memory allocation. FreeRTOS_IPInit() complete succesfully and return “pdTRUE” but it is not setting xIPTaskInitialised = true because in the code line that linked below.
xTaskCreate_of_FreeRTOS_IPInit() (Line 1014)

1014 - xReturn = xTaskCreate( prvIPTask, "IP-task", ( uint16_t)ipconfigIP_TASK_STACK_SIZE_WORDS, NULL, (UBaseType_t)ipconfigIP_TASK_PRIORITY, &xIPTaskHandle );

Task Creation success but it is not setting up “xIPTaskInitialised = pdTrue” so in the socket side build process exiting in #this_line demo shown in below. It originates from inner of specified line xIPIsNetworkTaskReady is returning pdFALSE

270 . Socket_t FreeRTOS_socket( BaseType_t xDomain, BaseType_t xType, BaseType_t xProtocol){
272. FreeRTOS_Socket_t *pxSocket;
273. size_t uxSocketSize;
274. EventGroupHandle_t xEventGroup;
275. Socket_t xReturn;
277. (#this_line) if( prvDetermineSocketSize( xDomain, xType, xProtocol, &uxSocketSize ) == pdFAIL)
278. {
280. }

Demo is exist in FreeRTOS Official Demo named by TCP Minimal Windows Simulator Demo

Question Scope 3

Of course, I attached project link here (Official TCP Minimal Demo Project ).

I did not know that information thanks for the feedback. I will try to take a move from here.

My main code :

int main(void)
const uint32_t ulLongTime_ms = pdMS_TO_TICKS(1000UL);

//Instructions for using this project are provided on:


FreeRTOS_IPInit(ucIPAddress, ucNetMask, ucGatewayAddress, ucDNSServerAddress, ucMACAddress);
xSocketSet_t xSocket;

struct freertos_sockaddr xBindAddress;

  xBindAddress.sin_port = FreeRTOS_htons(9999);
  if (FreeRTOS_bind(xSocket, &xBindAddress, sizeof(&xBindAddress)) == 0)
  	//The bind was successful.

//There was insufficient FreeRTOS heap memory available for the socket
to be created.


for (;; )

Shortly, FreeRTOS IPInit() successful, but there is a problem as stated hierarchically below.

main.c :

FreeRTOS_socket( BaseType_t xDomain, BaseType_t xType, BaseType_t xProtocol )

FreeRTOS_Sockets.c :

->prvDetermineSocketSize( xDomain, xType, xProtocol, &uxSocketSize ) == pdFAIL
->->xIPIsNetworkTaskReady() == pdFAIL
->->->BaseType_t xIPIsNetworkTaskReady( void )
->->-> return xIPTaskInitialised;

xIPTaskInitialised is firing and setting pdTRUE in below

FreeRTOS_IP.c :
xReturn = xTaskCreate( prvIPTask, “IP-task”, ( uint16_t )ipconfigIP_TASK_STACK_SIZE_WORDS, NULL, ( UBaseType_t )ipconfigIP_TASK_PRIORITY, &xIPTaskHandle );

prvIPTask is created successfully and return pdTRUE but why socket creation is failing .

I check your answer before related topic below :

You said “basically network is not ready” I configure all of the requirements. What do you think about what are the missing matters when we look to network ready requirements?

Can you please attach the code that you wrote to initialise the stack and that calls FreeRTOS_socket() to create a socket.

All I see in this post is a function main() in which FreeRTOS_socket()is called before the scheduler is running.

1 Like

Dear Hein Tibosch,

Firstly, I think that issue about heap memory and I did not focus to TaskScheduling part. But after your feedback, I gone into the issue of scheduling and I learned FreeRTOS concepts. Now issue is successfully solved. Grateful for your help.

Thanks and Regards.