Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

Refusing TCP connections - ServerSocket

Thread view: 
Steve Sobol - 11 Nov 2007 23:49 GMT
I want to create a ServerSocket.

I want it to accept one connection.

If another connection request comes in, I want it to be refused, not queued.
Even if I set backlog to 1, on the OS I'm testing on (Linux) I'm able to
queue up a bunch more connections (5 or 6).

I know the actual backlog size is OS dependent even if I explicitly set it
in the ServerSocket constructor.

So, how do I refuse all subsequent connections after I accept that first one?

Signature

Steve Sobol, Victorville, CA     PGP:0xE3AE35ED  www.SteveSobol.com
Geek-for-hire. Details: http://www.linkedin.com/in/stevesobol

Knute Johnson - 12 Nov 2007 00:07 GMT
> I want to create a ServerSocket.
>
[quoted text clipped - 8 lines]
>
> So, how do I refuse all subsequent connections after I accept that first one?

You could close the ServerSocket until you were ready to accept
connections again.
Steve Sobol - 12 Nov 2007 01:01 GMT
I asked

>> how do I refuse all subsequent connections after I accept that first one?

Knute Johnson suggested

> You could close the ServerSocket until you were ready to accept
> connections again.

And that works wonderfully. Thanks!

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 01:17 GMT
> > I want to create a ServerSocket.
> >
[quoted text clipped - 12 lines]
> You could close the ServerSocket until you were ready to accept
> connections again.

If you wanted to accept multiple connection but not until you you were done
with the first one, the other option is to accept all connections and just
close the connection after accepting it.  It depends on the protocol
whether that would make sense or whether that would get you in trouble
(like if that just made the clients try to connect again).  But if you want
to accept a single connection, and no more, then Knute is correct, just
close the ServerConnection after you accept the first connection.  Closing
the ServerSocket will not effect the socket you just accepted.  You can
still go ahead and use it for whatever was needed.

Signature

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

Steve Sobol - 12 Nov 2007 01:48 GMT
> 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/

Martin Gregorie - 12 Nov 2007 12:47 GMT
> I want to create a ServerSocket.
>
[quoted text clipped - 3 lines]
> Even if I set backlog to 1, on the OS I'm testing on (Linux) I'm able to
> queue up a bunch more connections (5 or 6).

Are you limiting connections because your server instance can't handle
multiple connections, but running several servers would be OK or because
 you only want a single connection per host?

Either way you should consider writing your server to run under the
control of xinetd if its intended to run on a *NIX type system. The
benefits are that your server doesn't need to handle sockets (xinetd
does that for it) and your service definition says how many copies
xinetd is allowed to start.

Signature

martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.