NodeJs 服务器充斥着 UDP 广播,不发送响应

NodeJs Server flooded with UDP Broadcasts, doesn't send response

本文关键字:响应 广播 UDP 服务器 NodeJs      更新时间:2023-10-16

我正在使用ESP8266/NodeMcu模块(类似于Arduino,只是具有网络功能(和在本地网络上运行的NodeJs服务器构建一个系统。

为了发现服务器的IP地址,我正在尝试在NodeMcu模块上使用UDP广播。这个想法是在本地广播IP上发送消息(例如192.168.1.255(。然后,服务器接收消息并发送响应,确认它是服务器。这样,NodeMcu知道服务器的直接地址,以便进一步通信。

问题是,每当服务器从 NodeMcu 收到第一条消息时,它就会完全用相同的消息淹没自己,而 NodeMcu 实际上每秒只发送一次消息。

在 NodeMcu 端看起来像这样:

[UDP] Sending UDP Broadcast on IP: 192.168.43.255, Port: 8080, Message: ESP8266 UDP Server Discovery Broadcast

服务器每秒输出很多次类似的东西:

[10:33:07] 127.0.0.1:8080 @ service discovery : ESP8266 UDP Server Discovery Broadcast
[10:33:07] 127.0.0.1:8080 @ service discovery : ESP8266 UDP Server Discovery Broadcast
[10:33:07] 127.0.0.1:8080 @ service discovery : ESP8266 UDP Server Discovery Broadcast

它接收那么多消息是没有意义的,特别是因为它显然来自127.0.0.1而不是 NodeMcu的 IP。它也不会发出任何响应。

我尝试使用UDP Monitor应用程序,一个名为Packet Sender的应用程序和Linux终端在手机上接收广播。一切正常,发送手动响应触发了 Nodemcu 上的确认。

所以我认为服务器或我正在使用的网络一定存在某种错误。服务器在我的计算机上运行在Linux上,而我通过手机上的热点托管网络(我的真实WiFi网络阻止了UDP广播(。Linux 防火墙已关闭。

无论如何,我都不是JavaScript或NodeJs的专家,服务器是由我一起工作的人编写的,但他也不知道。无论如何,这是服务器上的重要部分:

client.on('listening', function () {
var address = client.address();
debugMessage(
format('Service discovery running on port %s', config.port)
);
client.setBroadcast(true);
});
client.on('message', function (message, rinfo) {
debugMessage(
format('%s:%s @ service discovery : %s', rinfo.address, rinfo.port, message)
);
client.send(message, 0, message.length, rinfo.port, rinfo.ip);
});
client.bind(config.port);

NodeMcu 上的代码如下所示:

#include <ESP8266WiFi.h>        // WiFi library
#include <WiFiUdp.h>            // UPD functionality
// UDP variables
WiFiUDP udp;
unsigned int localUdpPort = 8080;
char incomingPacket[255];
const char broadcastMessage[] = "ESP8266 UDP Server Discovery Broadcast";
// Server details - written to when the server is found
IPAddress serverIp = ~WiFi.subnetMask() | WiFi.gatewayIP(); // Use Broadcast Address as default in case the UDP service discovery isn't working as intended
unsigned int serverPort = localUdpPort; // Use local port as default in case the UDP service discovery ins't working as intended
void setupWiFi()
{
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
#if LOGGING
Serial.println("Connecting to network: " + (String) WIFI_SSID);
#endif
while (WiFi.status() != WL_CONNECTED)
{
delay(100);
}
#if LOGGING
Serial.print("Connected to network, Local IP Address: ");
Serial.println(WiFi.localIP());
#endif
udp.begin(localUdpPort); // begin listening on UDP port
#if LOGGING
Serial.printf("Now listening at IP %s, UDP port %dn", WiFi.localIP().toString().c_str(), localUdpPort);
#endif LOGGING
}
// Discover the server via a UDP broadcast, and store it's IP and Port in the local network in field variables for later use
// IMPORTANT - For the server to work, the Linux Firewall has to be disabled!!!
void discoverServer()
{
changeColor(PURPLE, false); // change the color of the RGB status LED to signal that the program is searching for the server
bool serverFound = false; // stop when the server is found
IPAddress broadcastIp = ~WiFi.subnetMask() | WiFi.gatewayIP(); // Get the Broadcast IP of the local network (e.g. 192.168.0.255)
while (!serverFound)
{
// Send UDP Broadcast
udp.beginPacket(broadcastIp, localUdpPort);
udp.write(broadcastMessage);
udp.endPacket();
#if LOGGING
Serial.printf("[UDP] Sending UDP Broadcast on IP: %s, Port: %d, Message: %sn", broadcastIp.toString().c_str(), localUdpPort, broadcastMessage);
#endif
delay(1000); // Pause a few milliseconds to avoid flooding the network
// Receive UDP packets
int packetSize = udp.parsePacket();
if (packetSize > 0)
{
// Read incoming UDP Packet
int len = udp.read(incomingPacket, 255);
if (len > 0)
{
incomingPacket[len] = 0;
}
#if LOGGING
Serial.printf("[UDP] Received %d bytes from %s, port %dn", packetSize, udp.remoteIP().toString().c_str(), udp.remotePort());
Serial.printf("[UDP] Packet contents: %sn", incomingPacket);
#endif
// Check if the received message is from the server we are searching for
if (strcmp(incomingPacket, broadcastMessage) == 0)
{
serverIp = udp.remoteIP();
serverPort = udp.remotePort();
#if LOGGING
Serial.printf("[UDP] Found Server on IP: %s, Port: %dn", serverIp.toString().c_str(), serverPort);
#endif
serverFound = true;
changeColor(YELLOW, false); // Change status color of RGB LED back to yellow
}
}
}
}

我真的想知道服务器,网络或NodeMcu是否有问题。特别是因为我尝试的所有其他方法都完美运行,只是当我从 NodeMcu 发送它时并非如此。任何帮助都非常感谢!

原来服务器代码有误。 而不是

client.send(message, 0, message.length, rinfo.port, rinfo.ip);

它应该是

client.send(message, 0, message.length, rinfo.port, rinfo.address);

服务器不知道rinfo.ip,所以它一遍又一遍地用相同的消息向自己发送垃圾邮件。