0

My proof of concept for a simple socket between Android and esp8266 works only 50%. I send one message from Android to esp8266, which is received normally, but from esp8266 to android is not, and ends in having java.io.EOFException in android side and Timeout (as seen in code below) for esp8266 side. I provide the related part of code to be brief, if needed I will post all.

Android:

                while (true) {
                socket = serverSocket.accept();
                dataInputStream = new DataInputStream(
                        socket.getInputStream());
                dataOutputStream = new DataOutputStream(
                        socket.getOutputStream());

                String msgReply = "Android server is alive!";
                dataOutputStream.writeUTF(msgReply);
                dataOutputStream.flush();

                message = dataInputStream.readUTF();
                MainActivity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        msg.setText(message);
                    }
                });
            }

ESP8266 side:

String cmd = "Hello from NodeMCU";  
Serial.print("send to server: ");
Serial.println(cmd);
client.print(cmd);
client.flush();
unsigned long timeout = millis();
  while (client.available() == 0) {
      if (millis() - timeout > 5000) {
        Serial.println(">>> Client Timeout !");
        client.stop();
        return;
      }
  }

    while(client.available()){
    String line = client.readStringUntil('!');
    Serial.print(line);
   }

I've tried whatever came to my mind, switching to writing char using client.write('h'), etc. and nothing works. Please point me to some working direction.

4
  • I send one message from Android to esp8266, which is received normally, . Hard to believe. as the leading utf bytes would give you a corrupted string. Commented May 31, 2018 at 15:06
  • You know this: Your server sends a message. Then (tries to ) read a message and dumps the client. It is going to wait for another client to connect. Is this what you want? Commented May 31, 2018 at 15:16
  • client.print(cmd); should be client.print(cmd + "\n"); if you decide to work with lines. Commented May 31, 2018 at 15:18
  • "You know this" - honestly, not really! My knowledge about what I'm doing is so limitted, in fact I'm learning by doing now, took this simple project to start learning sockets. So you mean "dataInputStream.readUTF()" will dump client, could you clarify it? All that means that, in order to not to drop the client after 1 read op, I must simply bring "socket = serverSocket.accept();" out of while true, correct? Also, I can't vote the accepted answer up and I wish, could you make an edit and save, so I vote up please? (it complains when I try "only if it is edited") Commented Jun 2, 2018 at 22:49

2 Answers 2

1

You must either use readUTF() and writeUTF() at both ends or not use them at all: use read() and write(), or readLine() and println(), etc. You can't mix them up like this.

Sign up to request clarification or add additional context in comments.

Comments

0

dataOutputStream.writeUTF(msgReply);

message = dataInputStream.readUTF();

Do not use the UTF functions.

Better use the normal write() and read();

Further you could much better write an read lines. ( "\n" as line/message end. Not a "!").

On Android side you can then use the functions writeLine() and readLine() which makes life much easier.

Further: The server sends a message first and then tries to read an answer. But the client also sends a message first and then tries to read an answer. That can never work. You should decide which one sends a message first.

1 Comment

Thank you, I switched out of readUTF() in android side (and applied other suggestions) and all now works. Used BufferedReader and InputStreamReader instread of readUTF().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.