Clients and servers cannot run simultaneously

giomaca wrote on Saturday, March 16, 2019:

Hello Everyone,
I’m trying to develop an application where run an http web-server and an http client.

I have one thread for the web-server and the other thread for the client.

When the two threads run independently all works fine, but when I enable both of them the server works fine untill the client makes a connection.
I have used different ports for both the sockets: port 80 for http server socket and 8080 for http client socket.

During normal operatation the http client has to send an amount of collected data to a server, meanwhile the web-server could be able to accept a connection from a client.

At the moment, when the http client makes a connection the web-server remains indefenitely in the listen state, evev if a clinet tries to connect.

Can someone help me to resolve this problem?

I can’t understand this behavior.


heinbali01 wrote on Sunday, March 17, 2019:

Hi Giovanni, when you run either one of the tasks, there is no problem. When the tasks run simultaneously, things go wrong as soon as the client gets connected.

It makes me think of the shared resources: memory, network buffers, CPU-time.

Please make sure that pxGetNetworkBufferWithDescriptor() never fails. When it returns NULL, the macro iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER() will be called. You can define it in your FreeRTOSIPConfig.h

Also pvPortMalloc() should never fail. You can define this hook:

/* Please define in your FreeRTOSConfig.h */
#define configUSE_MALLOC_FAILED_HOOK   1

/* And add this function to your code */
void vApplicationMallocFailedHook( void )
	/* Do whatever to draw attention, but do not call pvPortMalloc(). */

CPU-time: when a task forgets to block (sleep), it may lock-up the CPU, and other tasks can not function normally.

If you want you can zip and attach some client and server code to your post.

giomaca wrote on Tuesday, March 19, 2019:

Hi Hein,
thanks a lot for the reply.

Following your advice, I found that there is a problem with the pvPortMalloc() function.
After I have defined the hook
#define configUSE_MALLOC_FAILED_HOOK 1
if the client is running, each time that i try to send a message to the web-server the function pvPortMalloc() fails and the function vApplicationMallocFailedHookfires.

Can you help me to solve this problem?

Thanks in advance,

richarddamon wrote on Tuesday, March 19, 2019:

If you end up in vApplicationMallocFailedHook, then you do not have enough heap defined in your system. If you have some more spare memory, you can make the heap bigger. If not, you need to reduce the amount of memory you are using (perhaps fewer buffers).

rtel wrote on Tuesday, March 19, 2019:

Do you have enough memory available to increase your heap size? Which
memory allocater are you using? If heap_2, heap_4 or heap_5 then keep
increasing configTOTAL_HEAP_SIZE in FreeRTOSConfig.h until your
application no longer links - then dial it back a bit. That should
ensure you are using all the heap. Also, if your application never
calls malloc() (rather then pvPortMalloc()), then ensure the heap
allocated by your compiler/linker is tiny so as not to waste RAM.

giomaca wrote on Thursday, March 21, 2019:

Thanks a lot for your help.

I resolve the problem reducing the size of buffers.

Now client and server can run together.

giomaca wrote on Thursday, March 21, 2019:

Thanks a lot for your help.

I resolve the problem reducing the size of buffers.

Now client and server can run together.