tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cmanola...@yahoo.com
Subject RE: ServerSocket not being closed properly.
Date Tue, 24 Apr 2001 14:23:42 GMT
On Tue, 24 Apr 2001, Brad Cox PhD wrote:

> At 7:19 PM -0700 04/23/2001, cmanolache@yahoo.com wrote:
> >On Mon, 23 Apr 2001, Arun Katkere wrote:
> >In general you need to do something to unblock the accept thread, and 3.2
> >doesn't have this in.
> 
> Yes, I remember now. You're absolutely right. You need 
> serverSocket.setSoTimeout() to break the accept periodically. Here's 
> how I did it in my app.

Hi Brad,

Yes, I remember about setSoTimeout() - I never liked that solution. If you
set it to 5 min then stop() may take up to 5 minutes ( since it has to
wait for the accept() socket to timeout). Multiply by 3 sockets...
( well, on average is probably half ).

Anyway - a cleaner solution ( IMHO ) is to just set "stoped" and do a
bogus connection. That's what I did in 3.3 - and seems to work fine.

The module creating the socket can do that easily - and I think it's much
cleaner than periodically breaking accept() ( and gives fast response,
without having to wait for the timeout to stop ) 

Costin

> 
> Notice the comment: apparently I got this from an early version of Tomcat.
> 
>   public void run()
>   {
>    if (serverSocket != null) // original is server
>    {
>     isListening = true;
>     while(isListening)
>     {
>      try
>      {
>       Socket clientSocket = serverSocket.accept();
>       SocketServer clone = (SocketServer)clone();
>       clone.serverSocket = null;
>       clone.clientSocket = clientSocket;
>       clone.thread = new Thread(clone, name+"Clone");
>       clone.thread.start();
>      }
>      catch (InterruptedIOException e)
>      {
>       /**
>        * Ignore periodic SO_TIMEOUT interrupts
>        * Without this the accept call never returns when the serversocket
>        * is closed, and all subsequent runs fail with "port 8082 busy".
>        * I found this in the  Tomcat http server code.
>        */
>      }
>      catch (Throwable e)
>      { e.printStackTrace(); isListening = false; }
>     }
>    }
>    else run(clientSocket);
>   }
>   void startServer(int port) throws UserFault
>   {
>    if (thread == null)
>    {
>     try
>     {
>      serverSocket = new ServerSocket(port);
>      serverSocket.setSoTimeout(WAKEUP_INTERVAL);
>     }
>     catch (IOException e)
>     {
>      throw UserFault.errEnablerPortBusy(port);
>     }
>     thread = new Thread(this, name+"Master");
>     isListening = true;
>     thread.start();
>    }
>   }
> 


Mime
View raw message