Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In a web browser that supports having multiple tabs, such as Firefox, do different tabs that go to different website domains use a dedicated port for each domain?.

Or does the browser use a single port for managing all tabs and therefore all domains?.

share|improve this question
    
Browsers use 2 ports when connecting to websites, 80 is for http connections, 443 is for https connections. en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers – Moab Mar 20 at 20:43
2  
I know the ports used to connectto the server, but I was wondering about the port numbers used to connect from the client (host computer). – yoyo_fun Mar 20 at 20:49
2  
I think that the term, "outgoing ports," is imprecise. Ports are bi-directional. Maybe you could say. "local ports," instead. The local ports are used as source (outgoing) ports for sending requests, and destination (incoming) ports for receiving responses. – Ron Maupin Mar 20 at 20:53
4  
Ports are assigned by the OS and each new connection is assigned a new local port to make it distinct from all other open connections. – Ex Umbris Mar 21 at 1:51
1  
@ExUmbris: That may be a sensible and simple strategy, but TCP connections are identified by the quad { local IP, local port, remote IP, remote port}. Local port isn't necessary for uniqueness, which is a good thing: the webserver cannot use its local port at all for uniqueness. And from the webserver's perspective, remote IP isn't unique either as multiple users may be located behind a single gateway/proxy. – MSalters Mar 21 at 14:57
up vote 37 down vote accepted

Do browsers use different ports to connect to different websites?

Yes, they do.

Here is an example, showing my current Firefox connections (I have 9 open tabs) on Windows 7:

enter image description here

Notes:

  • You can see that the local ports are all different.

  • The remote ports are usually 80 (HTTP), 443 (HTTPS) or 8080 (HTTP Alternate).

  • The full process of rendering a web page is described below. See in particular steps 5, 6, 13 and 15 (which are in bold):

    • In general rendering a single web page uses multiple connections, not all of which will be to the same remote address.

    • This is because web pages often include resources hosted elsewhere (javascript files, etc).

  • Multiple connections to the same website (eg stackoverflow.com) also have different local ports (because they are separate connections in different tabs rendering different pages).


Rendering a web page – step by step

Note:

  • Steps 5, 6, 13 and 15 (which are in bold) are directly relevant to the question.

Have you ever thought about what happens when you surf the web? It’s not as simple as it seems:

  1. You type an URL into address bar in your preferred browser.
  2. The browser parses the URL to find the protocol, host, port, and path.
  3. It forms a HTTP request (that was most likely the protocol)
  4. To reach the host, it first needs to translate the human readable host into an IP number, and it does this by doing a DNS lookup on the host
  5. Then a socket needs to be opened from the user’s computer to that IP number, on the port specified (most often port 80)
  6. When a connection is open, the HTTP request is sent to the host
  7. The host forwards the request to the server software (most often Apache) configured to listen on the specified port
  8. The server inspects the request (most often only the path), and launches the server plugin needed to handle the request (corresponding to the server language you use, PHP, Java, .NET, Python?)
  9. The plugin gets access to the full request, and starts to prepare a HTTP response.
  10. To construct the response a database is (most likely) accessed. A database search is made, based on parameters in the path (or data) of the request
  11. Data from the database, together with other information the plugin decides to add, is combined into a long string of text (probably HTML).
  12. The plugin combines that data with some meta data (in the form of HTTP headers), and sends the HTTP response back to the browser.
  13. The browser receives the response, and parses the HTML (which with 95% probability is broken) in the response
  14. A DOM tree is built out of the broken HTML
  15. New requests are made to the server for each new resource that is found in the HTML source (typically images, style sheets, and JavaScript files). Go back to step 3 and repeat for each resource.
  16. Stylesheets are parsed, and the rendering information in each gets attached to the matching node in the DOM tree
  17. Javascript is parsed and executed, and DOM nodes are moved and style information is updated accordingly
  18. The browser renders the page on the screen according to the DOM tree and the style information for each node
  19. You see the page on the screen
  20. You get annoyed the whole process was too slow.

Source Rendering a web page – step by step

share|improve this answer
    
Thank you for the elaborate information. I have a question. What does Hal stands for? – yoyo_fun Mar 20 at 21:02
1  
Hal is the name of my laptop. The first two connections are local connections back to my own machine. – DavidPostill Mar 20 at 21:12
1  
Something like wireshark will give this information – DavidPostill Mar 20 at 22:02
7  
Your machine's name is hal? Does he ever say "I'm sorry, Dave. I'm afraid I can't do that" – Raystafarian Mar 21 at 11:25
1  
@Raystafarian LOL. At one point (many years ago) I did have a Windows error sound set to that clip ;). I've also been know to call my computers orac and zen – DavidPostill Mar 21 at 11:27

Each connection to a website uses a different socket with default destination TCP port 80 for plain HTTP and 443 for HTTPS. For the socket to be unique, the combination of the source IP address, source TCP port, destination IP address and destination TCP port must be different.

If you have multiple connections to the same website (assuming the website uses only 1 IP address) from the same computer, a different source TCP port must be used. This way, each connection is unique.

However, it should be noted that as of HTTP 1.1, all connections are persistent for a given period of time (unless declared otherwise). This means that the same connection can be reused by your browser if multiple resources from the same website are requested (e.g. css/js files). This also applies if you have multiple instances of the same website in your browser.

If you are on Windows, the netstat -no -p TCP command will show you all active TCP sockets and their corresponding process ID, including those of your browser:

enter image description here

If you are on Unix/Linux (Debian in this case), you can use the netstat -ntp or ss -t command:

enter image description here

share|improve this answer
3  
Note that netstat will also show non-browser connections, for example email connections for an email client, news connections for a news reader. These connections will be on different remote ports. – DavidPostill Mar 20 at 20:55
    
Unless I'm missing something, it seems like you are making the assumption that the asker is using Windows, although he hasn't mentioned an operating system. It's also good to list what OS you are referring to whenever providing console commands. – user45623 Mar 21 at 6:50
8  
@user45623: Although the screenshot is a Windows screenshot, netstat -n should work in most operating systems, including Linux and Mac OS. – Heinzi Mar 21 at 7:29
1  
On Windows (maybe other OSs too) you can use netstat -n -o to see which process created which connection. Or you can run SysInternal's tcpview to see the list in a GUI, with process names and icons and all. – Jonathan Mar 21 at 8:55
    
Now the question is, do web browsers reuse connections from different tabs if they lead to the same server? – Jan Dvorak Mar 21 at 16:48

As regards tabs to different websites, there is nothing in TCP that requires the local port to be different, as long as the tuple {local IP, local port, target IP, target port} is unique. For tabs to the same website, the situation is much more complex.

The browser, like any other piece of client software, uses a different local port per outgoing connection to the same target. In general it will form multiple connections to any given website, to fetch embedded resources such as images, CSS, JavaScript, etc. It will also pool those connections for possible reuse.

It isn't possible to say whether different tabs to the same website, will use distinct connections, because (a) there usually isn't a single connection per tab anyway, and (b) depending on the timing and authentication, connections may be reused between tabs; and as it isn't possible to identity the connections it therefore isn't possible to identify the local ports either.

share|improve this answer
    
Thanks. What does "pooling" a connection mean? – yoyo_fun Mar 20 at 21:56
    
Instead of closing it, return it to a pool, and only closing it if it stays there idle for some timeout interval; and looking in the pool first before creating a new connection to that target. This is how HTTP keep-alive is implemented. – EJP Mar 20 at 22:00
    
So a pool is a data structure in the memory that stores opened and not yet closed connections? – yoyo_fun Mar 20 at 22:04
    
That's correct, typically a map keyed by target IP:port. – EJP Mar 20 at 22:06
1  
@EJP: Note that for HTTPS, it is not secure to key the map by target IP and port; you also need to distinguish between connections to different hostnames even though they happen to resolve to the same IP and port. Arguably a browser also should keep connections for different tabs separate if at least one tab is in incognito mode. – Henning Makholm Mar 20 at 22:24

Yes. No. Maybe. It depends.

First, a browser may use any of these strategies for connections:

  1. Single connection (unlikely for any browser more recent than 1995)
  2. One connection per tab (basically same as #1, just very slightly better)
  3. One connection per resource (naive, but doesn't work so bad)
  4. Pool of connections with keep-alive, re-using connections
  5. Something different (read as: weird stuff)

You don't have a way of knowing which strategy a browser will use, although using a pool of connections (and re-using connections) is a reasonable assumption.

Second, the way TCP works, you have a source port, and a destination port for every connection. The pair of source and destination address/port defines the connection.
You always[1] use a well-known port (such as 80 or 443) to connect to the server (to which it listens on its advertized address), but the other port is randomly chosen. Thus, depending on from which side you look at a connection, it either has one or many possible ports.

Thus, the same tab may (and usually will) use several different ports on its end, but in principle different tabs might (if connections are pooled and different resources in different tabs are loaded from the same server) use the same port.

Since the question explicitly mentions outgoing, in the "normal" case, the port numbers would be the same regardless which tab they're in, or one of two possible ports (80 and 443). Although of course it is possible to explicitly ask for a different port (like 8080) in an URL. That's kind of rare, though.


[1] Well, not always... but let's not complicate it too much.

share|improve this answer
    
One other factor... the client-side port is typically chosen by the OS, not the browser; and the client-side port that the server sees might be different than what the browser sees, if the connection goes through a NAT device. Most OSes allocate either linearly or randomly within a (configurable) ephemeral port range, but a browser could request the same source-port across multiple connections to different servers. (And the OP is asking about client ports, not server ports.) – david Mar 22 at 17:24
    
@david: It's hard to tell which one is right (or what to answer) since the Q is a tidbit ambiguous, hence my somewhat lengthy cover-all excursion. The OP is asking about the outgoing port on the client. "Client" suggests that we're talking about the source port (in TCP terms) which is the one that is freely or randomly chosen (by the implementation), but "outgoing" suggests it's really the destination port we are talking about. Which is much better described as "port on server" in layman words. Your comment on NAT is correct as seen from the server, but doesn't affect the client. – Damon Mar 23 at 11:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.