Hi All Excuse my apparent misunderstanding of this topic, I would just like to be totally clear on something.
accept() .. blocks, waiting.. a connection comes in and is passed off to a new port number.. the ServerSocket then goes back and continues to wait for connections on the original port... another connection comes in.. this is passed of to another port number and once again ServerSocket returns to listening for connections on the original port..
Now we have two simultanious connections, each on the port provided internally by accept().. so in effect, up to this point accept acts a little like it is multi-threaded by handling multiple connections..The reason we actually start a new thread to handle connections is so each connection can access resources on the server independently etc,... in essence then ... if all we wanted to do was connect without accessing resources(pointless I Know but I am just clarifying) accept() would continue to pass off connections until we had no more ports... so it is the use of the resources and not the multiple connections that is the reason we run new threads to handle connections....
Is this just about right?...
Sorry if I this comes over as obvious, but I just want to be solid in my understanding.. many thanks in advance of any help.
Alex :roll: [ June 06, 2005: Message edited by: Alex Joyce ]
Without additional threads the server could only process one request at a time, so request n+1 could not start until request n is finished. If all the requets are lightning fast, the server might keep up. But if they take any time at all *and* they do something that blocks that JVM for a while, like reading or writing files or maybe even the request stream, then threads let the JVM work on two requests at once.
Does that part makes sense?
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Feb 17, 2005
Thanks Stan... I understand completely now... Apologies for appearing daft on this... you know.. when you just want to make something crystal clear... well now it is.. cheers
Joined: Jan 29, 2003
Glad to help.
You had another thing going on in your question about exhausting all the "ephemeral" ports that the conversation switches to after the accept. There was another post recently with a link into some TCP/IP details on those ports. The ranges and quantity vary by OS and configuration, but there are a lot of them. When the server closes the socket it got from accept() the port is available for reuse. To use them all up the server would have to hang onto them forever. That would be an interesting defect, but in normal use the server can only handle so many simultaneous requests so it only holds a limited number open at a time and releases them fairly soon. I think we have IBM HTTP Server set to accept a max of 4096 simultaneous requests. Wow, that sounds like a lot now that I said it!