You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have different servers on the local network, waiting for incoming UDP packet at some port. On incoming packets, the server will reply to that socket.
As a client that sends a broadcast on the net I have an ESP82266 that after connecting to the local network via WiFi runs that code:
IPAddress local_ip = WiFi.localIP();
IPAddress broadcast_ip = WiFi.broadcastIP();
bool master_found = false;
WiFiUDP udp;
udp.setTimeout(5000);
udp.beginPacket(broadcast_ip, 2038);
udp.write("DATA SEND TO BROADCAST");
udp.endPacket();
while(true) {
int packet_size = udp.parsePacket();
if (packet_size)
{
Serial.printf("Received %d bytes from %s\n", packet_size, udp.remoteIP().toString().c_str());
int len = udp.read(incomingPacket, 255);
if (len > 0) {
incomingPacket[len] = 0;
}
Serial.printf("UDP packet contents: %s\n", incomingPacket);
master_ip = udp.remoteIP();
master_found = true;
} else {
Serial.printf("Timeout\n");
delay(2000);
}
}
Servers (UNIX C program) receive the packet correctly using the following code:
And sends back the response to that sockaddr. But I'm not able to receive the answer on the ESP8266. The parsePacket function returns inmediatly with no data.
The server code works. I made a the UNIX C client to test that receive the responses. Here I share:
int sock;
int yes = 1;
struct sockaddr_in broadcast_addr{};
struct sockaddr_in server_addr{};
socklen_t addr_len;
int count;
int ret;
fd_set readfd;
char buffer[1024];
int i;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
printf("sock error");
fflush(stdout);
return -1;
}
ret = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&yes, sizeof(yes));
if (ret == -1) {
printf("setsockopt error");
fflush(stdout);
return 0;
}
addr_len = sizeof(struct sockaddr_in);
memset((void*)&broadcast_addr, 0, addr_len);
broadcast_addr.sin_family = AF_INET;
broadcast_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
broadcast_addr.sin_port = htons(PORT);
ret = sendto(sock, ADDON_UUID, strlen(ADDON_UUID), 0, (struct sockaddr*) &broadcast_addr, addr_len);
FD_ZERO(&readfd);
FD_SET(sock, &readfd);
ret = select(sock + 1, &readfd, NULL, NULL, NULL);
if (ret > 0) {
if (FD_ISSET(sock, &readfd)) {
count = recvfrom(sock, buffer, 1024, 0, (struct sockaddr*)&server_addr, &addr_len);
if (strstr(buffer, MASTER_ACK)) {
uint32_t serial = 0;
sscanf(buffer,MASTER_ACK" %u",&serial);
printf("[CLIENT] Found master with serial %u on IP on %s\n", serial, inet_ntoa(server_addr.sin_addr));
fflush(stdout);
}
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi all,
I have different servers on the local network, waiting for incoming UDP packet at some port. On incoming packets, the server will reply to that socket.
As a client that sends a broadcast on the net I have an ESP82266 that after connecting to the local network via WiFi runs that code:
Servers (UNIX C program) receive the packet correctly using the following code:
And sends back the response to that sockaddr. But I'm not able to receive the answer on the ESP8266. The parsePacket function returns inmediatly with no data.
The server code works. I made a the UNIX C client to test that receive the responses. Here I share:
Can you give some help please?
Kind regards,
Jordi
Beta Was this translation helpful? Give feedback.
All reactions