我正在尝试使服务器套接字 java 和 esp32 客户端套接字对话,但只能工作一次,在第一次之后,不能再工作了。如果我重置 java 应用程序工作更多一次,我会使用 java 客户端进行测试并且运行良好,有人帮忙吗?我没有找到问题,我的代码如下。
谢谢
封装套接字;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Server {
public static void main(String args[]) {
try {
ServerSocket server = new ServerSocket(80);
System.out.println("Server ok on port 80");
while(true){
Socket cliente = server.accept();
System.out.println("Client connected from IP " + cliente.getInetAddress().getHostAddress());
InputStreamReader inputStreamReader = new InputStreamReader(cliente.getInputStream());
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(cliente.getOutputStream());
BufferedReader bfr = new BufferedReader(inputStreamReader);
BufferedWriter bfw = new BufferedWriter(outputStreamWriter);
while (true) {
String str = bfr.readLine();
if (str != null) {
System.err.println("client say: " + str);
bfw.write("received");
bfw.newLine();
bfw.flush();
if (str.equalsIgnoreCase("by")) {
break;
}
}
}
bfr.close();
server.close();
}
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
esp32 代码
#include <WiFi.h>
#include <WiFiMulti.h>
WiFiMulti WiFiMulti;
void setup()
{
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
WiFiMulti.addAP("TP-Link_3757", "@ap401mocelin");
Serial.println();
Serial.println();
Serial.print("Waiting for WiFi... ");
while(WiFiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(500);
}
void loop()
{
// const uint16_t port = 80;
// const char * host = "192.168.1.1"; // ip or dns
const uint16_t port = 80;
const char * host = "192.168.0.114"; // ip or dns
Serial.print("Connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, port)) {
Serial.println("Connection failed.");
Serial.println("Waiting 5 seconds before retrying...");
delay(5000);
return;
}
// This will send a request to the server
//uncomment this line to send an arbitrary string to the server
//client.print("Send this data to the server");
//uncomment this line to send a basic document request to the server
client.print("GET /index.html HTTP/1.1\n\n");
int maxloops = 0;
//wait for the server's reply to become available
while (!client.available() && maxloops < 1000)
{
maxloops++;
delay(1); //delay 1 msec
}
if (client.available() > 0)
{
//read back one line from the server
String line = client.readStringUntil('\r');
Serial.println(line);
client.flush();
client.stop();
}
else
{
Serial.println("client.available() timed out ");
}
Serial.println("Closing connection.");
client.flush();
client.stop();
Serial.println("Waiting 5 seconds before restarting...");
delay(5000);
}