🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #8539

Re: newbie Sockets and Threading

#8539
From: Torsten Welches
Date: Tue, 13 Mar 2001 07:08
65 lines
2652 bytes
Mozy,

serverSocket.accept() blocks until the next connection is made.
After setting listening to false your SrvrListener would still 
accept *one* more connection and then stop. Current threads are
untouched and keep running. So it already does a "graceful close 
down". 

>what is the best way to limit the number of threads spawned?

A simple counter would do I guess. 

Torsten

"Mozy" <mozybth@covad.net> wrote:
>Hi all,
>
>I'm having some problems understanding how sockets work and possibly threads
>as well. Most of this code was taken from the tutorial on sockets from the
>javasoft web site. I have this class:
>
>public class SrvrListener
>{
>  private static boolean listening;
>  private ServerSocket serverSocket = null;
>
>  public void StartListening() throws IOException
>  {
>    StartListening(5555);
>  }
>
>  public void StartListening(int port) throws IOException
>  {
>
>    listening = true ;
>
>    serverSocket = new ServerSocket(port);
>
>    while (listening)
>        new SrvrListenerThread(serverSocket.accept()).start();
>
>    serverSocket.close();
>  }
>
>  public void StopListening()
>  {
>    listening = false;
>  }
>}
>
>I have a couple of questions here. Why doesn't a call to StopListening()
>cause the sever to stop? Is it because the server socket won't close if it
>still has open connections with a client?? What would be the best way to
>stop accepting new connections while at the same time allowing any current
>threads to finish processing (in effect a graceful close down)?  Along the
>same lines, what is the best way to limit the number of threads spawned?
>
>Any suggestions are greatly appreciated.
>
>TIA
>
>mozy
>
>
Technical Details
Message-ID: <3aadc75b$1_1@dnews>
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!logbridge.uoregon.edu!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!bornews.borland.com!dnews!207.105.83.65
References: <3aacf02b_1@dnews>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,a2ff05ab680e7e9d
X-Google-Attributes: gidf74b3,public
X-Google-ArrivalTime: 2001-03-12 23:16:04 PST
From: "Torsten Welches" <torsten.welches@who.cares.netcologne.de>
Sender: "Torsten Welches" <torsten.welches@who.cares.netcologne.de>
Reply-To: "Torsten Welches" <torsten.welches@who.cares.netcologne.de>
Subject: Re: newbie Sockets and Threading
Newsgroups: borland.public.jbuilder.java.language
X-User-Info: 193.158.55.74 193.158.55.74    
References:  <3aacf02b_1@dnews> 
NNTP-Posting-Host: 207.105.83.65
Message-ID: <3aadc75b$1_1@dnews>
Date: 12 Mar 2001 23:08:11 -0800
X-Trace: 12 Mar 2001 23:08:11 -0800, 207.105.83.65
Lines: 66
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!logbridge.uoregon.edu!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!bornews.borland.com!dnews!207.105.83.65