Mentre il ciclo nel thread java non viene eseguito
Sto creando un mod Minecraft in cui voglio avere un server socket in esecuzione in background in attesa di un messaggio dal client python.
Il server è in esecuzione in un thread.
Ecco il codice in cui inizio il thread:
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
InterPythonJavaCommunicatorServer.begin(socket, sockIn, sockOut);
}catch (IOException e){
System.out.println("Woah! Somethings gone wrong! ringing a alarm now!");
}
}
});
t.start();
t.join();
E l'intera classe Server
package com.satyamedh.minecraft_ai_helper.communicator;
import com.satyamedh.minecraft_ai_helper.Movement;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.net.Socket;
public class InterPythonJavaCommunicatorServer{
public static void begin(Socket socket, BufferedReader sockIn, BufferedWriter sockOut) throws IOException {
boolean done = false;
while (true) {
System.out.println("Test!");
boolean ready = sockIn.ready();
if(!ready) {
return;
}
try {
String response = sockIn.readLine(); // Stops Here!
System.out.println(response);
//if (response == null) {
//System.out.println("Remote process closed the connection.");
//done=true;
//}
if (response.equals("forward\n")){
boolean o = Movement.forward(1);
if (o){
sockOut.write("done");
sockOut.flush();
}
}
} catch (IOException e) {
System.out.println("Welp! ALARM BOI!");
}
}
}
}
Ho interrotto l'intero codice e sembra che si fermi String response = sockIn.readLine();. E il Test!messaggio viene eseguito solo una volta.
Ps: ho cercato su Google per ore perché! non riesco a trovare nulla di correlato!
So che potrebbe essere molto stupido e facilmente scoperto :)
Modifica: come ha detto @DevParzival ho provato a usare t.join();ma fa ancora lo stesso! Ho modificato il codice sopra in modo che corrisponda a quello attuale.
Edit2: ecco il codice client (python)
import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", 1243))
server_socket.listen(5)
print(
"TCPServer Waiting for client on port 1243")
while 1:
client_socket, address = server_socket.accept()
print(
"I got a connection from ", address)
while 1:
data = input("SEND( TYPE q or Q to Quit):")
if data == 'Q' or data == 'q':
client_socket.send(bytes(data, encoding='utf8'))
client_socket.close()
break
else:
client_socket.send(bytes(data, encoding='utf8'))
data = client_socket.recv(512)
if data == 'q' or data == 'Q':
client_socket.close()
break
else:
print("RECEIVED:", data)
Grazie in anticipo
Risposte
Prova a inviare il NewLine | CHR(10) | \ncarattere alla fine del payload del pacchetto Python, se lo usi socketIn.readLine()in un lato ricevente attende un delimitatore di riga prima di tornare.
Se vuoi studiare il socket NIO Java non bloccante, puoi studiare il mio programma di esempio qui, tieni presente che il socket NIO necessita di un approccio completamente diverso https://stackoverflow.com/a/26312841/185565