UDP broadcasting over Station+SoftAP
Posted: Mon Jan 02, 2023 1:48 pm
Hi,
I have a particular problem. I have two ESP32-S2 devices, one configured as Station + SoftAP
and the other is connected to the first one (as a station).
both devices are set to send and receive UDP broadcast packets to each other on the same port. and both of them work fine. In another word, both can send successfully, and both can receive the other packet successfully either.
BUT, if the first device (as a station) gets connected to a third access-point, then it could no longer send UDP packets to its SoftAP interface! it seems the device just sends the UDP broadcasting packet to a third AP, not to stations that get connected to it.
The question is, how can I force it to send UDP broadcast packets to both Station and SoftAP interfaces
Using ESP-IDF 4.3
These simple diagrams may be helpful,
The below scenario works without any problem:
But this scenario get into problem:
the source code gets simplified to something like this for better understanding. (the original source code has error handling and other functionalities that are irrelevant.
I have a particular problem. I have two ESP32-S2 devices, one configured as Station + SoftAP
and the other is connected to the first one (as a station).
both devices are set to send and receive UDP broadcast packets to each other on the same port. and both of them work fine. In another word, both can send successfully, and both can receive the other packet successfully either.
BUT, if the first device (as a station) gets connected to a third access-point, then it could no longer send UDP packets to its SoftAP interface! it seems the device just sends the UDP broadcasting packet to a third AP, not to stations that get connected to it.
The question is, how can I force it to send UDP broadcast packets to both Station and SoftAP interfaces
Using ESP-IDF 4.3
These simple diagrams may be helpful,
The below scenario works without any problem:
Code: Select all
+--------------------------+ +---------------------+
| [DEVICE: 1] | | [DEVICE: 2] |
| SoftAP (Connected) | ---- UDP packets (OK) ---> | Station (Connected) |
| Station (NOT CONNECTED) | <--- UDP packets (OK) ---- | |
+--------------------------+ +---------------------+
Code: Select all
+--------------+ +-----------------------+ +---------------------+
| Another | | [DEVICE: 1] | | [DEVICE: 2] |
| Access-Point | <-------> | SoftAP (Connected)-> | ---- UDP (NOT OK) ---> | Station (Connected) |
| | | <-Station (CONNECTED) | <--- UDP (OK) -------- | |
+--------------+ +-----------------------+ +---------------------+
- source.sin_addr.s_addr = htonl(INADDR_ANY);
- source.sin_family = AF_INET;
- source.sin_port = htons(port);
- dest.sin_addr.s_addr = htonl(INADDR_BROADCAST);
- dest.sin_family = AF_INET;
- dest.sin_port = htons(port);
- sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
- bind(sock, (struct sockaddr *)&source, sizeof(source))
- ....
- recvfrom(sock, rx_buffer, sizeof(rx_buffer) - 1, MSG_DONTWAIT,
- (struct sockaddr *)&source_addr, &socklen);
- ....
- sendto(sock, payload, strlen(payload), 0,
- (struct sockaddr *)&dest, sizeof(dest));