简体   繁体   中英

Socket inside bukkit plugin closes after use

I am trying to open a socket inside a bukkit plugin so i could send data to it using php or node but instead of socket remaining open after one use it just closes and also server does not load before this happens what should i do i am out of ideas.

Main:

public class Main extends JavaPlugin {

    public void onEnable() {
        saveDefaultConfig();
        getConfig().options().copyDefaults(true);

        System.out.println("[INFO] Main class loaded.");

        start();


    }

    public void start() {
        SocketServer server = new SocketServer();
        try {
            server.start(getConfig().getInt("port"), getConfig().getString("socket-password"));
            System.out.println("[INFO] Main successfully called start.");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

Socket server class:

When called this should read information convert it into array check the first item in array and use it as auth code then array should be converted into string and used in Command executor class. This works fine but after one use this just closes

public class SocketServer {

    private ServerSocket serverSocket;
    private Socket clientSocket;
    private PrintWriter out;
    private BufferedReader in;

    public void start(int port, String socketpwd) throws IOException {
        System.out.println("[INFO] Socket server listening on: " + port);

        serverSocket = new ServerSocket(port);
        clientSocket = serverSocket.accept();
        out = new PrintWriter(clientSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        Boolean enabled = true;

        try {
        // Socket authentication
        String message = in.readLine();
        String suffix[] = message.split(" ");
        System.out.println("Socket auth code used: "+ suffix[0]);
        System.out.println("Socket pwd is: " + socketpwd);

            if (socketpwd.equals(suffix[0])) {
                out.println("Auth sucessfull!");
                // do the following command from args here

                String command = suffix[1];
                int suffixL = suffix.length;

                // add arguments to command

                for (int i = 2; i < suffixL; i++) {
                    command = command + " " + suffix[i];
                }

                // call req exec

                System.out.println("[INFO] Socket server contacted Request executor with: " + command);
                RequestExecutor.executor(command);

                enabled = false;

            }
            else {
                out.println("Unrecognised auth code!");
            }
        } catch (NullPointerException e) {
            System.out.println("Exception prevented!");
        }

    }

    public void stop() throws IOException {
        in.close();
        out.close();
        clientSocket.close();
        serverSocket.close();
    }

}

Other problem as i mentioned is that bukkit server does not fully load before one request has been made to this socket. Thank you for your help.

First of all you shouldn't be running a socket like that on the main thread, typically you should be running this on an async task using the Bukkit scheduler.

Then once you open the socket you should create a while loop to continuously poll for a connection and handle the incoming data. Instead what you are doing is opening the socket, reading a line and then dropping the connection.

You want to be doing something similar to

while(true){
    Socket socket = serverSocket.accept();
}

See this webpage for some more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM