File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes How webserver handle multiple requests on a single port? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "How webserver handle multiple requests on a single port?" Watch "How webserver handle multiple requests on a single port?" New topic
Author

How webserver handle multiple requests on a single port?

Raj Bansilal Champaneriya
Ranch Hand

Joined: Dec 17, 2011
Posts: 36

Hello Ranchers,

When I was thinking of web development, I came across with a question in my mind that how a web server handle multiple requests on a single port?

According to me there might be some threading logic but I want to know this mechanism in detail so Ranchers help me to get my doubt clear.

Thanks
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

What do you mean by multiple requests on a single port? If a client with some IP address connects to a server on a specific port, a socket is created. If the same IP address does this again, it will fail because the socket already exists.

If another IP address connects to the same port, it will create a different socket. But this is no different from handling separate requests on different ports.
Raj Bansilal Champaneriya
Ranch Hand

Joined: Dec 17, 2011
Posts: 36

Thanks for your interest,

I want to know how server serves multiple requests simultaneously?I don't know any thing about it. I know we are connection to a server via its ip address and port number that's it.
So i thought there is only one port and many request come to that port only via different clients so how server manages all the requests?

This is all I want to know. If you explain this concept in detail it would be very helpful. Thanks any way.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40


Not threads related. Moving to the networking forum.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Raj Bansilal Champaneriya wrote:
I want to know how server serves multiple requests simultaneously?I don't know any thing about it. I know we are connection to a server via its ip address and port number that's it.
So i thought there is only one port and many request come to that port only via different clients so how server manages all the requests?

This is all I want to know. If you explain this concept in detail it would be very helpful. Thanks any way.



There is only one accept port, but there are many ephemeral ports, when using TCP... basically, the port used by the server socket is only used to accept connections. A different port is used for each data connection.

Henry
khushbu bothra
Greenhorn

Joined: Dec 31, 2011
Posts: 2
Hey I think it is related to three way handshaking of tcp/Ip you should refer that if you find answer let me know ... thanks in advance...
Rohan Dhapodkar
Greenhorn

Joined: Jun 27, 2011
Posts: 23
Using NIO features ( like ServerSocketChannel, Selector) introduced in java 1.4 you can handle multiple requests ( I think this can be done by ServerSocket as well, but not tested ).

I created sample program which accepts multiple requests, new thread is assigned per request( this can be optimised using ThreadPool), Thread just sleeps for 10 seconds (just to demo that i can handle multiple requests).



Stephan van Hulst wrote:If a client with some IP address connects to a server on a specific port, a socket is created. If the same IP address does this again, it will fail because the socket already exists.

If another IP address connects to the same port, it will create a different socket. But this is no different from handling separate requests on different ports.

I even tried to test what Stephen already quoted. I initially opened multiple tabs in firefox everyone pointing to http://localhost:8085/test and surprised, firefox normally opens one http connection at time. I then tried to use telnet localhost 8085 and found than I can open 2 simultaneous telnet connection and nothing is blocked.
I suspected about firefox behaviour and then i just modified url in every tab to include request param ( just to make sure every tab will submit different http requests) eg, http://localhost:8085/test?1...12 . I opened 12 tabs everyone with different parameters and i found that firefox use to create 6 parallel connections and after closing first connection it submits next connection (but max 6 at a time).

Look likes for same url, instead of creating multiple http connection, browser used to tunnel http requests on same connection and max 6 can be the limitation set by browser for any host/site.
I even verified this limitation of 6 by creating single html page with 6 iframes and hit at same time from both firefox and chrome browser. I can see now 12 active connections in tcpmonitor tool (i used tcpmonitor here to track timing and request and response).
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Stephan van Hulst wrote:If a client with some IP address connects to a server on a specific port, a socket is created. If the same IP address does this again, it will fail because the socket already exists.



No, it will succeed, as long as the client either lets the OS provide an ephemeral port (the usual case) or explicitly specifies a different local port. A TCP connection consists of 4 pieces: client address, client port, server address, server port. You can have multiple concurrent connections between the same client and server.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Raj Bansilal Champaneriya wrote:Hello Ranchers,

When I was thinking of web development, I came across with a question in my mind that how a web server handle multiple requests on a single port?

According to me there might be some threading logic but I want to know this mechanism in detail so Ranchers help me to get my doubt clear.

Thanks


Yes, the server typically has multiple threads. A naive approach would be to start a new thread for each request, but that won't scale. A more common approach would be a thread pool. Note, however, that since HTTP is a stateless protocol, a low-volume web server might not need multiple threads. I doubt any server is written that way though, other than possibly small embedded ones or class projects.
 
GeeCON Prague 2014
 
subject: How webserver handle multiple requests on a single port?