ESP32 WiFiUDP Udp; setting remote-port 4210 it works but the shown portnumber is different 51206

Hi everybody,

I'm developping a ESP32-project where the ESP32 is broadcasting UDP-messages in a certain mode.

With setting up the IP-adress as broadcasting to the local network
X.X.X.255
and as remoteport 4210

int remotePort = 4210;      
...
    Udp.beginPacket(broadcastIP, remotePort);
    Udp.write((const uint8_t*)UDP_Msg_SS.c_str(), UDP_Msg_SS.length() );  
    Udp.endPacket();             

I can receive the UDP-messages on my PC with a python script that uses the same port

udp_host = socket.gethostname()		        # Host IP
udp_port = 4210			                # specified port to connect
print("udp_host",udp_host)
sock.bind((udp_host,udp_port))
print ("Waiting for client...")

and I can receive the messages on my smartphone with an UDP-receiver-App

but what I find weird is that the if I print the port

  # python-script
  data,addr = sock.recvfrom(1024)	        #receive data from client
  print("data #",data,"#")
  Msg = data.decode('utf-8')
  print ("Received Message: #",Msg,"# from",addr)

there is a completely different portnumber
I defined 4210 and what is printed is 51206 !

Received Message: # ESP-IP=192.168.178.186 # from ('192.168.178.186', 51206)

and it is the same on the smartphone
In the settings I enter port 4210

I receive the messages but the port is this 51206
on smartphone
[192.168.178.186:51206]"ESP-IP=192.168.178"

Huh? can somebody explain this to me?

remote port is 4210.
esp32 uses port 51206 as local port to send the message

I find this very confusing
There is the function-call

    Udp.beginPacket(remoteIP, remotePort);
    Udp.write((const uint8_t*)UDP_Msg_SS.c_str(), UDP_Msg_SS.length() );  

Which I thought is the function to send
and in case of sending it is interesting what port I have to specify to send the message to
because the receiver (receiver=the remote-side) is checking the incoming data for coming from a specific port

But it seems to be different.
Is there a variant for the function-call

   Udp.beginPacket(remoteIP, remotePort);

where I can specify the "local port to send the message" ?

would this be a third parameter

   Udp.beginPacket(remoteIP, remotePort, localPort);

?

best regards Stefan

And why the heck is this "local port-number changing if I change the variable-type of variable remotePort?

unsigned int remotePort = 4210;                 // receiver port to listen on must match the portnumber the receiver is listening to
//int remotePort = 4210;                 

then the python script and the smartphone app both report port 53394

why does changing the variable-type change "the local port to send the message" ????
this is really weird

edit

I pressed the reset-button on the ESP32-board and let the board start new.
the port reported from the receivers (PC-python-script and Smartphone)
are the same but seem to randomly change from reset to reset

I found this thread in the esprerssiv forum

which indeed says

"the port from which the esp32 can send through UDP"
this is the port of the requested client then - name it "LOCALPORT"
this port is usually a randomly free thing

never thought this could be.
Can anybody explain to me why this localport is set randomly?
What might be the advantage to have it set randomly???

local port is random and it is irrelevant

there are differences between the implementations of the UDP class in different Arduino networking libraries.

the Arduino Ethernet library requires udp.begin(localPort) and starts listening at this port (UDP server) and will use the same port to send a message. it is not usual in the TCP/IP networking world to listen and send on the same port. this is not right because it always starts the UDP listener even if you want only send messages. all networking libraries by Arduino have it as the Ethernet library.

I didn't experiment with UDP on esp8266 or esp32 so I don't know how it behaves. both use the standard TCP/IP LwIP stack so on low level they behave 'normal' (use a random port for local port). I don't know what will the result be if you use udp.begin(localPort).

btw endPacket sends the message

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.