Arduino Ethernet Server Client communication issue

Using ATMega2530 with Ethernet Shield HanRun 11105A. Something is wrong with my following code and i am not able to figure it out for last 2 days!

The problem is when ‘#’ key is pressed first time with input_code =111, client.read somehow miss or skip the response (name) from server and shows blank name. But when i press ‘#’ key second time with input_code = 222, it shows previous response (name = ABC Company) from server. Please help me to find where is the problem !!! thanks in advance

111 - ABC Company
222 - XYZ Limited

Please Help!
Thanks in advance.

This is my code-

    #include <Keypad.h>
    #include <LiquidCrystal.h>
    #include <Ethernet.h>
    #include <SPI.h>
    
    
    
    bool hands = false;
    bool result = true;
    char Key;
    unsigned int Number=0;
    String weight;
    String path;
    bool alreadyConnected = false; // whether or not the client was connected previously
    int cnt=0;
    
    
    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
    byte ip[] = { 192, 168, 1, 2 }; // ip in lan
    byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
    byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
    
    LiquidCrystal lcd(35, 34, 33, 32, 31, 30);
    
    EthernetServer server(9639); //server port
    EthernetClient client;
    
    char keys[4][4] = {
      {'1', '2', '3','A'},
      {'4', '5', '6','B'},
      {'7', '8', '9','C'},
      {'*', '0', '#','D'},
    };
    byte rowpins[4] = {22, 23, 24, 25};
    byte colpins[4] = {26, 27, 28, 29};
    
    Keypad mykeypad = Keypad(makeKeymap(keys), rowpins, colpins, 4, 4);
    
    String readString;
    
    void setup()
    {
    
      lcd.begin(20, 4);
    
    
      Ethernet.begin(mac, ip, gateway, subnet);
      
      if (Ethernet.linkStatus() == LinkOFF) {
      lcd.setCursor(0, 0);
      lcd.print("       :Error:       ");
      lcd.setCursor(0, 1);
      lcd.print("Ethernet Cable");
      lcd.setCursor(0, 2);
      lcd.print("Not Connected!");    
        Serial.println("Ethernet cable is not connected.");
        while(true);
      }
       
      Serial.begin(19200);
      Serial1.begin(19200);
      EthernetClient client = server.available();
      
      lcd.setCursor(0, 1);
      lcd.print("    Connecting...    ");
      delay(5000);
      if (client.connect(gateway, 9639))
      {
        client.print("A");
        Serial.println("Connecting MilkyWeigh Server...");
      }
      else
      { lcd.clear();
        lcd.setCursor(0, 1);
        lcd.print("1.Check LAN Cable");
        lcd.setCursor(0, 2);
        lcd.print(" --Reset MilkoSol-- ");
        while (true);
      }
      
      server.begin();
      
      while (true) {
        client = server.available();  // wait for a new client:
        if (client)
        {
          while (client.connected())
          {
            if (client.available())
            {
              char b = client.read();
              if (b == 'B')
              {
                lcd.setCursor(0, 2);
                lcd.print("     Connected!     ");
                Serial.println("Successfully Connected!");
                delay(1500);
                hands = true;
                b = "";
              } //if B comes, handshaking done
            } // if client available
          } //while client connected
        } //if (client)
        if (hands == true)
        {
          lcd.clear();
          client.stop();
          break;
        }
      } //while true
      lcd.setCursor(0, 0);
      lcd.print ("Input Code: ");
      lcd.print (Number);
    } // void setup
    
    void loop()
    {
      while (Key == NO_KEY){
        Key = mykeypad.getKey();
      }
      DetectButtons();
      if (Key == '4')
      {
       SaveToServer();
       delay(200);
       SaveToServer();
      }
        if (Key == '#')
      {   
          if (Number > 0)
          {
            GetName();
          }
          Getweight();
      }
     Key=NO_KEY;
    }
    
    void Getweight(){
      while (Serial1.available()>0)
      {
        weight = Serial1.readStringUntil('\r');
        Serial1.flush();
      }
      Serial.print(weight);
    }
    
    void SaveToServer()
      {
       if (client.connect(gateway, 9639))
      {
        client.print('@'+String(Number)+','+weight);
        //client.stop();
      }
      else
      { lcd.clear();
        lcd.setCursor(0, 2);
        lcd.print(" Save Error ");
        Serial.println("Try again!");
        Number=0;
        readString="";
      //  weight="";
        alreadyConnected=false;
        return 0;
      }
    
      server.begin();
    
          EthernetClient client = server.available(); // Listening for client (i.e. server) response
    
        if (client)
        {
         if (!alreadyConnected)
         {
          // clear out the input buffer:
          client.flush();
          alreadyConnected = true;
        }
          while (client.connected())
          {
            if (client.available())
            {
              if (client.available()>0)
              {
              char z = client.read();
              if (z == 'K')
              {
                Serial.print("Success: Data Saved!");
                lcd.clear();
                lcd.setCursor(6, 2);
                lcd.print("Success!");
                delay(1500);
                Number=0;
                weight="";
                readString="";
                DisplayResult();
                }
                client.flush();
                client.stop();
              }
            }
          }
        } // if client
      }// SaveToServer
    
    void GetName()
      {
       if (client.connect(gateway, 9639))
      {
        client.print(Number);
        //client.stop();
      }
      else
      { lcd.clear();
        lcd.setCursor(0, 2);
        lcd.print(" Connection Failed! ");
        Serial.println("Connection Failed!");
        Number=0;
        readString="";
      //  weight="";
        alreadyConnected=false;
        return 0;
      }
    
      server.begin();
    
          EthernetClient client = server.available(); // Listening for client (i.e. server) response
    
        if (client)
        {
         if (!alreadyConnected)
         {
          // clear out the input buffer:
          client.flush();
          alreadyConnected = true;
        }
          while (client.connected())
          {
            if (client.available())
            {
              readString="";
              while (client.available()>0)
              {
              //client.readStringUntil('?');
              readString = client.readStringUntil('?');
                client.flush();
                client.stop();
              }
            }
          }
              if (readString == "err")
              {
                lcd.clear();
                lcd.setCursor(0, 2);
                lcd.print("Not found,Try again!");
                Serial.println("Not found,Try again!");
                delay(1500);
                Number=0;
                readString="";
                hands = true;
                DisplayResult();
              }
              else
              {
              lcd.clear();
              DisplayResult();
                Serial.println(readString);
                hands = true;
              }
        } // if client
      }// GetName
    
    
    void DetectButtons()
    {
                // detecting Keys other then '#'
     }

   DisplayResult();
    } // end of detect buttons function
    
    void DisplayResult()
    {
             // display to lcd
    }

Also posted at: https://stackoverflow.com/q/53004603 If you're going to do that then please be considerate enough to add links to the other places you cross posted. This will let us avoid wasting time due to duplicate effort and also help others who have the same questions and find your post to discover all the relevant information. When you post links please always use the chain links icon on the toolbar to make them clickable.

you mix a client of local server with client of a remote server

btw HanRun 11105A is the RJ45 connector, not the shield

Thank you for replying. but I want EthernetClient to behave both like client (when send data to my .net application) and like server when receiving data from my .net application. Can you show me an example to do it right way ?

The EthernetClient can be used to connect to remote server as in WebClient example or it is used by the EthernetServer as a peer for remote client as in ChatServer example.

If you use both, use different names. Now your client 'client' is global and clients returned from server are local but with the same name. And you can make the global client variable local too.

The variable alreadyConnected is used with local client and with server's client.

You don't stop the local client.

You have redundant while/if (client.available()) to test the request (server's client).

Why while (client.available() > 0) around readStringUntil('?')?

EthernetServer should by started with begin only once in setup().

Awesome! it works now :)

I did modification as you stated and it is perfectly running now. Thank you so much !