Urgent - command is sent received but its not matching the if then else.

When i send the command1 and command2 its not matching, is it a BUG??? Please can someone share i have been trying with this for about more then a week but still not matching.

Always returning 3 even i send command1 or command1 and i made sure i am not sending \n and \r\n

Send command:

echo -n "command1" | nc -4u ip port
printf "command1" | nc -4u ip port

Code:

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

byte mac[]={0xDE, 0xAD, 0xBE, 0xEF, 0xAD, 0xDE};
IPAddress ip(192,168,1,2);
unsigned int localPort = 21;

char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
char  ReplyBuffer[] = "hello world";
EthernetUDP Udp;

char          r1[] = "1";
char          r2[] = "2";
char          r3[] = "3";

void setup() {
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
}

void loop() {
  int packetSize = Udp.parsePacket();
  if(packetSize) {
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
    Udp.write(packetBuffer);

      if ( !strcmp(packetBuffer, "command1") ) {
        Udp.write(r1);
      } else if ( !strcmp(packetBuffer, "command2") ) {
        Udp.write(r2);
      } else {
        Udp.write(r3);
      }

    Udp.endPacket();
  }
  delay(10);
}

Firstly you don't explain exactly what you expect to happen and what is actually happening - please do so - a bug report involves these crucial details.

Secondly you should print out what you actually receive on the serial port, in hex.

I don't know what those libraries do, but I suspect they may be giving you more than just the packet payload, or that the payload isn't null-terminated, or various things. You can see what is actually happening and then that should explain why your sketch isn't recognising the packets as you expect.

I don't know that EthernetUdp.h library, but I'm rather certain that Udp.read() doesn't put a null byte after the last received packet byte (because there's no reason for it to). On the other hand, strcmp() expects both of its arguments to be null-terminated strings.

That Udp.read() method call almost certainly returns the number of actually received bytes. If so, you're currently throwing that return value away; use it to put a null byte just behind the last received byte, then use strcmp() to compare packetBuffer to those string constants.