Do web browsers use different outgoing ports for different tabs?
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?.
networking browser port tabs
fixer1234
yoyo_funyoyo_fun
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 '16 at 20:43
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 '16 at 20:49
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 '16 at 20:53
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 '16 at 1:51
@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 '16 at 14:57
Do browsers use different ports to connect to different websites?
Here is an example, showing my current Firefox connections (I have 9 open tabs) on Windows 7:
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
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:
You type an URL into address bar in your preferred browser.
The browser parses the URL to find the protocol, host, port, and path.
It forms a HTTP request (that was most likely the protocol)
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
Then a socket needs to be opened from the user's computer to that IP number, on the port specified (most often port 80)
When a connection is open, the HTTP request is sent to the host
The host forwards the request to the server software (most often Apache) configured to listen on the specified port
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?)
The plugin gets access to the full request, and starts to prepare a HTTP response.
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
Data from the database, together with other information the plugin decides to add, is combined into a long string of text (probably HTML).
The plugin combines that data with some meta data (in the form of HTTP headers), and sends the HTTP response back to the browser.
The browser receives the response, and parses the HTML (which with 95% probability is broken) in the response
A DOM tree is built out of the broken HTML
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.
Stylesheets are parsed, and the rendering information in each gets attached to the matching node in the DOM tree
Javascript is parsed and executed, and DOM nodes are moved and style information is updated accordingly
The browser renders the page on the screen according to the DOM tree and the style information for each node
You see the page on the screen
You get annoyed the whole process was too slow.
Source Rendering a web page – step by step
DavidPostill♦DavidPostill
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:
If you are on Unix/Linux (Debian in this case), you can use the netstat -ntp or ss -t command:
Egon OlieuxEgon Olieux
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 '16 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 '16 at 6:50
@user45623: Although the screenshot is a Windows screenshot, netstat -n should work in most operating systems, including Linux and Mac OS. – Heinzi Mar 21 '16 at 7:29
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 '16 at 8:55
Now the question is, do web browsers reuse connections from different tabs if they lead to the same server? – John Dvorak Mar 21 '16 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.
user207421user207421
Thanks. What does "pooling" a connection mean? – yoyo_fun Mar 20 '16 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. – user207421 Mar 20 '16 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 '16 at 22:04
That's correct, typically a map keyed by target IP:port. – user207421 Mar 20 '16 at 22:06
@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 '16 at 22:24
Yes. No. Maybe. It depends.
First, a browser may use any of these strategies for connections:
Single connection (unlikely for any browser more recent than 1995)
One connection per tab (basically same as #1, just very slightly better)
One connection per resource (naive, but doesn't work so bad)
Pool of connections with keep-alive, re-using connections
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.
DamonDamon
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 '16 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 '16 at 11:19
Not the answer you're looking for? Browse other questions tagged networking browser port tabs or ask your own question.
Understanding unique ip and port
Does a website know what (local) port is my browser using?
My web server and web browser appear to be using port 80 at the same time
Multi-Session handling web-browsers?
Tabs and Url bar on same toolbar in web browser
Web browsers(3) aren't loading page completely, in Windows 8
Clone user input over multiple browser tabs for testing different languages
Web Browsers for Oracle Linux
Multiple tabs of a browser
Use DDNS domain for multiple machines and ports
Routing data to different ports on the same device based on MAC Address
Windows 10 different web browsers and web development testing