2 years, 2 months ago.

mbed socket connection takes a long time

Hi Guys,

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

code

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

Comment on this question

1 Answer

2 years, 2 months ago.

Assuming you are using mbed OS, set the socket to blocking and set the timeout to whatever you think is appropriate. No modifications are necessary. The method syntax can be seen here.

However, I am having trouble reproducing your issue. Can you use a debugger and confirm if the application is hanging at this line - https://github.com/ARMmbed/mbed-os/blob/533e6f0febb5ea582a61f0c8aaf686c70333524f/features/netsocket/TCPSocket.cpp#L57?

Hello Sarah :) I use different library - I just imported EthernetInteface and there I found Socket and others files, but the copyright is from 2012 and source code looks differently. Is it any other library, which I should download? Maybe it is some kind of "not smart" question, but I'm very new in mbed programming :)

You're asking about TCPSocket.cpp, but in EthernetInterface that file even doesn't exist, so I suppose it should be something else. Could you say me what I should look for? I uploaded the most fresh EthernetInterface, but it is still the same.

After editing: I use Socket from: https://developer.mbed.org/handbook/Socket and it is actually very analogic. I tried to set it to blocking=true, timeout=200 and many other combinations, but still nothing :/

posted by Antonina Kirkało 27 Jul 2017

Antonia,

The latest EthernetInterface library is included in mbed OS. Would you be willing to try using mbed OS? It sounds like you might be using the mbed 2 library. The example shown here - https://docs.mbed.com/docs/mbed-os-api-reference/en/latest/APIs/communication/ethernet/ would be a good jumping off point.

posted by Sarah Marsh 27 Jul 2017

I need to say that the last comment was the answer :) I didn't know that I need to "refresh" mbed OS to new version. I got this program schema from somebody else and I figure out that it used old version of libraries ;( And mostly it is not a huge issue...but in socket connection it is. Without fresh liberaries we have no control on connection time of the socket.

Thank you Sarah! :)

posted by Antonina Kirkało 25 Aug 2017