• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How webserver handle multiple requests on a single port?

 
Raj Bansilal Champaneriya
Ranch Hand
Posts: 36
Java Linux MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5590
55
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
Java Linux MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21008
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Not threads related. Moving to the networking forum.

 
Henry Wong
author
Marshal
Pie
Posts: 21008
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic