0

我有一个正在处理的程序,主线程在其中初始化一个侦听器线程以侦听与 serverSocket 的传入连接

    /*
     * run - this will set up the server so that it is ready to listen.
     */
    public void run( serverVariables servVar, storageManager writer, storageVariables write, CoreVariables coreVar, serverFunctions serv, SQLFunctions SQL, SQLvariables SQLvar, netFunctions conn, netVariables netVar) {
        ControlFunctions cntrl = new ControlFunctions();
        util.doCommand("ifconfig eth0 " + servVar.ip);
        ListenerThread listen = new ListenerThread(servVar, coreVar, writer, serv, write, SQL, SQLvar, util);
        try {
            servVar.servSocket = new ServerSocket(servVar.port);
            listen.start();
            cntrl.getInput(util, clr.YELLOW, SQL, SQLvar, listen, conn, netVar);
        } catch (IOException e) {
            System.out.println("Could not read input stream");
            e.printStackTrace();
        }
    }

然后等待来自服务器管理员的输入(在 cntrl.getInput() 中)。这一切都很好,但是我试图从 getInput 实现的主要功能是“halt”,它应该检查没有连接,然后停止侦听器及其本身。

这是侦听器线程:

   package server;

import java.io.IOException;

import net.netFunctions;
import net.netVariables;
import core.CoreVariables;
import utl.utilFunctions;
import utl.color;
import server.serverVariables;
import store.storageManager;
import store.storageVariables;
import conn.SQLFunctions;
import conn.SQLvariables;

public class ListenerThread extends Thread {
    color clr = new color();
    CoreVariables coreVar = new CoreVariables();
    utilFunctions util = new utilFunctions();
    netVariables netVar = new netVariables();
    storageManager writer = new storageManager();
    netFunctions conn = new netFunctions();
    storageVariables write = new storageVariables();
    serverFunctions serv = new serverFunctions();
    serverVariables servVar = new serverVariables();
    SQLFunctions SQL = new SQLFunctions();
    SQLvariables SQLvar = new SQLvariables();
    message msg = new message();

    public ListenerThread(serverVariables servVar, CoreVariables coreVar,
            storageManager writer, serverFunctions serv,
            storageVariables write, SQLFunctions SQL, SQLvariables SQLvar,
            utilFunctions util) {
        super("ListenerThread");
        this.coreVar = coreVar;
        this.util = util;
        this.writer = writer;
        this.write = write;
        this.serv = serv;
        this.servVar = servVar;
        this.SQL = SQL;
        this.SQLvar = SQLvar;
    }

    public void run() {
        util.outColored("Listener thread started", clr.CYAN);
        while (true) {
            try {
                new ConnectionThread(clr.GREEN, servVar.servSocket.accept(),
                        coreVar, util, writer, write, serv, servVar, SQL,
                        SQLvar).start();
            } catch (IOException e) {
                System.out.println("Failed to accept");
            }
        }
    }
}

和 haltServer 功能:

private boolean haltServer(SQLFunctions SQL, SQLvariables SQLvar, utilFunctions util, String color, ListenerThread listen, netFunctions conn, netVariables netVar) {
    Socket s = null;
    boolean success = false;
    String result = null;
    int count = 0;
    //check for open connections
    SQL.doQuery("SELECT * FROM Clients WHERE Status != 0", SQLvar);
    try {
        while(SQLvar.resultSet.next()) {
            result = SQLvar.resultSet.getString("ClientID");
            util.outColored("Client " + result + " is still connected", color);
            count++;
        }
        if(count == 0) {
            listen.stop();
            success = true;
        }
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return success;
}

我知道 stop 已贬值,并考虑使用 interupted() 作为标志,但由于 serverSocket.accept() 调用,我认为这不会起作用。

想法?

4

1 回答 1

2

使用布尔标志来表示它是否应该中断:

private volatile boolean stop = false;

然后创建一个名为 kill(不能停止)之类的方法,将标志设置为 true,然后关闭您的 ServerSocket:

public voic kill() {
    stop = true;
    serverSocket.close();
}

然后,在这里几乎完成了,在将您的 ServerSocket 接受为 Socket 的代码行中,在 catch 块中,如下所示:

try {
   //accept server socket
} catch(IOException e) {
    if(stop)
        return;
    e.printStackTrace();
}

因此,当您关闭 ServerSocket 时它不会打印错误,而只是停止尝试接受连接。最后一件事,将您的 while 循环更改为:

while(!stop) { ...

只是为了确保它正确退出。

实际上我有一个服务器在我的计算机上运行,​​它使用以下代码:

    threadHandler = new ThreadPoolExecutor( maxThreads, maxThreads, 1, TimeUnit.MINUTES,
            new ArrayBlockingQueue<Runnable>( maxThreads, true ) );
    while (!serverTerminated) {
        try {
            final Socket connectionSocket = serversocket.accept();
            SocketHandler handler = new SocketHandler( this, connectionSocket );
            threadHandler.submit( handler );
        } catch (IOException e) {
            if (!serverTerminated)
                e.printStackTrace();
        }
    }
于 2012-10-03T20:02:27.313 回答