6

I am just learning computer networks so please forgive me if this question is naive. I know the TCP protocol binds itself to a port till the transfer of messages is over (port 80) and UDP is best effort (ie no binding). My question is if I try and access two websites at the same time (multiple tabs on my browser), assuming both websites are web servers, my questions are

  1. Does my computer communicate with one webservice (website) first and then communicate with the other (serially). Also if this is the case is the time difference so small that I feel it loads simultaneously?

  2. Suppose I have my own web server (tomcat) running on port 80, how can I communicate with other websites if it happens on the same port?

  3. Do websites decide which protocol to use TCP or UDP?

9

I know the TCP protocol binds itself to a port till the transfer of messages is over (port 80)

A TCP connection is uniquely identified by the 4-tuple (source address, source port, dest address, dest port). The source and destination IP addresses will take care of themselves, but you're slightly confused about the ports.

A port is just a 16-bit integer, used to distinguish between multiple active sockets on the same host, but there are certain conventions governing their allocation (reference: wikipedia):

  1. "well-known" ports are < 1024.

    As you noted, 80 is the "well-known" port for HTTP, which means it's the default unless your URL specifies otherwise. These ports are generally protected by the OS, so an unprivileged process cannot bind one and hence masquerade as a well-known service.

  2. "registered" ports, useable by unprivileged processes to provide services, are between 1024 and 49152. For example, 8080 is commonly used for an unprivileged HTTP server
  3. remaining values from 49152 to 65535 are used for ephemeral ports. When you create a socket and connect to a server, without binding your socket to a particular local port, the kernel assigns a free port from the ephemeral range. This is just to create a unique 4-tuple identifying your connection, and you'll normally never care what the value is.
    • NB. the actual range used for ephemeral ports may vary by OS and even be configurable - it'll always start above 1024 though.

and UDP is best effort (ie no binding).

UDP stands for User Datagram Protocol. You're right that it is best-effort, but that has nothing to do with ports. Both TCP and UDP use exactly the same IP addressing scheme, with the same 4-tuple. You'll probably never use it for HTTP though (see the answer to your last question below).

My question is if I try and access two websites at the same time (multiple tabs on my browser), assuming both websites are web servers, my questions are

  1. Does my computer communicate with one webservice (website) first and then communicate with the other (serially). Also if this is the case is the time difference so small that I feel it loads simultaneously?

Yes and no. You create two sockets and connect them both. The source IP will be the same for each (since they're on the same machine, and presumably using the same network interface on that machine). The destination IP and port will be the same (they're connecting to the same HTTP server). The source port however will be different, because your OS allocated a different ephemeral port to each socket.

Because each socket is your endpoint for a different TCP connection (they have different unique 4-tuples), they can run in parallel. However, assuming as above the two connections are over the same physical network interface, they can't send or receive physical packets simultaneously. In practise this doesn't matter, since the OS will interleave their packets onto the physical network for you.

The connections will generally be asynchronous, so both sockets can have in-flight requests at once, and the replies can also be interleaved.

  1. Suppose I have my own web server (tomcat) running on port 80, how can I communicate with other websites if it happens on the same port?

Your website will be listening on the IP,port tuple (localhost,80). If you connect to it from the same machine, your connection will be something like (localhost, ephemeral1, localhost, 80). If you connect to a web server on a different machine, your connection will be something like (localhost, ephemeral2, remotehost, 80). They're still different, even if they both have an 80 in one of the 4 values.

The only thing you can't do is have two different web-servers both listening to port 80 on the same machine.

  1. Do websites decide which protocol to use TCP or UDP?

You can always check this stuff yourself: the standard is here. Here's the relevant section:

HTTP communication usually takes place over TCP/IP connections. The default port is TCP 80 [19], but other ports can be used. This does not preclude HTTP from being implemented on top of any other protocol on the Internet, or on other networks. HTTP only presumes a reliable transport; any protocol that provides such guarantees can be used; the mapping of the HTTP/1.1 request and response structures onto the transport data units of the protocol in question is outside the scope of this specification.

So you see HTTP doesn't have to use TCP, but it does assume a reliable (and connection-oriented) protocol, so UDP is out.

  • 2
    You said: "UDP stands for Unreliable Datagram Protocol." Wrong. That is a colloquial name invented after the fact. UDP stands for "User Datagram Protocol" – riwalk Sep 16 '13 at 17:59
  • Thanks, the incorrect mnemonic is too easy to remember. Fixed. – Useless Sep 16 '13 at 18:02
  • @Stargazer712 and at useless: upd is still unreliable tho. You have to deal with dup packets and packets out of order. – ott-- Sep 16 '13 at 20:40
  • There is this IETF draft for HTTP over UDP (when the HTTP message fits in a single UDP datagram) but usually HTTP uses any reliable full-duplex stream. – ysdx Jan 26 '15 at 14:46
2

Here are some basics for you to guide your learning.

Connections to Web servers are always TCP connections, and the port number is the port number on the server receiving the connection, not the client making the connection.

So:

  1. It is common for web browsers to make multiple connections to web servers at the same time. Its considered good manors to not make more than 2 connections to the same server from the same client at any one time, but busy web servers will have hundreds (and even thousands) of simultaneous client connections.

    1. The local port used to connect to a remote server is generated randomly by your OS. It is not the same port number as the remote port you are connecting to, so there is no problems connecting to multiple web servers at the same time as running a web server locally.

    2. The protocol spoken by web servers are defined HTTP/1.0 and HTTP/1.1, and are specifically TCP connections.

  • so theoretically I can access 65,535 unique web services at one time from a computer ? – Rahul Kumar Sep 16 '13 at 17:22
  • @RahulKumar, assuming they are open at the same time, yes (although the actual number is likely to be smaller due to reserved ports). But your network card will probably melt before that happens. – riwalk Sep 16 '13 at 17:57
  • You will have problems getting to that number. Around about 10k connections is the most that you can achieve, although to handle this many connections also requires some skilled programming too. – Michael Shaw Sep 16 '13 at 18:00
  • @Ptolemyso for "server" architecture , how many ports do machines dedicated to be servers have ? Also how do popular sites like google , stack exchange :) manage the number of incoming connections . – Rahul Kumar Sep 16 '13 at 18:54
1

Each TCP connection has 4 values to identify it: IP1 (own IP address), port1 (the port on my end), IP2 (the other computer's address), port2 (the port on the other end).

As soon as any of these is different you are talking about a different connection. This creates a theoretical 4 billion TCP connections between any 2 computers. keeping this in mind:

  1. computers can have multiple connections active at the time (most of the time they'll choose a different local port from the 65k available),

    you can even connect twice to the same server on the same port as long a the locally chosen port is different

  2. same as above, there is enough leeway in the local port range to open multiple connections, (though no-one will ever use 80 as the local port for outgoing connections)

  3. http (like 90% of all internet traffic) is over TCP,

    UDP is only used for specialist applications where you are allowed to lose some data

  • so theoretically I can access 65,535 unique web services at one time from a computer ? – Rahul Kumar Sep 16 '13 at 17:21
  • More like 16k, since not all ports are available, and probably fewer if any ports are already used by background services, your mail client, etc. etc. – Useless Sep 16 '13 at 17:49
  • so for "server" architecture , how many ports do machines dedicated to be servers have ? Also how do popular sites like google , stack exchange :) manage the number of incoming connections . – Rahul Kumar Sep 16 '13 at 18:56
  • @RahulKumar it depends on the kernel, small PCs can choke on a few dozen incoming connection because they don't allocate enough resources to deal with them, high end servers will be set to handle more, (or use load distribution) – ratchet freak Sep 16 '13 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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