How can I get multiple line message in the first time?

Hi everyone,

My problem is when I send the one line message to my client , it gets. However, when I send multiple lines it gets some parts first line and when ı send the another request it prints the rest of all. After all, when send the request again it gets but doesnt work , the other different request it gives my request answer which is my old request. I'm newbie , so can someone help me? thank you a lot.

As you see;

My arduino side

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, xx, x, xx);
IPAddress gateway( 10, 0, 0, 1 );
IPAddress subnet(255, 255, 0, 0);
EthernetServer server(5002);
String inData; //char inData;

void setup() {

 // initialize the ethernet device
 Ethernet.begin(mac, ip, gateway, subnet);
 // start listening for clients
 server.begin();
// Open serial communications and wait for port to open:
 Serial.begin(9600);
 Serial.println(Ethernet.localIP());
}

void loop() {
 
 EthernetClient client = server.available();

 // when the client sends the first byte, say hello:
 if (client.connected()) {
while (client.available())
    {
     char thisChar = client.read();
     inData += thisChar;
     // echo the bytes to the server as well:
     Serial.write(thisChar);
    }

   }
   
  if (inData == "1") {
  StaticJsonDocument<250> doc;
  char json[] ="{\"model\":\"Arduino UNO R3 - USB Chip CH340\",\"micro\":\"Microcontroller: ATmega328\",\"flash\":\"Flash Memory: 32 KB (ATmega328) 0.5 KB bootloader\",\"sram\":\"SRAM: 2 KB (ATmega328)\",\"eeprom\":\"EEPROM: 1 KB (ATmega328)\"}";  
  DeserializationError error = deserializeJson(doc, json);
  // Test if parsing succeeds.
  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.c_str());
    return;
  }

 else
 {
   serializeJsonPretty(doc, client);
   inData="";
 }
  }
  
  else if(inData!="1")
  {
    inData="";
    client.print("Any different request?");
  }


}

Cpp client side (get send message parts)

// Do-while loop to send and receive data
	char buf[4096];
	string userInput;
	cout << "Getting information about device: 1" << endl;
	do
	{
		
		// Prompt the user for some text
		cout << "> ";
		getline(cin, userInput);

		if (userInput.size() > 0)		// Make sure the user has typed in something
		{
			// Send the text
			int sendResult = send(sock, userInput.c_str(), userInput.size() + 1, 0);
			if (sendResult != SOCKET_ERROR)
			{
				// Wait for response
				ZeroMemory(buf, 4096);
				int bytesReceived = recv(sock, buf, 4096,0);
				if (bytesReceived > 0)

				{
					// Echo response to console
					cout << "SERVER> " << string(buf, 0, bytesReceived) << endl;
					
				}
				
			}
		}

	} while (userInput.size() > 0);

you may want to wait until you receive a complete "line" before processing what you receive. a "line" could be terminated by a linefeed.

all the JSON string processing may take a bit a time and the serial input buffer overflows before being read

it may be useful to acknowledge a received "line". the sender would not send another "line" until the previous one is acknowledged

Hi gcjr,

Thank you for answering,

I tried sleep functıon in client cpp socket side, but it did not work. I tried 2 minutes and 10 seconds wait it did not work.

if (sendResult != SOCKET_ERROR)
			{
		// Wait for response
		ZeroMemory(buf, 4096);
		int bytesReceived = recv(sock, buf, 4096,0);
		sleep_until(system_clock::now() + seconds(10));
		if (bytesReceived > 0)

		{
				
		cout << "SERVER> " << string(buf, 0, bytesReceived) << endl;
					
			}
				
			}

How I'm gonna do that ? can you give a solution or example for me?

Thank you very much.

don't know how sleep would help.

this function reads/returns a complete line or max string size

// ---------------------------------------------------------
// toggle output bit
#define NUL  0
int
readString (
    char *s,
    int   maxChar )
{
    int  n = 0;

    Serial.print ("> ");
    do {
        if (Serial.available()) {
            int c    = Serial.read ();

            if ('\n' == c)  {
                s [n++] = NUL;
                break;
            }

            s [n++] = c;
            if (maxChar == n)  {
                s [n++] = NUL;
                break;
            }
        }
    } while (true);

    return n;
}

// -----------------------------------------------------------------------------
void
loop (void)
{
    char  s [10];
    int   size = sizeof(s);
    if (readString (s, size))
        Serial.println (s);
}

// -----------------------------------------------------------------------------
void
setup (void)
{
    Serial.begin(115200);
    Serial.println("Read String");
}

it blocks. doesn't need to but needs to keep track of index

it did not work for me , but thank you again for tryıng to help me .

Respects.

how did you test it? do you terminate a line with a '\n'? ??