> > I want to create a ServerSocket.
> >
[quoted text clipped - 12 lines]
> You could close the ServerSocket until you were ready to accept
> connections again.

Signature
Curt Welch http://CurtWelch.Com/
curt@kcwc.com http://NewsReader.Com/
> If you wanted to accept multiple connection but not until you you were done
> with the first one,
I like Knute's option, though, because it doesn't require me to accept
connections. In my opinion, it's somewhat cleaner than accepting-then-dropping.
So if I want to allow X connections at a time, I maintain a counter and
call ServerSocket.close() when I reach X.

Signature
Steve Sobol, Victorville, CA PGP:0xE3AE35ED www.SteveSobol.com
Geek-for-hire. Details: http://www.linkedin.com/in/stevesobol
Curt Welch - 12 Nov 2007 03:36 GMT
> > If you wanted to accept multiple connection but not until you you were
> > done with the first one,
[quoted text clipped - 5 lines]
> So if I want to allow X connections at a time, I maintain a counter and
> call ServerSocket.close() when I reach X.
It all depends on the protocol and the application really. If it's normal
for the server to accept multiple connections but be limited by the number
it can handle, the more typical way to do it is accept the connection, and
report an error back to the client using a protocol specific message over
the connection and then close. This way, the client can tell the
difference between an overloaded server and a down server. Servers
normally, once started, never stop accepting connections. It's very odd
for a long lived server to stop listening for new connections.
It's normally not allowed for two servers to listen on the same port. If
one server is already running, the second will get an error saying it can't
bind to the port. So keeping the socket open is a simple way of preventing
two servers from running at the same time on the same port. If you open
and close the port as you are ready to accept connections, you could get
the possibility that multiple servers are running, and fighting to see who
can use the port next.
On the other hand, if you are writing a special application which is
designed to accept only a single connection on a temporary port, then the
way you are doing it wouldn't be that odd.
It all depends on the application.

Signature
Curt Welch http://CurtWelch.Com/
curt@kcwc.com http://NewsReader.Com/