简体   繁体   English

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

[英]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. 我正在尝试在bukkit插件中打开一个套接字,所以我可以使用php或node向其发送数据,但不是套接字在使用一次后仍保持打开状态,它只是关闭并且服务器在这种情况发生之前也不会加载,我该怎么办?的想法。

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. 调用时,应读取信息将其转换为数组,然后检查数组中的第一项并将其用作身份验证代码,然后将数组转换为字符串,并在Command executor类中使用。 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. 我提到的另一个问题是,在对此套接字发出一个请求之前,bukkit服务器没有完全加载。 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. 首先,您不应该在主线程上运行类似的套接字,通常应该使用Bukkit调度程序在异步任务上运行此套接字。

Then once you open the socket you should create a while loop to continuously poll for a connection and handle the incoming data. 然后,一旦打开套接字,就应该创建一个while循环,以不断轮询连接并处理传入的数据。 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. 有关更多信息,请参见此网页

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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