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);