🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: newbie Sockets and Threading

#8564
From: Nabin Thomas
Date: Wed, 14 Mar 2001 08:58
161 lines
5307 bytes
This is a multi-part message in MIME format.
--------------D9B31D5E92FBA22D8CDAF056
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit




Mozy 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?

Because the loop in start listening is excecutng continuosly. The loop
exits
only when the next iteration starts after
the listening variable has tha value false.
The accept() call blocks the thread waiting for a connection request.
You can
insert a statement that interrupts this blocking in the stoplistening()
method
to stop the Server() but still the accepted connections will be serviced
as
theey are in different threads.

Or another simple method would be to set a scoket timeout for connection
Use the Sockect.setSoTimeOut(timeInMillisec); method before entering the
loop.
Now the statement inside the loop may throw an Exception if a timeout
occurs.
Catch them and discard them appropriately.

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);
    serverSocket.setSoTimeOut(1000);

    while (listening){
        try{
            Socket s=serverSocket.accept();
            new SrvrListenerThread(s).start();
        }catch(Exception e){ //
           //discard the timeout error
        }

    serverSocket.close();
  }

  public void StopListening()
  {
    listening = false;
  }
}

The code above will terminate the server within 1000ms of calling
StopListening

> 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)?

The above code works without much modification. Remember not to put any
System.exit() anywhere as it would cause all the threads to stop.


> Along the
> same lines, what is the best way to limit the number of threads spawned?

Use a static variable numConnections to keep track of the number of
active
connections inside the SrvrListenerThread.
If the number is greater than the maximum permitted no of connections
throw an
exception so that the Connection is refused.

OR

modify the loop so that it enters the socket.accept() only if no of
active
connections is less than maximum permitted.
Other wise use a semaphore Object to wait till the active threads are
complete.
( Remember to put a semaphore.notify() at the last line of the run()
method in
SrvrListenerThread.


>
> Any suggestions are greatly appreciated.
>
> TIA
>
> mozy
--------------D9B31D5E92FBA22D8CDAF056
Content-Type: text/x-vcard; charset=us-ascii;
 name="viewmycard.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Nabin Thomas
Content-Disposition: attachment;
 filename="viewmycard.vcf"

begin:vcard 
n:Thomas;Nabin
x-mozilla-html:TRUE
adr:;;;;;;
version:2.1
email;internet:DONOTSPAMME.nabinthomas@REMOVETHIShotmail.comANDTHISTOO
note;quoted-printable:REMOVE the Capitalised letters in my email ID given in this card=0D=0A=0D=0APlease remember to avoid spam.=0D=0A=0D=0AFeel free to contact me trhu email if required but DONOT SPAM=0D=0A=0D=0A
fn:Nabin Thomas
end:vcard

--------------D9B31D5E92FBA22D8CDAF056--
Technical Details
Message-ID: <3AAF32B9.3D1D812D@foremail.nospamplease>
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!isdnet!fr.clara.net!heighliner.fr.clara.net!newsfeed01.sul.t-online.de!t-online.de!bornews.borland.com!not-for-mail
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-14 02:18:39 PST
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!isdnet!fr.clara.net!heighliner.fr.clara.net!newsfeed01.sul.t-online.de!t-online.de!bornews.borland.com!not-for-mail
From: Nabin Thomas <viewmycard@foremail.nospamplease>
Newsgroups: borland.public.jbuilder.java.language
Subject: Re: newbie Sockets and Threading
Date: Wed, 14 Mar 2001 14:28:33 +0530
Organization: Another Netscape Collabra Server User
Lines: 164
Message-ID: <3AAF32B9.3D1D812D@foremail.nospamplease>
References: <3aacf02b_1@dnews>
NNTP-Posting-Host: 202.86.153.223
Mime-Version: 1.0
Content-Type: multipart/mixed;
 boundary="------------D9B31D5E92FBA22D8CDAF056"
X-Mailer: Mozilla 4.76 [en] (Win98; U)
X-Accept-Language: en