# Clients and servers cannot run simultaneously

**URL:** <https://forums.freertos.org/t/clients-and-servers-cannot-run-simultaneously/7731>\
**Category:** Kernel\
**Created:** [March 16, 2019, 8:30pm UTC](https://forums.freertos.org/t/clients-and-servers-cannot-run-simultaneously/7731 "2019-03-16T20:30:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 16, 2019, 8:30pm UTC](https://forums.freertos.org/t/clients-and-servers-cannot-run-simultaneously/7731/1 "2019-03-16T20:30:11Z")

</div>

**[giomaca](https://sourceforge.net/u/giomaca/)** wrote on [Saturday, March 16, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/2de1d1175b/#9068):

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.

Regards,  
Giovanni.

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [March 17, 2019, 1:54am UTC](https://forums.freertos.org/t/clients-and-servers-cannot-run-simultaneously/7731/2 "2019-03-17T01:54:47Z")

</div>

**[heinbali01](https://sourceforge.net/u/heinbali01/)** wrote on [Sunday, March 17, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/2de1d1175b/#1c69):

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:

```auto
/* 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.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 19, 2019, 1:11am UTC](https://forums.freertos.org/t/clients-and-servers-cannot-run-simultaneously/7731/3 "2019-03-19T01:11:04Z")

</div>

**[giomaca](https://sourceforge.net/u/giomaca/)** wrote on [Tuesday, March 19, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/2de1d1175b/#58d6):

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 `vApplicationMallocFailedHook`fires.

Can you help me to solve this problem?

Thanks in advance,  
Giovanni

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 19, 2019, 1:48am UTC](https://forums.freertos.org/t/clients-and-servers-cannot-run-simultaneously/7731/4 "2019-03-19T01:48:49Z")

</div>

**[richarddamon](https://sourceforge.net/u/richarddamon/)** wrote on [Tuesday, March 19, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/2de1d1175b/#b198):

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).

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [March 19, 2019, 1:54am UTC](https://forums.freertos.org/t/clients-and-servers-cannot-run-simultaneously/7731/5 "2019-03-19T01:54:10Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Tuesday, March 19, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/2de1d1175b/#58d6/d12e):

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.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 21, 2019, 10:17am UTC](https://forums.freertos.org/t/clients-and-servers-cannot-run-simultaneously/7731/6 "2019-03-21T10:17:13Z")

</div>

**[giomaca](https://sourceforge.net/u/giomaca/)** wrote on [Thursday, March 21, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/2de1d1175b/#c6b5):

Thanks a lot for your help.

I resolve the problem reducing the size of buffers.

Now client and server can run together.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 21, 2019, 10:17am UTC](https://forums.freertos.org/t/clients-and-servers-cannot-run-simultaneously/7731/7 "2019-03-21T10:17:19Z")

</div>

**[giomaca](https://sourceforge.net/u/giomaca/)** wrote on [Thursday, March 21, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/2de1d1175b/#2fab):

Thanks a lot for your help.

I resolve the problem reducing the size of buffers.

Now client and server can run together.
