Do web browsers use different outgoing ports for different tabs?

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


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).

    • Many other ports are used to host web servers. Search for HTTP on List of TCP and UDP port numbers for some of them.
  • 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


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.