7 Replies Latest reply on Dec 15, 2015 10:51 PM by BenKissBox

    Send Arbitrary UDP Packet Using Arduino EthernetUdp Library


      Has anything ever gotten a UDP packet to send through straight C++ code? (Using built-in EthernetUdp library) I am tearing my hair out over this, and the fact that the Galileo is able to receive packets just fine but cannot send them as far as I can tell.


      Greater detail on my issue here:

      What Am I Doing Wrong With Wakeup-On-LAN (WOL)? [Semi-Resolved]


      I just want to send a packet, and it happens to be a Wake-On-LAN magic packet. And I want to send it to the broadcast address (, but so far I cannot send it to any address at all.


      I am using Wireshark to monitor traffic in between my PC and my Galileo. I see packets going to the Galileo over UDP, and the Galileo properly echoes the packet contents back to me over its serial port, but I see no responses coming back from it.


      Will post code on request. In the meantime I am looking into installing Linux on an SD card and using Python instead, so alternately any advice on that would help too.

        • 1. Re: Send Arbitrary UDP Packet Using Arduino EthernetUdp Library

          Hi Maggion

          I haven’t done it, but I’ll investigate about it. If I find something useful that might help you I’ll post it here.






          • 2. Re: Send Arbitrary UDP Packet Using Arduino EthernetUdp Library


            Have you tried running the examples of the IDE for UDP to check if it is in fact is sending? I would recommend you to use a basic example to see how it behaves prior to writing code. For example take a look at this one: http://arduino.cc/en/Tutorial/UDPSendReceiveString#.UyshdPldVps. Maybe you can check that one to see if it works properly, after that you can modify that example to send the data you need. I hope it works for you, I’ll keep investigating and I’ll get to you if I find something else.




            • 3. Re: Send Arbitrary UDP Packet Using Arduino EthernetUdp Library

              So here's my code below for the 'Lite' version of my WOL Application. The key part being where the packet get sent in the sendWOLpacket routine. This is modified from some code in the NTP Time example that I kept running into online while looking for UDP-related info.




              #include <String.h>

              #include <Ethernet.h>

              #include <EthernetUdp.h>


              //Debug Purposes. Use Mac defined by 'wake_mac' below

              static const boolean USE_HARD_MAC = true;

              static unsigned char wake_mac[6] = { 0x00,0x16,0xD4,0x03,0x1F,0x66 };  // mac address of Target WOL machine


              int LED_PIN = 13;

              static int i = 0, j = 0;


              const int WOL_PACKET_SIZE = 102;

              byte packetBuffer[WOL_PACKET_SIZE];



              EthernetUDP Udp;

              byte MY_MAC[] = { 0x98, 0x4F, 0xEE, 0x00, 0x12, 0x04 };

              unsigned int localPort = 8888; // local port to listen for UDP packets

              //#define WOL_PORT 2304

              #define WOL_PORT 9

              static unsigned char bcast[4] =    { 255,255,255,255 } ;   // broadcast IP address



              unsigned long sendWOLpacket(unsigned char macaddr[] )


                // set all bytes in the buffer to 0

                //memset(packetBuffer, 0, NTP_PACKET_SIZE);


                int k = 0;

                for(k = 0; k < 6; k++)


                  packetBuffer[k] = 0xFF;


                k = 6;

                for (i = 0; i < 16; i++)


                  for (j = 0; j < 6; j++)


                    packetBuffer[k + j] = wake_mac[j];



                  k += 16;



                Udp.beginPacket(bcast, WOL_PORT);

                Udp.write(packetBuffer, WOL_PACKET_SIZE);






              void setup()




                pinMode(LED_PIN, OUTPUT);


                //Initialize UPD Port

                //Ethernet.begin(MY_MAC, ip);







              void  loop()




                //Blink pulse to show activity

                digitalWrite(LED_PIN, HIGH);


                digitalWrite(LED_PIN, LOW);



              • 4. Re: Send Arbitrary UDP Packet Using Arduino EthernetUdp Library

                So here's what happened:


                I couldn't get any packets to send over UDP, so I installed Linux on my Galileo on an SD card as per Intel's instructions, and then I launched a Python script from the C++ interface and the Python script took care of everything much more easily.


                So just for the heck of it, I want to try something else out tonight to see if I can still get UDP communication to work, and I'll report back to the community here when I get a chance.

                Use the command:

                    system("ifconfig eth0 netmask up");

                ...to configure the ethernet port first, and then try sending packets using a compatible broadcast address, like in this case it would be ''.






                A tip I figured out on my own for working with 'system()' commands in C++ just btw:

                Log into the linux environment over SSH (Putty anybody?), and copy and paste your commands into the console and run them. You'll be able to see exactly why your commands or scripts do not work.


                I am having a lot of fun with my Galileo even though it does not work as I expect it to.

                • 5. Re: Send Arbitrary UDP Packet Using Arduino EthernetUdp Library

                  Hi Maggion

                  so what did you get when you try another way?I've the same problem as you described.I've tried the wifi udp through smartphone or myPC,and get the same result(ip,port 0),which cannot send data to my phone.Any idea to solve this?


                  • 6. Re: Send Arbitrary UDP Packet Using Arduino EthernetUdp Library

                    I have the same problem.


                    I think we should use some Intel library or system call, but I can't find find it...

                    • 7. Re: Send Arbitrary UDP Packet Using Arduino EthernetUdp Library

                      Hi Maggion,


                      I am a bit late to answer to this question, but I think it can be interesting.


                      Question : are you using the Ethernet shield or the embedded network socket on the Galileo ?


                      If you are using the W5100 shield on the Galileo, simply forget about it

                      The shield is perfect for AVR based Arduino boards, but using it on Galileo is exactly like if you have a Tesla car and you want to move it using an electric bicycle motor using the car batteries


                      You can easily access to the Galileo's embedded network support (the local RJ45 if you prefer) from any sketch simply by using the BSD sockets. The BSD sockets are one of the marvels hidden the Galileo's hood, they are extremely powerful (they are true Linux sockets!) and they will make you forget very quickly the numerous limitations (and bugs...) of the W5100 chip.

                      (I have used the W5100 for years, and I can tell that there are many issues with this chip, especially when you deal with UDP)


                      Just look for the socket related functions (the header files are located in /sys within the Arduino compiler chain), they are not complex at all to use, there are a lot of examples on the Web to show how to create a UDP socket under Linux (just Google for "Linux BSD socket UDP example")


                      I am using these BSD sockets on my Galileo boards all the time here, and I can tell how powerful they are