0

我有一个 IRC 机器人,它在给定一些参数的情况下托管游戏服务器。问题是,一旦它托管了一个服务器,它就会停止侦听 IRC(实际上,一次只能托管一个服务器)。这不是我想要的。

我认为线程将是我的问题的答案,但我似乎无法让它工作。看来它实际上并没有在另一个线程中开始?

这是我的主类,它通过线程启动并运行该方法:

// Everything is okay, run the server.
Runnable r = new Server(this, channel);
Thread thread = new Thread(r);
thread.start();

这是(大概)控制线程的 Server 类:

public class Server extends PircBot implements Runnable  {

public void run() {

}

public Server (bot BotRun, String channel) {
    String names[] = org.bestever.bebot.bot.hostbuilder.split(" ");
    ProcessBuilder pb = new ProcessBuilder(names);
    pb.redirectErrorStream(true);
    try {
        Process proc = pb.start();
        BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        String strLine = null;
        while((strLine = br.readLine()) != null) {
            // Returns UDP Initialized if the server was successfully started
            if (strLine.equalsIgnoreCase("UDP Initialized.")) {
                BotRun.sendMessage(channel, "Server started successfully.");
            }
            // Returns Bad Hex Number if there is a problem with the WAD file
            else if (strLine.startsWith("Bad hex number")) {
                BotRun.sendMessage(channel, "Error starting server: "+strLine);
            }
            System.out.println(strLine);
        }
        Thread.currentThread().interrupt();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

我实际上没有在线程中启动它吗?谢谢你的帮助!

4

2 回答 2

3

恐怕没有。

服务器类应该更像:

public class Server extends PircBot implements Runnable {

    private bot BotRun;
    private String channel;
    public void run() {
        String names[] = org.bestever.bebot.bot.hostbuilder.split(" ");
        ProcessBuilder pb = new ProcessBuilder(names);
        pb.redirectErrorStream(true);
        try {
            Process proc = pb.start();
            Reader reader = new InputStreamReader(proc.getInputStream());
            BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String strLine = null;
            while((strLine = br.readLine()) != null) {
                // Returns UDP Initialized if the server was successfully started
                if (strLine.equalsIgnoreCase("UDP Initialized.")) {
                    BotRun.sendMessage(channel, "Server started successfully.");
                }
                // Returns Bad Hex Number if there is a problem with the WAD file
                else if (strLine.startsWith("Bad hex number")) {
                    BotRun.sendMessage(channel, "Error starting server: "+strLine);
                }
                System.out.println(strLine);
            }
            reader.close();
            Thread.currentThread().interrupt();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Server (bot BotRun, String channel) {
        this.BotRun = BotRun;
        this.channel = channel;
    }
}
于 2012-08-11T00:22:48.467 回答
2

你的run()方法是空的;它开始,什么都不做,然后结束。

于 2012-08-11T00:22:53.310 回答