Alla chiusura di un client viene segnalato l'errore "Socket closed" che, da come ho letto, indica che continua ad esserci una richiesta anche dopo la chiusura del socket.
Quello che ho notato però, è che l'errore si presenta solo usando un interrupt() mentre usando il metodo deprecato stop() non c'è nessun errore. Come posso modificare il codice?
Codice: Seleziona tutto
// metodo che rimuove il client
public synchronized void remove(int ID){
// posizione del thread
int pos = findClient(ID);
if (pos >= 0){
ServerThread toTerminate = clients[pos];
ui.TextArea1.append("\nRimozione client thread " + ID);
if (pos < clientCount-1){
for (int i = pos+1; i < clientCount; i++){
clients[i-1] = clients[i];
}
}
clientCount--;
try{
toTerminate.close();
}
catch(IOException ioe){
ui.TextArea1.append("\nErrore nella chiusura del thread: " + ioe);
}
toTerminate.stop(); //************ERRORE***************************************++
}
}
Codice: Seleziona tutto
// metodo che ricerca un client attraverso il suo ID
private int findClient(int ID){
for (int i = 0; i < clientCount; i++){
if (clients[i].getID() == ID){
// viene ritornata la posizione del thread nell'array
return i;
}
}
return -1;
}
Codice: Seleziona tutto
public void close() throws IOException {
if (socket != null)
socket.close();
if (streamIn != null)
streamIn.close();
if (streamOut != null)
streamOut.close();
}
Codice: Seleziona tutto
@Override
public void run(){
ui.TextArea1.append("\nServer Thread " + ID + " avviato.");
while (true){
try{
Message msg = (Message) streamIn.readObject();
System.out.println("Messaggio ricevuto"+msg.toString());
server.handle(ID, msg);
}
catch(IOException | ClassNotFoundException ioe){
System.out.println(ID + " ERROR reading: " + ioe.getMessage());
server.remove(ID);
interrupt();
}
}
}