I am working on a project that includes sockets on the local network - I want to make a Java server (a desktop application running on Windows) that will listen and establish connections with several clients - Arduino boards.
The problem is that the code is used when trying to establish a connection. Here's the Java code:
monitorThread = new Thread(() -> {
try {
System.out.println("Creating socket...");
ServerSocket server = new ServerSocket(4444);
while (true) {
System.out.println("Waiting for connection...");
Socket client = server.accept();
System.out.println("Did establish connection");
if (delegate != null) {
delegate.didConnect(client);
}
}
} catch (IOException exception) {
System.out.print(exception);
}
});
monitorThread.start();
and Arduino code
#include <SPI.h>
#include <Ethernet.h>
IPAddress serverIp(192, 168, 1, 101);
int serverPort = 4444;
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF
};
IPAddress ip(192, 168, 1, 178);
EthernetClient client;
void setup() {
Ethernet.begin(mac, ip);
Serial.begin(9600);
while (!Serial) {
;
}
delay(1000);
Serial.println("connecting...");
if (client.connect(serverIp, serverPort)) {
Serial.println("connected.");
} else {
Serial.println("connection failed.");
}
}
What happens, both of them do not crash when trying to make a connection, but rather hang - the Java server freezes Socket client = server.accept();, but the Arduino also freezes - onclient.connect(serverIp, serverPort)
A computer running on a Java server has a static IP address (192.168.1.101).
, Java server.accept() - , , ( ), , , - , Arduino .
IP - , 64.233.187.99 (Google), .
? - , ? - ?