-->
Page 1 of 5

Changing server listening port on the fly

PostPosted: Thu Mar 30, 2017 4:51 pm
by JimDrew
Is it possible to ever change a WiFiServer listening port after the ESP8266 has started up?

Re: Changing server listening port on the fly

PostPosted: Thu Mar 30, 2017 4:59 pm
by martinayotte
Although I've never tried it, I think it is possible.
You would have to add a new setPort() method, and then using code like this one :
Code: Select alltcpserver.close();
tcpserver.setPort(newport);
tcpserver.begin();


EDIT : If you don't wish to add new setPort() method, it can still be accomplish by using two instances of WiFiServer, one with old port and the other with new port and do the switch like :

Code: Select alltcpserver1.close();
tcpserver2.begin();

Re: Changing server listening port on the fly

PostPosted: Thu Mar 30, 2017 7:59 pm
by JimDrew
These don't work because WiFiServer has to be defined *before* the Setup() routine. So, whatever name you use for the server throughout your app must be the same. I have tried various things like stopping the current server, creating a new one and then assigning the new one to the old name. Nothing seems to work.

Re: Changing server listening port on the fly

PostPosted: Fri Mar 31, 2017 7:34 am
by martinayotte
I know that those 2 instances need to be global variable and declared outside setup().
Thus doesn't prevent the code provided to be able to compile.
Did you tried it out ?