I try to write a simple chat software. But I am new socket programming and therefore I have a question:
When a chat client sends "goodby" to the server the server should close the client's connection and release all ressources. But after I close the socket the "isConnected()" flag still shows "true".
I have found some threads with similar questions but I must admit that I don't understood all the explainations there.
But is seems that socket.isConnected() does not show the current connection state of a a socket ?
Is this right?
So how can the server close a client connection (socket) and release all ressources etc. back to the operating system?
I want to avoid that over time the server will keep "dead" connections\sockets.
Is it enough to just execute "socket.close" ?
My Server code:
class connection extends Thread
{
public Socket client;
public PrintStream output;
private BufferedReader input;
...
...
while(true) //loop where server waits for client's message
{
//****** wait for client's message
line=input.readLine();
//******* Client wants to leave chat - so close its connection , release server ressources, close this thread...
if (line.equals("goodbye"))
{
//close I/O streams
this.input.close();
this.output.flush();
this.output.close();
//Close Socket
client.close(); //<-- results in: .isClosed()==true but : .isConnected()==true
//close thread (not clear if neccessary)
this.stop();
//exit loop / this should also exit and thus close this thread
break;
}
}
Connection. - user1907906