Page 1 of 1

UDP Client example : where to get ESP32 UDP port

Posted: Sat Sep 11, 2021 9:28 am
by shirogeek
Hey guys,

I am playing around with the udp_client example. I can easily receive and send packets to the ESP32. However, I would like to know if there is a systematic way to know the UDP port of the ESP32. I currently can figure it out by looking at the source UDP port of the message the UDP sends and send back packets to it with success. Is that the only way ? Does the ESP negotiate what port it gets with the AP or is there a way to fix it ?

Thanks

Re: UDP Client example : where to get ESP32 UDP port

Posted: Sat Sep 11, 2021 9:58 am
by WiFive

Re: UDP Client example : where to get ESP32 UDP port

Posted: Sat Sep 11, 2021 12:10 pm
by rudi ;-)
shirogeek wrote:
Sat Sep 11, 2021 9:28 am
I would like to know if there is a systematic way to know the UDP port of the ESP32.
Hi
like WiFive linked to the example - you find the "CONFIG_EXAMPLE_UDP_PORT" variable there - it is from the menuconfig
you can set the port in the menuconfig - the variable "EXAMPLE_UDP_PORT" is then the "CONFIG_EXAMPLE_UDP_PORT" in the ESP_LOGI which you read.

You can set the Port on this systematic way in the Menuconfig -
or in your code too:

The Bind is done there - and or here, , depended on your "sa_family_t" , so you can change the Port there on the Bind procedure.

Hope this helps.

best wishes
rudi ;-)

Re: UDP Client example : where to get ESP32 UDP port

Posted: Fri Oct 15, 2021 11:02 am
by shirogeek
Thanks for your reply guys.
I actually meant the port from which the ESP32 can send messages through UDP. The port you guys are mentioning is the port of the UDP server (on my computer for instance). I'm actually looking for the port I can combine with the IP address of the ESP32 to send messages from my computer to the ESP32 and this doesn't seem to be the one in the config.
From my tests, it seems it is randomly generated. I wait for the ESP32 to send me a packet and there in the packet i can see esp32 IP address and incoming port. I can use then that incoming port to reply to the esp32 but it's not systemic.
I hope my question is clearer now.

Re: UDP Client example : where to get ESP32 UDP port

Posted: Fri Oct 15, 2021 5:17 pm
by rudi ;-)
shirogeek wrote:
Fri Oct 15, 2021 11:02 am
I actually meant the port from which the ESP32 can send messages through UDP.
"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
shirogeek wrote:
Fri Oct 15, 2021 11:02 am
The port you guys are mentioning is the port of the UDP server (on my computer for instance).
right, the port of the responder part which is listening on this port for incomming messages - name it "SERVERPORT"
shirogeek wrote:
Fri Oct 15, 2021 11:02 am
I'm actually looking for the port I can combine with the IP address of the ESP32 to send messages from my computer to the ESP32 and this doesn't seem to be the one in the config.
you want send from your PC ( client ) to the ESP32 ( server - listening on a specific fixed port )
how many ESP32 do you have in your setup? do you want talk from PC to an other ESP32 which is listening

or do you want send from your PC ( Server ) to the ESP32 ( client ) back/reply to the requested packt /request ?
so only want send back messages to a client which was connected as Client the PC which act s a server?

if you want connect to a responder - the responder should listening on a specific port which you are setup for listening and acting
all clients should know this port for connecting and it is a fix port then which you setup like it is talked -
shirogeek wrote:
Fri Oct 15, 2021 11:02 am
From my tests, it seems it is randomly generated.
you sure mean the client sending port ( request port ) - this is randimly generated as long you not fix this in your setup too.
it is a code thing, but you can set this also to a fixed port - not sure why you want do this, you get allways the remotly port which was connecting to the host in the message, so if you want send back, you simple send to the "sockets.client.port" which you see in your PC protokoll.
shirogeek wrote:
Fri Oct 15, 2021 11:02 am
I wait for the ESP32 to send me a packet
...now you have the senders port
it does not have to do with the senders target port, which you setup
shirogeek wrote:
Fri Oct 15, 2021 11:02 am

and there in the packet i can see esp32 IP address and incoming port.
incoming port = request port = randomly as long as you not set it fix in your code at socket setup.
shirogeek wrote:
Fri Oct 15, 2021 11:02 am

I can use then that incoming port to reply to the esp32 but it's not systemic.
if you want send an ACK this request.client.port is then usefull.
if you want talk between esp32 and pc or many esp.. you can setup every as a UDP "server" on the same port..
all "servers" listening now on same port but have difference IP's.
you can talk to one over the specific IP then -
if you want talk to one of them simple use the specific IP then.
if you want talk with all, simple send message to all of them in a broadcast.
shirogeek wrote:
Fri Oct 15, 2021 11:02 am
I hope my question is clearer now.
it was from begin :) but i think the answeres goes now trivial :)

do you use arduino or ESP-IDF or other?
how many clients you have in the setup? ( PC - ESP32 )
which programming software / language you use on PC side?


edit:
arduino example
what you search for is likewise this for your PC:
// Send return packet
UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
UDP.write(reply);
UDP.endPacket();
and you want know how you can setup your ESP so - that the PC allways has the right ESP port to connect them
you can setup your ESP as an Server ecample? Or you can set your ESP client port as static so it is not more randomly -
so pricipal you transform then the client to an server with this, which is not listening but use allways the same
request port in sending packet's. it is not a good idea. better you setup then the esp as an server i think.

as long as you not close the socket, the requested port is valid.
so you can talk between your pc and your ESP as long as you not close the socket.connection over this port right?