Test TCP Server - only 4 client connections before stack dies

31 Jul 2012

Does any one know why the current tcp stack only handles 4 successive client connections before failing ?

Running this program, and connecting to the mbed via "telnet 192.168.10.231 80" work 4 times, and on the fifth attempt, just hangs indefinitely

Import programtest_tcp_server

Test successive tcp connections

TIA for any help on this.

31 Jul 2012

Hi Neal, thank you for your experiments on the new networking stack

Neal Horman wrote:

Does any one know why the current tcp stack only handles 4 successive client connections before failing ?

Official Example

The mbed official TCP Server example does not have this problem, you can use it as a reference:

Import programTCPEchoServer

Simple TCP Echo Server

Socket API

In your example you are using a blocking call (default) to "send_all", if anything goes wrong in the connection with your client, your server will remain stuck there without any chance to recover:

client.send_all(str, sizeof(str)-1);

Try to change that with something like a 3 seconds timeout:

client.send_all(str, sizeof(str)-1, 3000);

RTOS

Another thing you can do to increase the reliability of your program is to move the most stack intensive code to main.

As you can see from the RTOS Memory Model documentation, the main stack has room for growth in the whole free RAM memory. Any other thread has a fixed size stack that cannot grow at run time.

Remember that RAM in a microcontroller is a very scarce resource. Each RTOS thread has a relevant cost in terms of RAM for its stack. You want to avoid having a thread staying idle:

// Avoid code like this in a thread:
while(1) {
    Thread::yield();
}

In your example, you can reduce the total number of threads by one (for example, move the ThreadProcSocket to main).

HTH, Emilio

31 Jul 2012

Hi Neal, actually getting the blocking behaviour keeping the "timeout=0" default did look a bit counter-intuitive.

I have changed that to a "set_blocking(bool blocking, unsigned int timeout=1500)" call, therefore you can use in your server:

client.set_blocking(false, 3000);
[...]
client.send_all(str, sizeof(str)-1);

Cheers, Emilio