UdpClient, UdpServer, and "remote port number"

I am trying to become familiar with and understand sending and receiving UDP data over Ethernet and have installed the UIPEthernet library.

I have the UdpClient running on a Nano and the UdpServer on a Mega (with ENC28J60) using the Examples and communicating over a private network. (All I have changed is to assign addresses within my network address range).

The communications seems to be working EXCEPT that the "remote port number" reported by the server increases by 1 with each data group received. That doesn't seem right!

Client:

/*
 * UIPEthernet UdpClient example.
 *
 * UIPEthernet is a TCP/IP stack that can be used with a enc28j60 based
 * Ethernet-shield.
 *
 * UIPEthernet uses the fine uIP stack by Adam Dunkels <adam@sics.se>
 *
 *      -----------------
 *
 * This UdpClient example tries to send a packet via udp to 192.168.0.1
 * on port 5000 every 5 seconds. After successfully sending the packet it
 * waits for up to 5 seconds for a response on the local port that has been
 * implicitly opened when sending the packet.
 *
 * Copyright (C) 2013 by Norbert Truchsess (norbert.truchsess@t-online.de)
 */

#include <UIPEthernet.h>

EthernetUDP udp;
unsigned long next;

void setup() {

  Serial.begin(9600);

  uint8_t mac[6] = {0x00,0x06,0x02,0x03,0x04,0x05};

  Ethernet.begin(mac,IPAddress(10,0,199,5));

  next = millis()+5000;
}

void loop() {

  int success;
  int len = 0;

  if (((signed long)(millis()-next))>0)
    {
      do
        {
          success = udp.beginPacket(IPAddress(10,0,199,6),5000);
          Serial.print("beginPacket: ");
          Serial.println(success ? "success" : "failed");
          //beginPacket fails if remote ethaddr is unknown. In this case an
          //arp-request is send out first and beginPacket succeeds as soon
          //the arp-response is received.
        }
      while (!success && ((signed long)(millis()-next))<0);
      if (!success )
        goto stop;

      success = udp.write("hello from Client");

      Serial.print("bytes written: ");
      Serial.println(success);

      success = udp.endPacket();

      Serial.print("endPacket: ");
      Serial.println(success ? "success" : "failed");

      do
        {
          //check for new udp-packet:
          success = udp.parsePacket();
        }
      while (!success && ((signed long)(millis()-next))<0);
      if (!success )
        goto stop;

      Serial.print("received: '");
      do
        {
          int c = udp.read();
          Serial.write(c);
          len++;
        }
      while ((success = udp.available())>0);
      Serial.print("', ");
      Serial.print(len);
      Serial.println(" bytes");

      //finish reading this packet:
      udp.flush();

      stop:
      udp.stop();
      next = millis()+5000;
    }
}

Server:

/*
 * UIPEthernet UdpServer example.
 *
 * UIPEthernet is a TCP/IP stack that can be used with a enc28j60 based
 * Ethernet-shield.
 *
 * UIPEthernet uses the fine uIP stack by Adam Dunkels <adam@sics.se>
 *
 *      -----------------
 *
 * This UdpServer example sets up a udp-server at 192.168.0.6 on port 5000.
 * send packet via upd to test
 *
 * Copyright (C) 2013 by Norbert Truchsess (norbert.truchsess@t-online.de)
 */

#include <UIPEthernet.h>

EthernetUDP udp;

void setup() {

  Serial.begin(9600);

  uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05};

  Ethernet.begin(mac,IPAddress(10,0,199,6));

  int success = udp.begin(5000);
 
  Serial.print("initialize: ");
  Serial.println(success ? "success" : "failed");

}

void loop() {

  //check for new udp-packet:
  int size = udp.parsePacket();
  if (size > 0) {
    do
      {
        char* msg = (char*)malloc(size+1);
        int len = udp.read(msg,size+1);
        msg[len]=0;
        Serial.print("received: '");
        Serial.print(msg);
        free(msg);
      }
    while ((size = udp.available())>0);
    //finish reading this packet:
    udp.flush();
    Serial.println("'");
    int success;
    do
      {
        Serial.print("remote ip: ");
        Serial.println(udp.remoteIP());
        Serial.print("remote port: ");
        Serial.println(udp.remotePort());
        //send new packet back to ip/port of client. This also
        //configures the current connection to ignore packets from
        //other clients!
        success = udp.beginPacket(udp.remoteIP(),udp.remotePort());
        Serial.print("beginPacket: ");
        Serial.println(success ? "success" : "failed");
    //beginPacket fails if remote ethaddr is unknown. In this case an
    //arp-request is send out first and beginPacket succeeds as soon
    //the arp-response is received.
      }
    while (!success);

    success = udp.println("hello world from arduino");

    Serial.print("bytes written: ");
    Serial.println(success);

    success = udp.endPacket();

    Serial.print("endPacket: ");
    Serial.println(success ? "success" : "failed");

    udp.stop();
    //restart with new connection to receive packets from other clients
    Serial.print("restart connection: ");
    Serial.println (udp.begin(5000) ? "success" : "failed");
  }
}

Can anyone explain to me what is happening????

Maybe you should use udp.begin(5000) in the client setup function also.

Tried that - no change.

The sample program I copied from did not use a "udp,endPacket" command so I added one.

The udp now reaches the receiver on the same port every time

Your client code shows the endPacket call. Maybe you should have posted your real code.

I have the same problem, I cant get my server to reply back to udp.remotePort(), it just uses the same port as set within udp.begin()

do I have to flush() to make this work, or some other function ?

mcnobby:
I have the same problem, I cant get my server to reply back to udp.remotePort(), it just uses the same port as set within udp.begin()

do I have to flush() to make this work, or some other function ?

I don't understand what you mean. If you mean it is replying to the packet using the source port set with udp.begin, that is the way it works. If you mean it is sending the response packet to the destination port set in udp.begin, that is a fail somewhere.

If any doubt, post your code.

Hi Tim

I am back looking at this issue again...

I meant that if my device is set to 5555 with udp.begin, and a packet arrives from outside on port 8888, when I try and reply back to the originator using udp.remotePort(), my device replies using 5555 instead of the 8888, so the originator doesnt see my reply

Am I just seeing this wrong, or perhaps is there a bug in the library I wonder
I tried to trace it through but kind of ran out of steam with it

Bob

DianneB:
I am trying to become familiar with and understand sending and receiving UDP data over Ethernet and have installed the UIPEthernet library.

I have the UdpClient running on a Nano and the UdpServer on a Mega (with ENC28J60) using the Examples and communicating over a private network. (All I have changed is to assign addresses within my network address range).

The communications seems to be working EXCEPT that the "remote port number" reported by the server increases by 1 with each data group received. That doesn't seem right!

Client:

/*
  • UIPEthernet UdpClient example.
  • UIPEthernet is a TCP/IP stack that can be used with a enc28j60 based
  • Ethernet-shield.
  • UIPEthernet uses the fine uIP stack by Adam Dunkels adam@sics.se

*      -----------------
*

  • This UdpClient example tries to send a packet via udp to 192.168.0.1
  • on port 5000 every 5 seconds. After successfully sending the packet it
  • waits for up to 5 seconds for a response on the local port that has been
  • implicitly opened when sending the packet.
  • Copyright (C) 2013 by Norbert Truchsess (norbert.truchsess@t-online.de)
    */

#include <UIPEthernet.h>

EthernetUDP udp;
unsigned long next;

void setup() {

Serial.begin(9600);

uint8_t mac[6] = {0x00,0x06,0x02,0x03,0x04,0x05};

Ethernet.begin(mac,IPAddress(10,0,199,5));

next = millis()+5000;
}

void loop() {

int success;
  int len = 0;

if (((signed long)(millis()-next))>0)
    {
      do
        {
          success = udp.beginPacket(IPAddress(10,0,199,6),5000);
          Serial.print("beginPacket: ");
          Serial.println(success ? "success" : "failed");
          //beginPacket fails if remote ethaddr is unknown. In this case an
          //arp-request is send out first and beginPacket succeeds as soon
          //the arp-response is received.
        }
      while (!success && ((signed long)(millis()-next))<0);
      if (!success )
        goto stop;

success = udp.write("hello from Client");

Serial.print("bytes written: ");
      Serial.println(success);

success = udp.endPacket();

Serial.print("endPacket: ");
      Serial.println(success ? "success" : "failed");

do
        {
          //check for new udp-packet:
          success = udp.parsePacket();
        }
      while (!success && ((signed long)(millis()-next))<0);
      if (!success )
        goto stop;

Serial.print("received: '");
      do
        {
          int c = udp.read();
          Serial.write(c);
          len++;
        }
      while ((success = udp.available())>0);
      Serial.print("', ");
      Serial.print(len);
      Serial.println(" bytes");

//finish reading this packet:
      udp.flush();

stop:
      udp.stop();
      next = millis()+5000;
    }
}





Can anyone explain to me what is happening????

Client:

Ethernet.begin(mac,IPAddress(10,0,199,5));
udp.begin(5001); //for example client port = 5001

//udp.stop(); //remove this command