簡體   English   中英

bukkit插件內的套接字在使用后關閉

[英]Socket inside bukkit plugin closes after use

我正在嘗試在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服務器沒有完全加載。 謝謝您的幫助。

首先,您不應該在主線程上運行類似的套接字,通常應該使用Bukkit調度程序在異步任務上運行此套接字。

然后,一旦打開套接字,就應該創建一個while循環,以不斷輪詢連接並處理傳入的數據。 相反,您要做的是打開套接字,讀取一條線,然后斷開連接。

您想做類似的事情

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

有關更多信息,請參見此網頁

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM