ESP32 与 java 本地服务器之间的套接字通信

Socket communication between ESP32 and java local server

我正在尝试在我的 Windows PC 上设置 ESP32 作为客户端和 Java 服务器 运行 之间的简单通信。

我有这个 python 有效的服务器代码:

import socket
 
s = socket.socket()         
 
s.bind(('192.168.1.246', 8090 ))
s.listen(0)                 
 
while True:
 
    client, addr = s.accept()
 
    while True:
        content = client.recv(32)
 
        if len(content) ==0:
           break
 
        else:
            print(content)
 
    print("Closing connection")
    client.close()

但是我的整个软件都是用java写的,所以我需要在java中写类似的东西。 这是我的 Java 服务器代码:

Thread serverThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try{  
                    ss = new ServerSocket(8090);
                    s = ss.accept();
                    
                    din = new DataInputStream(s.getInputStream()); 
                    dout = new DataOutputStream(s.getOutputStream());  

                    clientMessage = (String) din.readUTF();
                    while(!clientMessage.equals("stop")){  
                        
                        clientMessage = din.readUTF();
                        serverMessage = "This is the message from the server";
                        dout.writeUTF(serverMessage);
                        dout.flush();  
                    } 
                    din.close();  
                    s.close();  
                    ss.close();
                }catch(Exception e){
                    e.printStackTrace();
                    //System.out.println(e);
                }
            }
        });
        serverThread.start();

我的 ESP32 Arduino 代码是:

include <WiFi.h>
 
const char* ssid = "myssid";
const char* password =  "mywifipass";
 
const uint16_t port = 8090;
const char * host = "192.168.1.246";
 
void setup()
{
 
  Serial.begin(115200);
 
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("...");
  }
 
  Serial.print("WiFi connected with IP: ");
  Serial.println(WiFi.localIP());
 
}
 
void loop()
{
    WiFiClient client;
 
    if (!client.connect(host, port)) {  
 
        Serial.println("Connection to host failed");
 
        delay(1000);
        return;
    }
 
    Serial.println("Connected to server successful!");
 
    client.print("Hello from ESP32!"); 
 
    Serial.println("Disconnecting...");
    client.stop();
 
    delay(10000);
}

如果我使用 python 服务器一切正常,但如果我使用 Java 服务器我可以在它们之间建立连接并且我在服务器行得到一个 EOFExcepion:

clientMessage = (String) din.readUTF();

readUTF() API 说:

Throws:EOFException - if this input stream reaches the end before reading all the bytes.

那么,这是怎么回事,我该如何解决?

我解决了更换

的问题

clientMessage = (String) din.readUTF();

与:

byte[] myMessage = din.readAllBytes();

clientMessage = translate(myMessage);

其中 translate() 函数是:

private static String translate(byte[] word)
{      
    String translatedMessage = "";
    for(int i=0; i<word.length; i++) {
        translatedMessage = translatedMessage + (char) word[i];
    }
    
    return translatedMessage;
}