I'm studying to use ServerSocket
and get an error when trying to close serverSocket
object while method accept()
is working. Next I found solution with setSoTimeout()
method. But I think ignore exception isn't best practice.
So, here's my two classes:
class Server:
public class Server {
public static final int PORT = 8777;
private ServerSocket serverSocket;
private boolean serverRuns;
Server() {
try {
serverSocket = new ServerSocket(PORT);
serverRuns = true;
(new Control(this)).start(); // Takes commands while working
while (serverRuns) {
try {
serverSocket.setSoTimeout(1000);
Socket clientSocket = serverSocket.accept();
}
catch (SocketTimeoutException e) {
// cap
}
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
serverSocket.close();
System.out.println("Server stopped");
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void stopServer() {
serverRuns = false;
}
public boolean isServerRuns() {
return serverRuns;
}
public static void main(String[] Args) {
Server s = new Server();
}
}
class Control:
public class Control extends Thread {
private Server activeServer;
private Control() {}
Control(Server activeServer) {
this.activeServer = activeServer;
}
@Override
public void run() {
Scanner keyboard = new Scanner(System.in);
while (activeServer.isServerRuns()) {
String key = keyboard.nextLine().toLowerCase();
switch (key) {
case "close":
activeServer.stopServer();
break;
}
}
}
}
Is it correct way to terminate ServerSocket
work(ignore exception and check serverRuns
variable once a second)? Any recommendations are welcome
You need to call the server close from an another Thread, cause
serverSocket.accept();
will be blockingTake a look at: Interrupt accept method and close the server