Alors que la boucle dans le thread java ne s'exécute pas
Je crée un mod Minecraft dans lequel je souhaite qu'un serveur de sockets s'exécute en arrière-plan en attendant un message du client python.
Le serveur s'exécute dans un thread.
Voici le code dans lequel je lance le fil:
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();
Et toute la classe Serveur
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!");
}
}
}
}
J'ai cassé tout le code et il semble s'arrêter String response = sockIn.readLine();. Et le Test!message ne s'exécute qu'une seule fois.
Ps: J'ai cherché pendant des heures sur Google pourquoi! ne semble pas trouver quoi que ce soit lié!
Je sais que cela pourrait être très stupide et facilement attrapé :)
Edit: Comme @DevParzival l'a dit, j'ai essayé d'utiliser t.join();mais cela fait toujours la même chose! J'ai modifié le code ci-dessus pour qu'il corresponde à mon code actuel.
Edit2: voici le code 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)
Merci d'avance
Réponses
Essayez d'envoyer un NewLine | CHR(10) | \ncaractère à la fin de la charge utile du paquet python, si vous l'utilisez socketIn.readLine()du côté de la réception, il attend un délimiteur de ligne avant le retour.
Si vous souhaitez étudier le socket NIO Java NonBlocking, vous pouvez étudier mon exemple de programme ici, veuillez noter que le socket NIO nécessite une approche assez différente https://stackoverflow.com/a/26312841/185565