2

How does a web-server serve its client using the same port(80) for a TCP connection. For a UDP connection, i understand that there is no connection, per se, so we can have multiple clients send packets to same port. If i try to use an already used port on my localhost, i get BindException.

One solution i see to this is starting a thread for each connection, but wouldnt this be cumbersome for site like google/yahoo where there a >100000 connections in each server?

What solutions do web servers employ for this problem?

Chander Shivdasani
  • 9,878
  • 20
  • 76
  • 107

3 Answers3

3

Server listens on a well-known port (80) and delegate the request to a worker socket once it receive the request. That way it can serve the next request. You can write your own simple server to understand whats going on. Oracle site has a nice example code. [1]

[1] http://java.sun.com/developer/technicalArticles/Networking/Webserver/WebServer.java

first it creates a server socket;

ServerSocket ss = new ServerSocket(port);

then it listnes on the specified port and create a new socket once it accepts the request;

Socket s = ss.accept();

As shown in the code, it has a worker thread pool, so at a given moment you can control the number of request get served by the server at a given time. Others wait in a Queue may be.

2

You only have one port for listening, but a connection has two ports, one on each side of the connection. This pare must be unique.

So, say you connect to google.com port 80, then your connection will have some port on your machine, say 42312 and port 80 at google.com. You can see your connections with netstat -a. To get a shorter list: netstat -an| grep ESTABLISHED" Which shows all established connections without resolving their IPs to names.

Roger Lindsjö
  • 11,330
  • 1
  • 42
  • 53
1

AFAIK, Apache will start a new thread for every request, which is a big reason that event driven servers like Node.js are a little faster. Google and Yahoo also have TONS of servers and spread this large processing load among them. What Roger says also makes sense, although I'm not 100% sure on the details of how exactly google doing output on port 42312 would reach your computer at port 80 :P

Community
  • 1
  • 1
SuperTron
  • 4,203
  • 6
  • 35
  • 62
  • I don't think I said google would do output on port 42312 which would reach port 80n on the client. I said the opposite. So I (being the client) would sen packets with source port 42312, destination port 80. Google (when replying) would of course send with source 80 and destination 42312. – Roger Lindsjö Dec 17 '11 at 11:44
  • Ah makes sense, misunderstood. My mistake. – SuperTron Dec 17 '11 at 22:22