Reputation: 137
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?
Upvotes: 1
Views: 423
Reputation: 7326
Use a boolean flag to represent whether it should break or not:
private volatile boolean stop = false;
Then make a method called something like kill (can't be stop) which sets the flag to true, then closes your ServerSocket:
public voic kill() {
stop = true;
serverSocket.close();
}
Then, almost done here, in the line of code which accepts your ServerSocket into a Socket, in the catch block, have it like this:
try {
//accept server socket
} catch(IOException e) {
if(stop)
return;
e.printStackTrace();
}
So it won't print an error when you close the ServerSocket, but rather simply stop trying to accept connections. One last thing, change your while loop to be:
while(!stop) { ...
Just to make sure it exits properly.
Actually I've got a server running on my computer off Java, which uses the following code:
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();
}
}
Upvotes: 2