I'm trying connect esp32 with java aplication way socket, but i have a problem, it's works only one time, after the first time show connection failed. for work i need restar server application (java), below my code esp32 and java. thanks
Code: Select all
#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("ssid", pass");
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);
}
else
{
Serial.println("client.available() timed out ");
}
Serial.println("Closing connection.");
client.stop();
Serial.println("Waiting 5 seconds before restarting...");
delay(5000);
}
Code: Select all
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");
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("buy")) {
break;
}
}
}
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}