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
UDP Client example : where to get ESP32 UDP port
Re: UDP Client example : where to get ESP32 UDP port
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
-------------------------------------
love it, change it or leave it.
-------------------------------------
問候飛出去的朋友遍全球魯迪
love it, change it or leave it.
-------------------------------------
問候飛出去的朋友遍全球魯迪
Re: UDP Client example : where to get ESP32 UDP port
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.
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
"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
right, the port of the responder part which is listening on this port for incomming messages - name it "SERVERPORT"
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 -
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.
...now you have the senders port
it does not have to do with the senders target port, which you setup
incoming port = request port = randomly as long as you not set it fix in your code at socket setup.
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.
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:
and you want know how you can setup your ESP so - that the PC allways has the right ESP port to connect them// Send return packet
UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
UDP.write(reply);
UDP.endPacket();
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?
-------------------------------------
love it, change it or leave it.
-------------------------------------
問候飛出去的朋友遍全球魯迪
love it, change it or leave it.
-------------------------------------
問候飛出去的朋友遍全球魯迪
Who is online
Users browsing this forum: VinayDand and 116 guests