Udp.parsepacket() negative values

Hi,
I triyng to make a communication between Arduino and a Pc via UDP Ethernet.
To test the connection I used this simple script:

#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);

unsigned int localPort = 8888;      // local port to listen on

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac, ip);
  Udp.begin(localPort);

  Serial.begin(9600);
}

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();

  Serial.println("1) Received packet of size ");

  if (packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
  }
  delay(10);
}

The problem is that the script freeze at the "int packetSize = Udp.parsePacket();" line 'cause the monitor shows:

  1. Received packet of size
    Received packet of size 3
  2. Received packet of size
    Received packet of size 5
  3. Received packet of size
    Received packet of size 12594
  4. Received packet of size
    Received packet of size -24399

When the negative value arrive the sketch crash.

I use an Arduino nano (old bootloader)
An ethernet module 5100
IDE 1.8.19

What could be the problem?

Many thanks in advance.

try reading the contents of the UDP datagram - are they correct?
did you write the PC UDP transmitter code? if so post it

Hi,
should the amount received really be negative?
If not, it's probably a number greater than 32767, and that with variable type int becomes negative.
Try using long packetSize = Udp.parsePacket();

That's a humungous packet. The 12K one is pretty big too. I'm not sure how the UDP library buffers packet data, but if it's trying to load it all into RAM, I'm not surprised that you're seeing a crash given how little memory Arduinos have.

Even I am seeing this issue? Any solution found ?

I'm sorry, I ditched UDP for TCP without solving the problem.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.