I'm new to java and especially to java networking, but what I'm trying to do is set up a server (for a game). When TWO clients are connected to the server, I want to refuse any other connection. Am I able to close the ServerSocket? And if the ServerSocked is closed, those two connections which has been ran as threads and stored in a collection are still alive and able to cummunicate with the server? I hope you've got my point.
Here's the source code:
//Server
public synchronized List<ClientThread> getClients(){
return clients;
}
public void run() {
try {
while(true){
Socket clsock = srvsock.accept();
if(getClients().size() == 2){
System.out.println("Too many connections!");
clsock.close();
continue;
}
ClientThread clt = new ClientThread(clsock);
clients.add(clt);
clt.start();
System.out.println("Connection accepted.");
}
} catch (IOException ex) {
ex.printStackTrace(System.err);
}
}
And with this code, I'm not able to detect on the Client if the connection is still alive, or the server has closed the connection. Thanks in advance.