Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Socket Accept and Backlog

843790Jul 5 2008 — edited Jul 6 2008
Hello All,

Recently I have been working on a project that requires me to develop a server application for a widely used TCP protocol. During my coding, I discovered a somewhat interesting issue that I first thought to be a rather annoying bug. The server that I was creating required me to limit the maximum number of active client connections for various reasons. To my understanding, all that I needed to do was to stop calling Socket.accept() after my maximum number of connections had been reached. Wrong. I guess I made the mistake of thinking that the way Java implemented sockets was the same as standard Berkley sockets. I have since realized that Java's interpretation of backlog is NOT the same as what i am used to. Java completes a TCP handshake even though accept() has not been called and it is actually the COMPLETED connections that are put into the backlog queue. So, even though I am not calling accept, clients still connect (up to my backlog limit) and begin polling my server, to which I do not respond and therefore the clients think something is wrong. Setting the backlog to 0 is obviously no solution as this forces Java to default to a backlog of 50! (contrary to what some texts have suggested; that is that the default OS backlog is assumed).

So, I thought, maybe this is just poor design.... Since then I have come to the conclusion that this may not be the case (I'm sure the folks over at Sun are much smarter than I and would never have made such a design blunder). So, my current line of thinking is that this has been done to prevent DoS attacks from malicious agents who send numerous TCP request packets but never fulfill any intention to establish an actual connection. This means, that to effect a maximum connection limit to my server I must actively close my server socket down until an active connection is terminated at which point I must re-bind to my listening port.

Personally I would rather not release my port bindings to stop incoming connections.....

Are my conclusions correct? Or am I missing something? Is there a cleaner work around, or is this what Sun has intended?

Cheers.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 3 2008
Added on Jul 5 2008
2 comments
1,017 views