I've got a program I'm working on where the main thread initializes a listener thread to listen for incoming connections to a 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();
}
}
then waits for input from the server admin (in cntrl.getInput()). That's all well and good, but the main function I'm trying to implement from the getInput is "halt" which should check that there are no connections, and then stop the listener and itself.
Here's the listener thread:
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");
}
}
}
}
and the haltServer function:
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;
}
I'm aware that stop is depreciated, and looked in to using interupted() as a flag, but I don't think that will work due to the serverSocket.accept() call.
Ideas?