lwIP reconnect same address+port

f.h-f.s.
Posts: 215
Joined: Thu Dec 08, 2016 2:53 pm

lwIP reconnect same address+port

Postby f.h-f.s. » Tue Mar 28, 2017 9:30 am

Hello all,

I've been looking for a way to reconnect my ssl connection.
I can set-up my connection and close it, but then I have to wait a certain amount of time before binding/connecting on the same address+port according to:
Only after some time (minutes) the resources are completely cleared in the underlying stack due to the need to follow the TCP specification and go through the TCP timewait state.
http://lwip.wikia.com/wiki/Netconn_bind

I would like to know if there is another way to clear the resources in said stack. Or if there is another way to reconnect besides rebooting/waiting/keeping the connection alive.

heyinling
Posts: 19
Joined: Thu Mar 23, 2017 7:21 am

Re: lwIP reconnect same address+port

Postby heyinling » Tue Mar 28, 2017 12:37 pm

Hi,

The reason why we need to wait around 2 minutes is because TCP has time wait state. remote TCP peer could re-send FIN-ACK if last ACK lost. If we're reusing same port immediately, the new connection will receive un-excepted packets and could lead to problem.

As you're using ssl client, I think it's better to use random local port by passing 0 as port number.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: lwIP reconnect same address+port

Postby kolban » Tue Mar 28, 2017 1:59 pm

You might want to look into the SO_REUSEADDR option. Here is a Microsoft article ...

https://msdn.microsoft.com/en-us/librar ... s.85).aspx

By default, this feature is disabled in ESP-IDF but can be enabled in the "make menuconfig" options.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

f.h-f.s.
Posts: 215
Joined: Thu Dec 08, 2016 2:53 pm

Re: lwIP reconnect same address+port

Postby f.h-f.s. » Tue Mar 28, 2017 3:10 pm

heyinling wrote:As you're using ssl client, I think it's better to use random local port by passing 0 as port number.
Aah right, I had set up the bind to: bind to the same local port. I've now changed it to zero and its working fine. Thanks.
kolban wrote:You might want to look into the SO_REUSEADDR option.
Even with SO_REUSEADDR you will get ERR_USE when trying to bind a socket to the port you were using in the previous connection.

Who is online

Users browsing this forum: theodote and 76 guests