bukkit插件内的套接字在使用后关闭

时间:2018-09-04 13:12:02

标签: java sockets bukkit

我正在尝试在bukkit插件中打开一个套接字,以便我可以使用php或node向其发送数据,但不是使用一次后套接字保持打开状态,它只是关闭并且服务器在这种情况发生之前没有加载,我该怎么办我没主意了。

主要:

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

}

套接字服务器类:

调用此方法时,应读取信息,将其转换为数组,然后检查数组中的第一项并将其用作身份验证代码,然后将数组转换为字符串,并在Command executor类中使用。效果很好,但使用一次后就关闭了

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

}

我提到的另一个问题是,在对此套接字发出一个请求之前,bukkit服务器没有完全加载。 谢谢您的帮助。

1 个答案:

答案 0 :(得分:1)

首先,您不应该在主线程上运行这样的套接字,通常应该使用Bukkit调度程序在异步任务上运行它。

然后,打开套接字后,您应该创建一个while循环以连续轮询连接并处理传入的数据。相反,您要做的是打开套接字,读取一条线,然后断开连接。

您想做类似的事情

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

有关更多信息,请参见this webpage