2

A server in java, sends from a Server socket a byte array, this byte array contains text (utf-8) with the next format:

  1. first 4 bytes: an int with the number of bytes of the text
  2. next n bytes: each byte represents a char.

So i am using "WiFiClient" from the "ESP8266WiFi.h" library (it should be the same as "WiFi.h" library), WifiClient has a method to receive a byte using the read() method, the problem is that i am unable to read correctly the int (first four bytes) or transform the bytes into int value. So i will be very gratefull if you help me with that:

Java (Server) simplified code:

String respuestaServer="RESPUESTAS DEL SERVER";
DataOutputStream out=new DataOutputStream(sock.getOutputStream());
out.writeInt(respuestaServer.getBytes(StandardCharsets.UTF_8).length);
out.write(respuestaServer.getBytes(StandardCharsets.UTF_8));
out.flush();

Arduino (Client) code to receive and interpret the byte array (the objective of this code is transform the bytes into a String):

String recibirInfo() {
  //TRYING TO READ FIRST FOUR BYTES
  byte bytesSizeMsj[4];
  for (int i = 0; i < sizeof(bytesSizeMsj); i++) {
    bytesSizeMsj[i] = client.read();
    Serial.print("BYTE: "+bytesSizeMsj[i]);
  }
  //TRYING TO TRANSFORM THE FOUR BYTES INTO AN INT
  int sizeMsj = int((unsigned char)(bytesSizeMsj[0]) |
            (unsigned char)(bytesSizeMsj[1]) |
            (unsigned char)(bytesSizeMsj[2])|
            (unsigned char)(bytesSizeMsj[3]));
  Serial.println(sizeMsj);
  char charArray[sizeMsj];
  //TRYING TO READ THE REST OF THE MESSAGE
  for (int i = 0; i < sizeof(charArray); i++) {
    charArray[i] = client.read();
  }
  //TRYING TO TRANSFORM THE BYTE ARRAY INTO A STRING
  String msj=charArray;
  return msj;
}
LuisRococo
  • 173
  • 1
  • 12

2 Answers2

2

I fix it, i had problems with the way I read from the socket, the way I transform the 4 bytes into an Int datatype and with the way I transoform the bytes into a String, I figure out why i was not receiving all the bytes when trying to read from the socket, the reason was that when I call the method "client.read()" the server was not fast enought to send the information so it did not read anything, so I did a loop and with help of the method "client.available()" I checked that there was a byte ready to be read, so the loop ends when the first 4 bytes were read. Then I transform the 4 bytes readed into an Int, read the rest of the bytes in a similar way than before, and I tranformed the bytes into a String.

Here is my functional code:

String recibirInfo(bool* error) {
  String msj = "";
  byte bytesSizeMsj[4];
  for (int i = 0; i < sizeof(bytesSizeMsj); i++) {
    if (client.connected()) {
      if (client.available()) {
        bytesSizeMsj[i] = client.read();
      } else {
        delay(10);
        i--;
      }
    } else {
      *error = true;
      return "";
    }
  }
  //TRANSFORMAR LOS 4 BYTES A INT
  int sizeMsj = 0;
  sizeMsj = ((int)bytesSizeMsj[3]) | sizeMsj;
  sizeMsj = ((int)bytesSizeMsj[2]) << 8 | sizeMsj;
  sizeMsj = ((int)bytesSizeMsj[1]) << 16 | sizeMsj;
  sizeMsj = ((int)bytesSizeMsj[0]) << 24 | sizeMsj;

  char charArray[sizeMsj];
  //LEER EL TEXTO
  for (int i = 0; i < sizeof(charArray); i++) {
    if (client.connected()) {
      if (client.available()) {
        charArray[i] = client.read();
      } else {
        delay(250);
        i--;
      }
    } else {
      *error = true;
      return "";
    }
  }
  //TRANSFORMAR BYTES A STRING
  msj = charArrayToString(charArray, sizeMsj);
  Serial.print("RECIBIDO: ");
  Serial.println(msj);
  return msj;
}

String charArrayToString(char arrChar[], int tam) {
  String s = "";
  for (int i = 0; i < tam; i++) {
    s = s + arrChar[i];
  }
  return s;
}
LuisRococo
  • 173
  • 1
  • 12
1

It's kinda of hard to trouble shoot like this do you actually receive data ? Can you show the output you get ? Maybe you aren't even connected.

Also what i've done in the past is convert every int or "letter" into a char. Since i usually send a message of known lenght with a starting char i read every byte of my message and convert it to a char and then add it to my received message string. I dont know if this helps... Basically i do the conversion on the arrival of the byte.

  • I will use the suggestion of adding each incoming byte to the String directly in future projects. Later i will post an answer with corrections of my code since I was able to fix it. – LuisRococo May 13 '20 at 19:39