mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-27 18:02:17 +03:00
udp: restore correct address/port when parsing packet (#6011)
do interleave informations on addresses within reception pbuf chain: before: (data-pbuf) -> (data-pbuf) -> (data-pbuf) -> ... in the receiving order now: (address+port-info-pbuf -> data-pbuf) -> (address_port-info-pbuf -> data-pbuf) -> ... address/port informations are updated along with data exposed to user
This commit is contained in:
@ -26,7 +26,7 @@
|
||||
unsigned int localPort = 8888; // local port to listen on
|
||||
|
||||
// buffers for receiving and sending data
|
||||
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
|
||||
char packetBuffer[UDP_TX_PACKET_MAX_SIZE + 1]; //buffer to hold incoming packet,
|
||||
char ReplyBuffer[] = "acknowledged\r\n"; // a string to send back
|
||||
|
||||
WiFiUDP Udp;
|
||||
@ -49,21 +49,15 @@ void loop() {
|
||||
// if there's data available, read a packet
|
||||
int packetSize = Udp.parsePacket();
|
||||
if (packetSize) {
|
||||
Serial.print("Received packet of size ");
|
||||
Serial.println(packetSize);
|
||||
Serial.print("From ");
|
||||
IPAddress remote = Udp.remoteIP();
|
||||
for (int i = 0; i < 4; i++) {
|
||||
Serial.print(remote[i], DEC);
|
||||
if (i < 3) {
|
||||
Serial.print(".");
|
||||
}
|
||||
}
|
||||
Serial.print(", port ");
|
||||
Serial.println(Udp.remotePort());
|
||||
Serial.printf("Received packet of size %d from %s:%d\n (to %s:%d, free heap = %d B)\n",
|
||||
packetSize,
|
||||
Udp.remoteIP().toString().c_str(), Udp.remotePort(),
|
||||
Udp.destinationIP().toString().c_str(), Udp.localPort(),
|
||||
ESP.getFreeHeap());
|
||||
|
||||
// read the packet into packetBufffer
|
||||
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
|
||||
int n = Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
|
||||
packetBuffer[n] = 0;
|
||||
Serial.println("Contents:");
|
||||
Serial.println(packetBuffer);
|
||||
|
||||
@ -72,7 +66,7 @@ void loop() {
|
||||
Udp.write(ReplyBuffer);
|
||||
Udp.endPacket();
|
||||
}
|
||||
delay(10);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
|
Reference in New Issue
Block a user