Search code examples
c++socketstimeoutmbed

mbed socket connection takes a long time


I'm fighting with my Mbed controller and it's winning :/ Part of my code below:

eth.connect();
pc.printf("ethernet OK");
sock.set_blocking(false,200);
pc.printf(" socket setting OK ");
sock.connect("192.168.1.100", 80);
pc.printf("GO! GO! GO!");
conn = sock.is_connected();

When the server is on I can connect with socket and there is no problem. But when I shut my server down, my program wait in sock.connect(...) line for a long time and then it moves forward. I want it to move forward, but I don't want to wait such a long time :/

I found that I should change socket setting from blocking to non-blocking, and timeout from default=1500 to less. I changed it everywhere I could:
- Part of my socket.h library:
void set_blocking(bool blocking, unsigned int timeout=200);
- Part of my socket.cpp library:
Socket::Socket() : _sock_fd(-1), _blocking(false), _timeout(200)
And still nothing. It wait about 17-18sec, but I would like to change it to max. 1-2sec.

I would be glad, if you give me an any tip :)


Solution

  • Firstly I need to say, that TCP_SYNMAXRTX changing is something, what can really help, it helped me, but not at all. In my case the important was to change mbed OS to the latest version. In previous there was libraries with socket settings, but actually they work properly since mbed OS contains EthernetInterface and others libraries (you shouldn't import it as external library).

    Thank you for your time and goodwill! :)