Receiving UDP and sending Multiple UDP packets at the same time buffer problem

Hi there,

I am currently trying to set up a udp system with my arduino due and my ethernet Poe shield. I am using a packet generator in c# to send the arduino packets and using 2 wiresharks captures to monitor incoming and outgoing packets

I currently have a struct which i would like to send constantly (this will be dynamic in the future), and when i receive a pack i want to send a similar packet back. However my code seems to run correctly for about 5000 packets received then stop, and i will run it again and this time it will run for 10000 packets received then stop. I am at loss at what would cause this inconsistency, memory leak? any optimization of the code is very much appreciated.

 #include <Ethernet.h>
#include <SPI.h>
EthernetUDP udp;
EthernetClient client;
typedef struct __attribute__((__packed__)) sent {
  unsigned short major;
  unsigned short minor;
  unsigned char messageType;
  unsigned char channel;
  unsigned short payLoadLength;
  unsigned long ID;
  double dateTime;
  byte sourceIP1;
  byte sourceIP2;
  byte sourceIP3;
  byte sourceIP4;
  signed short zeroes1;
  signed short zeroes2;
} sent;

unsigned short majorVersion = 2;
unsigned short minorVersion = 0;
unsigned long IDCount = 1;
unsigned long IDCountPacket = 1;
double messageTimeStamp = 0;
double messageTimeStamp1 = 0;
byte timeStampBuff[8];
sent* sentData = (sent*) malloc(sizeof(sent));//allocate memory

void setup() {

  Serial.begin(9600);
  uint8_t mac[6] = {0x90, 0xA2, 0xDA, 0x00, 0xFA, 0x7D};

  Ethernet.begin(mac);

  int success = udp.begin(18005);

  Serial.print("initialize: ");
  Serial.println(success ? "success" : "failed");
  Serial.print("localIP: ");
  Serial.println(Ethernet.localIP());
  Serial.print("subnetMask: ");
  Serial.println(Ethernet.subnetMask());
  Serial.print("gatewayIP: ");
  Serial.println(Ethernet.gatewayIP());
  Serial.print("dnsServerIP: ");
  Serial.println(Ethernet.dnsServerIP());
  
  sentData->major = majorVersion;
  sentData->minor = minorVersion;
  sentData->messageType = 2;
  sentData->channel = 3;
  sentData->payLoadLength = 36;
  sentData->ID = 1;
  sentData->dateTime = messageTimeStamp;
  sentData->sourceIP1 = Ethernet.localIP()[0];
  sentData->sourceIP2 = Ethernet.localIP()[1];
  sentData->sourceIP3 = Ethernet.localIP()[2];
  sentData->sourceIP4 = Ethernet.localIP()[3];
  sentData->zeroes1 = 00;
  sentData->zeroes2 = 00;
 
}

void acknowledgementCreator ()
{
  sentData->ID = IDCount;
  IDCount++;//increment the id count
  udp.beginPacket(IPAddress(10, 234, 186, 18), 18006);
  udp.write((byte*) sentData, sizeof(sent));
  udp.endPacket();
}

void packetCreator ()
{
  sentData->ID = IDCountPacket;
  IDCountPacket++;//increment the id count
  udp.beginPacket(IPAddress(10, 234, 186, 18), 5001);
  udp.write((byte*) sentData, sizeof(sent));
  udp.endPacket();
}

void loop()
{
  //check for new udp-packet:
  int size = 0;
  size = udp.parsePacket();
  if (size > 0)
  {
    char* msg = (char*)malloc(size);//alocate memory
    int len = udp.read(msg, size);
    msg[len] = 0;
    udp.flush();
    acknowledgementCreator();
    free(msg);
  }
  packetCreator();
}

Not had a good look at your code but maybe worth using a freemem tool to see if your malloc/free commands are fragmenting memory. Might be better to have a fixed buffer just bigger than the largest packet your likely to receive but do checking to ensure you don't get buffer overflow.

I made the buffer big enough to hold 2 incoming packets so i don't think its them, memory seems to fine from what i can tell. The problem is i don't know what could cause such inconsistency.

thanks

I have completely striped my code of any dynamic packets and structs and all but the reading buffer and the problem still persist.

Is this issue still around? the only thing i can think of.
https://code.google.com/p/arduino/issues/detail?id=669

thanks

Could someone have a quick look at my code and double check that's not the problem?

My thoughts is that the w5100 chip buffer is overflowing because i sending packets to quickly.

Thanks

here is the same problem with the example code slightly modified packets stop sending after 5000-25000 and crashes/stops

/*
  UDPSendReceive.pde:
 This sketch receives UDP message strings, prints them to the serial port
 and sends an "acknowledge" string back to the sender

 A Processing sketch is included at the end of file that can be used to send
 and received messages for testing with a computer.

 created 21 Aug 2010
 by Michael Margolis

 This code is in the public domain.
 */


#include <SPI.h>         // needed for Arduino versions later than 0018
#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[] = {
  0x90,0xA2,0xDA,0x00,0xFA,0x7D
};

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

// buffers for receiving and sending data
char packetBuffer[1024]; //buffer to hold incoming packet,
byte  ReplyBuffer[28];       // a string to send back

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

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

  Serial.begin(9600);
}

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if (packetSize)
  {
    // read the packet into packetBufffer
    Udp.read(packetBuffer, 1024);
    Udp.flush();
    // send a reply, to the IP address and port that sent us the packet we received
    Udp.beginPacket(IPAddress(IPAddress), 18006);
    Udp.write(ReplyBuffer, 28);
    Udp.endPacket();
  }
  Udp.beginPacket(IPAddress(IPAddress), 5001);
  Udp.write(ReplyBuffer, 28);
  Udp.endPacket();
}

update on what the problem could be:

i think it could be when a packet is received and sent at the same time in the RX/TX buffer in the w5100 chip, because when i try and slow it down it still crashes/stops, its just more likely to happen when packets are being sent/received quickly, not sure on how to fix.