Arduino doesn't receive udp message when its powered off and on again

Hi guys! Wondering if anyone can help with this. I’m new to networking and arduino and have been trying to fix this for days with no luck.

So I have a C# program here that takes an IP address and a spring number(connected to arduino by motor). When I upload the script and run the program it works fine. The C# program sends over the spring number to rotate to the arduino through a switch and it works perfectly.

The problem is when I power off the arduino and turn it back on again the C# program doesn’t seem to be able to contact the arduino at all.

It’s probably something I’m doing wrong with the udp setup but I’ve tried looking all over for examples and nothing I’ve tried seems to fix it(My old C# code that worked the first time is commented out).

If any of you can help that’d be amazing!

C# code:

public bool SendNumber(string ipAddress, int spring)
            UdpClient udpClient = new UdpClient(80);
            IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ipAddress), 80);

            // Sends a message to the host to which you have connected.
            Byte[] sendBytes = Encoding.ASCII.GetBytes(spring.ToString());

            udpClient.Send(sendBytes, sendBytes.Length);


            //Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            //IPAddress serverAddr = IPAddress.Parse(ipAddress);

            //IPEndPoint endPoint = new IPEndPoint(serverAddr, 80);

            //byte[] send_buffer = Encoding.ASCII.GetBytes(spring.ToString());

            //sock.SendTo(send_buffer, endPoint);

            //DisplayDebug("Contacting IP: " + ipAddress + " to rotate spring " + spring);

            ////byte[] send_buffer = Encoding.ASCII.GetBytes(spring.ToString());

            //SOCK.SendTo(send_buffer, endPoint);

            return true;
        catch (Exception ex)
            DisplayDebug("Failed to send message to Arduino in 'SendNumber' function");
            return false;

Arduino code:

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

// 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, 10);  //Arduinos IP address
unsigned int localPort = 80;      // local port to listen on
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet
String datReq; //Stores incoming string
int packetSize; //Stores size of incoming package
bool debug = false;
int val;

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

//Controls pins
#define enA 9
#define in1 4
#define in2 5
#define enB 10
#define in3 6
#define in4 7

#define LED 13

//Controls motor speed
int motorSpeedA = 70; //Sets speed
int motorSpeedB = 70; //Sets speed

void setup() {
  //Selected pins to control
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  pinMode(LED, OUTPUT);

  // start the Ethernet and UDP:
  Ethernet.begin(mac, ip);
  delay(1500); //Delay a tiny bit to give it time to set up

  if (debug) {
    Serial.begin(9600); //Output window for debugging

void loop() {

  packetSize = Udp.parsePacket(); //Reads the packet size

  if (packetSize > 0) //Check if Arduino has recieved something
  {, UDP_TX_PACKET_MAX_SIZE); //Read the data request
    String datReq(packetBuffer); //Convert char array packetBuffer into a string

      if (datReq.equals("0")) {
        if (debug){
          Serial.println("Rotating spring 1");

        digitalWrite(LED, HIGH);
        digitalWrite(LED, LOW);

        int xAxis = analogRead(A0); // Read Joysticks X-axis
        int yAxis = analogRead(A1); // Read Joysticks Y-axis

        // Set Motor A backward
        digitalWrite(in1, HIGH);
        digitalWrite(in2, LOW);

        analogWrite(enA, motorSpeedA); // Send PWM signal to motor A


        digitalWrite(in1, LOW);
        digitalWrite(in2, LOW);

      if (datReq.equals("1")) {
        if (debug){
          Serial.println("Rotating spring 2");

        digitalWrite(LED, HIGH);
        digitalWrite(LED, LOW);

        int xAxis = analogRead(A0); // Read Joysticks X-axis
        int yAxis = analogRead(A1); // Read Joysticks Y-axis
        // Set Motor B backward
        digitalWrite(in3, HIGH);
        digitalWrite(in4, LOW);


        // Set Motor B backward
        digitalWrite(in3, LOW);
        digitalWrite(in4, LOW);
    //Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());//Initialize packet send

Please describe the process that leads to the failure in more detail.

Is this correct: You power up your Arduino UNO, transfer the sketch by the USB connection, then you start your C# program on the PC. This works.

Then switch the power to the UNO off and after a few seconds on again. The you stop your C# program, wait a few seconds and start it again. Now UNO doesn't react to it.

Is the above order of events correct? If yes, can you still ping your Arduino even if the connection to the C# program is not possible?