Java iş parçacığındaki döngü çalışmazken

Oct 27 2020

Python istemcisinden bir mesaj bekleyerek arka planda çalışan bir soket sunucusuna sahip olmak istediğim bir Minecraft modu yapıyorum.

Sunucu bir iş parçacığında çalışıyor.

İşte iş parçacığını başlattığım kod:

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();


Ve tüm Sunucu sınıfı


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!");
            }
        }
    }

}



Kodun tamamını kırdım ve durmuş gibi görünüyor String response = sockIn.readLine();. Ve Test!mesaj yalnızca bir kez çalışır.

Ps: Nedenini araştırmak için saatlerce Google'da arama yaptım! ilgili bir şey bulamıyor gibi görünüyor!

Çok aptalca olabileceğini biliyorum ve kolayca farkedilebilir :)

Düzenleme: @DevParzival'ın söylediği gibi kullanmayı denedim t.join();ama yine de aynı şeyi yapıyor! Yukarıdaki kodu mevcut koduma uyacak şekilde düzenledim.

Edit2: burada müşteri kodu (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)

Thx önceden

Yanıtlar

Whome Oct 27 2020 at 16:29

NewLine | CHR(10) | \nPython paket yükünün sonunda karakter göndermeye çalışın , eğer socketIn.readLine()bir alıcı tarafında kullanırsanız , dönüşten önce bir satır sınırlayıcı bekler.

Java NonBlocking NIO soketini incelemek istiyorsanız, burada örnek programımı inceleyebilirsiniz, lütfen NIO soketinin oldukça farklı bir yaklaşım gerektirdiğini unutmayın. https://stackoverflow.com/a/26312841/185565