Changing server_port of Ethernet server(server_port) on the fly, is it possible?

In the declaration EthernetServer server(80); we use port 80.

But if I use EthernetServer server(server_port);, then I can change server_port for any int value I want.

But, how can I change this value on the fly?

The idea is to change the value with an html web page then reboot the board to acces with the new port.

But I don't know how to do it because the EthernetServer server(server_port); is BEFORE the setup() function.

So is there a way to change the port value of declaration EthernetServer server(XXXX); on the fly?

Starting two servers is not an option?

I don't understand your answer, could you precise?

What I want in fact is the ability to change the port of my application because if the port is already taken by another device on the network, I would like to have the ability to change it in case of conflict.

I would like to have the ability to change it in case of conflict.

How would you know there was a conflict, and if there is a conflict, how will the change command get to the arduino?

This is for port forwarding from outside my internal network.

For know I have
port 80=> to my webserver
port 81=> to my Uno+Eth

I want to set port 90 for my new application with mega+eth

But, if for some reason I would like to change the port, I will have to reupload the code in the board.

If I can change this with my http interface it is more friendly.

Two :90 ports in local network 192.168.1.xx:90 for some minutes is not disturbing because acces is granted.

But from outside the local network, if 2 devices have the same port I'm bloked.

I think I found a solution by allocate in EEPROM and reboot the board, so the new port is read before starting.

john_lenfr:
I don't understand your answer, could you precise?

What I want in fact is the ability to change the port of my application because if the port is already taken by another device on the network, I would like to have the ability to change it in case of conflict.

What difference would it make if another IP has the same port in use? That would not be a conflict unless both devices had the same IP.

But from outside the local network, if 2 devices have the same port I'm bloked.

No. Only if you mess up the port forwarding in your router. You can port forward different ports on your public IP to different IPs with the same port on the localnet. At least I can with my router.

xx.xx.xx.xx:80 -> 192.168.0.2:80
xx.xx.xx.xx:8080 -> 192.168.0.3:80

SurferTim:
What difference would it make if another IP has the same port in use? That would not be a conflict unless both devices had the same IP.
No. Only if you mess up the port forwarding in your router. You can port forward different ports on your public IP to different IPs with the same port on the localnet. At least I can with my router.

xx.xx.xx.xx:80 -> 192.168.0.2:80
xx.xx.xx.xx:8080 -> 192.168.0.3:80

No, you can't do that because from the outside you can only make the difference when calling by the port.

Let's say your public IP adress is 345.345.345.345 (for the example ok, I know 345.345.345.345 does'nt exist)

If you redirect port 80 onto 2 different IP adresses:

what is going to happenend when you type http://345.345.345.345 ?
Is your web server on 192.168.0.2 or 192.168.0.3 executed?

The only way to do for me is to take 2 different PORT to make the difference from the outside:
If you have:

Public IP: 345.345.345.345
Internal IP1 : 192.168.0.1:80
Internal IP2: 192.168.0.2:90

So from the outside you could call:
345.345.345.345:80 will be redirected to 192.168.0.1 device
345.345.345.345:91 will be redirected to 192.168.0.2 device

So you could have your web server on 192.168.0.1:80 and an Arduino on 192.168.0.2:90

Is that correct?

So you could have your web server on 192.168.0.1:80 and an Arduino on 192.168.0.2:90

In your router you probably can set a range of ports for the LAN IP address assigned to the arduino.

john_lenfr:
The only way to do for me is to take 2 different PORT to make the difference from the outside:
If you have:

Public IP: 345.345.345.345
Internal IP1 : 192.168.0.1:80
Internal IP2: 192.168.0.2:90

So from the outside you could call:
345.345.345.345:80 will be redirected to 192.168.0.1 device
345.345.345.345:91 will be redirected to 192.168.0.2 device

So you could have your web server on 192.168.0.1:80 and an Arduino on 192.168.0.2:90

Is that correct?

Partly. You can also use port 80 on both the 192.168.0.1 and 192.168.0.2 devices.

345.345.345.345:80 will be redirected to 192.168.0.1:80
345.345.345.345:91 will be redirected to 192.168.0.2:80

Take look here for an example. This is a Netgear router. Look at the second pic that has "Port Forwarding / Port Triggering"on that page and the instructions below it. The start port is 91, and the end port is 80 91.

edit: This is the rest.. You must deselect "use same port for internal". Then enter 80 for start and 80 for end.

Attached is a pix of my netgear 614 port fowarding.

Thanks, zoomkat. He's stuck using port 91 on the ethernet shield. That shows me two things.

  1. Netgear routers can't do that.
  2. My Routerboard/MikroTik routers can.

edit: But this does not explain why the OP wants to change the port "on the fly". With a primitive router like the Netgear, it certainly wouldn't have any fallthrough capability.

  1. Netgear routers can't do that.

Can you give a little more detail on the "that" in the above statement? The discussion is some what rambling between router configuration and changing an arduino server port on the fly.

That wasn't very clear. The netgear does not do a full DNAT. It won't change the port according to your pic. It can port forward from the public IP port 91 to the localnet port 91, but can't change the port.

My routers can also DNAT the port. I can change the public IP port 91 to a localnet IP port 80. It may be that some Netgear routers can do that also, just not the one you have.

My question to the OP is why the need to change the port on the fly? What is gained by that? Can the OP change the router's DNAT on the fly?

edit: I played with the server code a bit and got it to change ports on the fly if that will help you. I used ports 80 and 8080 as a test. You can change that to anything you like.

// add this to includes
#include <utility/socket.h>

// in global variable area
EthernetServer server1(80);
EthernetServer server2(8080);
// set server to server1
EthernetServer server = server1;

// in setup
 server.begin();

// to change ports
      // change server to server2
      server = server2;
      // close all sockets
      for(byte i = 0 ;i < MAX_SOCK_NUM;i++) {
        close(i);
      }
      // start server2
      server.begin();

Hello guys,

Problem solved.
I forgot to soft reset the board after changing the value of the port in EEPROM.

Now all is good.

The purpose is that the user of the web UI can now change the port without programming the Arduino to set his port in his network if needed.

But thanks for the code SurferTim, will test it soon.