after closing serversocket, process remains
Hi everyone,
I'm trying to learn to write a client/server program. I begin by creating a serversocket and waiting for data. When I close the program, the serversocket still remains even when I create handlers that try to close the socket after the GUI has been closed.
The server is as follows:
try
{
ServerSocket myServerSocket =new ServerSocket(6666);
myServerSocket.setReuseAddress(true);
writeToDebugger("Server successfully created. Listening on port 6666\n");
//open a socket for listening for multiple requests
while(listening)
{
clientSocket = myServerSocket.accept();
//clientSocket.setReuseAddress(true);
writeToDebugger("Client " + clientSocket.getInetAddress().toString() +" accepted.");
//store clientSocket information
out =new PrintWriter(
clientSocket.getOutputStream(),true);
in =new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
while ((inputLine = in.readLine()) !=null && inputLine !="")
{
writeToDebugger(inputLine);
}
//breaker = inputLine.indexOf(',');
//store the incoming request's information username of requestor is first param, requested username is second param
//database.put(inputLine.substring(0, breaker), clientSocket.getInetAddress());
//return the IP address of the username lookup
//out.print(database.get(inputLine.substring(breaker+1)));
}
}
Here is my closing handler:
publicvoid windowClosed(WindowEvent arg0){
listening=false;
try
{
System.out.println("CLOSING FOR REAL");
if(clientSocket!=null)
{
clientSocket.close();
clientSocket.shutdownInput();
clientSocket.shutdownOutput();
}
if(myServerSocket!=null)
myServerSocket.close();
if(out!=null)
{
out.flush();
out.close();
}
if(in!=null)
in.close();
System.out.println("DONE CLOSING");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
If someone could please tell me why I have to go into the task manager to close the process after I have already closed the GUI, I'd really appreciate it.
Thanks in advance,
Julian

