i am writing a C# program as a server to communicate with two arduinos with ethernet shield acting as clients , intended to humidity and temperature reading . it is working but not as intended it gets data from Client4 IP( more than Client5 IP( is there a way to stabilize this evry 2 seconds get data from both

below is my code in c#

private void Form1_Load(object sender, EventArgs e)
        IPHostEntry IPHost = Dns.GetHostByName(Dns.GetHostName());
       label1.Text = "My IP address is " +
        alSockets = new ArrayList();
        Thread thdListener = new Thread(new ThreadStart(StartListening));


public void StartListening()
        int flag = 0;
        // Data buffer for incoming data.
        byte[] bytes = new Byte[1024];

        // Establish the local endpoint for the socket.
        // Dns.GetHostName returns the name of the 
        // host running the application.
        IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
        IPAddress ipAddress = ipHostInfo.AddressList[0];

        IPEndPoint localEndPoint = new IPEndPoint(ipAddress , 8000);

        // Create a TCP/IP socket.
        Socket listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);

        // Bind the socket to the local endpoint and  listen for incoming connections.
            while (true)

                // Program is suspended while waiting for an incoming connection.

                Socket handler = listener.Accept();

                int IPLength = handler.RemoteEndPoint.ToString().IndexOf(":");

                // An incoming connection needs to be processed.

                while ((true))  

                    if (handler.Available > 0)

                        bytes = new byte[1024];
                        int bytesRec = handler.Receive(bytes, 0, handler.Available, SocketFlags.None);
                        String data = Encoding.ASCII.GetString(bytes, 0, bytesRec);

                        if (data.Length>2)    //2 equals to \n  so the incoming value should not be empty 
                            listBox1.Invoke(new Action(() => listBox1.Items.Add("Start " + data + "data  from " + handler.RemoteEndPoint.ToString() + " End")));
                            //listBox1.Invoke(new Action(() => listBox1.Items.Add(" ")));
                            // echo data back to client
                            //handler.Send(end, 0, end.Length, SocketFlags.None);
                            if (data.IndexOf("\n") >= 0) //Terminate connection after \n
                                // Thread.Sleep(1000);

                                Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                                handler = listener.Accept();


                    }//end if (handler.available)

                  }//end while true
            }//end while true
        catch (Exception e)



     //Arduino Sketch   for Client4 and Client5 
    #include <SPI.h>
    #include <Ethernet.h>

      byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
      IPAddress ip(10,154,91,50);// shield ip(Client) - Arduino
      IPAddress server(10, 154, 91, 100); // my computer ip(Server)

       EthernetClient client;
       int i=0;
    void setup() 
      // start the Ethernet connection:

    Ethernet.begin(mac, ip);// Open Ethernet communications and wait for 

      while (!Serial) 
          ; // wait for serial port to connect. Needed for native USB port 
            //     only
}//end setup 


      void loop() 

 // give the Ethernet shield a second to initialize:

   delay(2000);/////set specific time to connect to server to send data to 
                ///  it periodically 

 if (client.connect(server,8000)) 
   // if you didn't get a connection to the server:
     Serial.println("connection failed");

 // if there are incoming bytes available
// from the server, read them and print them:

if (client.available()) 
 char c = client.read(); 

   if (client.connected()) 
      client.println("Client 4: ");

 //  delay(2000);
// if the server's disconnected, stop the client:

if (!client.connected()) 

 }//end  if


the output on C# enter image description here

  • Do you really have both clients having the same MAC address? – Majenko May 3 at 16:34
  • No , Client 5 MAC Address : { 0xDE, 0xAD, 0xBE, 0xEA, 0xFE, 0xED}; and Client 4 MAC Address : { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED} – user3315958 May 3 at 16:35
  • Ok, that's fair. What debugging do you get from the Arduinos? – Majenko May 3 at 16:39
  • actually iam using Arduino IDE and use Serial.println(""); command to get whats going on i can do this for on arduino at a time not both and i print events like trying to Connect - Connected -Sending Data - Disconnect do you recommend a special tool – user3315958 May 3 at 16:51
  • 1
    Then the problem is probably with your C# program, which is outside our sphere on this site... – Majenko May 3 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.