4

I am making a Minecraft mod in which I want to have a socket server running in the background waiting for a message from the python client.

The server is running in a thread.

Here is the code in which I start the 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();


And the entire Server class


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

}



I have breakpoined the entire code and it seems to stop on String response = sockIn.readLine();. And the Test! message only runs once.

Ps: I have googled for hours searching why! cant seem to find anything related!

I know it might be very stupid and easiely caught out :)

Edit: As @DevParzival Said I tried using t.join(); but it still does the same! I have edited the above code to match my current one.

Edit2: here is the client code(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 in advance

  • 2
    I'm guessing it's waiting for some input but it is not getting it. What is `sockIn`? How are you passing data to it? – Amongalen Oct 27 '20 at 08:44
  • try to use join method for thread so it will finish after main. – Udesh Oct 27 '20 at 08:47
  • @Amongalen it is a BufferedReader connected to my socket :) –  Oct 27 '20 at 08:48
  • @DevParzival I am a sorta newbie to java threading, can you pls explain? :) –  Oct 27 '20 at 08:48
  • thread has a join method so it will finish after main go to this link to find more : https://www.geeksforgeeks.org/joining-threads-in-java/ – Udesh Oct 27 '20 at 08:51
  • @DevParzival after a lot of debugging I can still confirm it stops at the same line, Even tho ready is true! –  Oct 27 '20 at 09:08
  • How are you sending data to the socket? If the `readLine` blocks the reason is almost always that no data arrives *or* that data arrives, but it's not terminated by a newline. – Joachim Sauer Oct 27 '20 at 09:16
  • @DevParzival I am sending it from python. here is the relevant code: –  Oct 27 '20 at 09:17
  • I'll put it in the question –  Oct 27 '20 at 09:18
  • 1
    Try to send `NewLine | CHR(10) | \n ` character at the end of python packet payload, if you use `socketIn.readLine()` it waits for a line delimiter before return. – Whome Oct 27 '20 at 09:22
  • Does this answer your question? [BufferedReader readLine() blocks](https://stackoverflow.com/questions/15521352/bufferedreader-readline-blocks) – Piro Oct 27 '20 at 09:24
  • If you want to study NonBlocking NIO socket you may study my example program here, please note NIO socket need a quite a different approach https://stackoverflow.com/a/26312841/185565 – Whome Oct 27 '20 at 09:25

1 Answers1

0

Try to send NewLine | CHR(10) | \n character at the end of python packet payload, if you use socketIn.readLine() in a receiving side it waits for a line delimiter before return.

If you want to study Java NonBlocking NIO socket you may study my example program here, please note NIO socket need quite a different approach https://stackoverflow.com/a/26312841/185565

Whome
  • 10,181
  • 6
  • 53
  • 65