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

[3913 byte] By [jboolean23a] at [2007-11-27 1:32:55]
# 1
Even when all the windows are closed the GUI applications leave the event dispatch thread running and as a result the process is still active.In GUI application we normally invoke System.exit to exit the process. Or you have to shutdown all running threads.
LRMKa at 2007-7-12 0:38:04 > top of Java-index,Core,Core APIs...
# 2

And you're doing all these closes in the wrong order, and redundantly.

Just close 'out' and the ServerSocket, doesn't matter in what order here.

All the other closes and shutdowns are redundant. Calling shutdownXXX after closing the socket does nothing (and calling them just before closing the socket would add nothing to what close() already does). Closing the client socket before closing the output stream built around the socket output stream denies the output stream the chance to flush.

ejpa at 2007-7-12 0:38:04 > top of Java-index,Core,Core APIs...