Article View: borland.public.jbuilder.java.language
Article #8622newbie Sockets and Threading
From: Mozy
Date: Mon, 12 Mar 2001 15:50
Date: Mon, 12 Mar 2001 15:50
46 lines
2109 bytes
2109 bytes
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:
<3aacf02b_1@dnews>Path:
arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!cyclone2.usenetserver.com!news-out.usenetserver.com!sunqbc.risq.qc.ca!nntp1.njy.teleglobe.net!teleglobe.net!hub.org!hub.org!PeerFeed.KWeb.on.ca!bornews.borland.com!dnews!206.222.62.236Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,a2ff05ab680e7e9d,start X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-03-12 08:02:18 PST From: "Mozy" <mozybth@covad.net> Newsgroups: borland.public.jbuilder.java.language Subject: newbie Sockets and Threading Date: Mon, 12 Mar 2001 09:50:03 -0600 Lines: 49 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 NNTP-Posting-Host: 206.222.62.236 Message-ID: <3aacf02b_1@dnews> X-Trace: 12 Mar 2001 07:50:03 -0800, 206.222.62.236 Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!cyclone2.usenetserver.com!news-out.usenetserver.com!sunqbc.risq.qc.ca!nntp1.njy.teleglobe.net!teleglobe.net!hub.org!hub.org!PeerFeed.KWeb.on.ca!bornews.borland.com!dnews!206.222.62.236