Computer Networks - Episode 3

Computer Networks - Episode 3

(Most IMP from interview perspective)

Agenda

  • HTTP

  • DNS

  • ipv4 & ipv6

  • NAT

  • TCP

    • HandShake
  • TCP vs UDP


HTTP (Hypertext Transfer Protocol)

It was a protocol to allow transfer of hypertext. Hypertext: documents which are specifying some information

Originally internet was used to transfer documents.

HTTP was a protocol that was created so as to transfer hypertexts. So as to transfer different documents. Nowadays http is used to transfer everything. You transfer videos through it, emojis through it, transfer files through it.

What is hypertext? It is nothing but texts. HTML is nothing but texts, but due to some tags, does it change the meaning of the text? It does! If a particular text is enclosed in anchor tag, then that means it's a hyperlink. If it's enclosed inside a li tag then it's a list item. That is what hypertext is. It is a text but it has some information associated with it.

HTTP is a - Client Server Protocol: It says when you are transferring file using HTTP, there are going to be 2 type of entities. 1st entity is a client and 2nd is a server. - Application Layer Protocol: It is used by applications to transfer data. This layer internally use transfer layer protocol. - HTTP used TCP. If someone wants to use UDP they can't use HTTP.

Properties of HTTP

  • It's a stateless protocol.

    • Stateless says every request sent via http is independent of any other requests

    • By default no state is maintained in servers. By default HTTP is going to maintain nothing.( So if you need to maintain a state, either the application or server or both have to maintain the state. When a client sends a request, server generates a token for that user who has just logged in, stores it within server as a map & sends back to the client. Client also stores the token. Whenever a client will send any more request to the server, it will also include the token while sending. When server will receive this token it will check if this token is correct. If token is correct, server will return response to client.) Imp thing to note here is this doesn't mean http is statefull. Http is still stateless.

  • Everything is an object/file. Whenever an http request is sent, it is sent for a particular file. A file that is there at a particular location. to get this location there's something called URL. HTTP uses url to get reference or to get the location of an object that is being fetched by an http request.

URL -> ex: google.com:80/somepath

  • http:// -> Protocol ( Ex: ws://_________ , ftp://__) When we try to connect to database in a java codebase : (jdbc://) The URL contains the protocol using which you are trying to access the object at given location.

  • Domain Name/ip address: google.com : The place from where you are trying to access that particular object.

  • 80: Port number. (If your application isnot using the default port number of that particular protocol, then you have to specify port in URL.)

  • somepath->Location: On this particular server/domain name, what is the path of the file I am trying to get.

Types of HTTP request

  1. Non Persistent

    1. Behind the scene http uses TCP. Let's say a client sends a request to his server. If you're setting a non-persistent http connection, in those particular scenarios client will send a request, a TCP connection will be established, server will send the response, work is done, each of the connection will get destroyed.

    2. In non persistent HTTP connections, a different TCP connection is established per request.

    3. Cons: Every request will require setting up TCP which is going to slow my application.

  2. Persistent

    1. Multiple request responses through a connection.

    2. Maintaining sessions consumes memory.

    3. Timeout: The connection will be automatically broken after timeout seconds.

By default, applications use non-persistent communication. because in 99% use cases work is done after a single re-res cycle. used in the majority of times.

Persistent is used when there is frequent data transfer between client-server. Ex: Streaming applications. live code editor.

When you open a website, what do you get from the server? The server will send you the HTML. When the server sends you the HTML, in that HTML there are other URLs as well. Those URLs can be your JavaScript files, those can be your CSS, images, those can be your any other files.

When we send an HTTP request there is a format

HTTP Methods

HTTP methods are just guidelines.

  1. GET: to fetch data from a particular resource. HTTP get request has a size limit around 356 characters.

  2. POST: to send some data to the server. It say's it's going to send data without knowing final location of the data. Server puts this data at correct location & sends me final location of data.

  3. PUT: Upload the data at a particular URL. Client knows final location of data.

Imp Questions

  • PUT vs POST

  • Search via POST

When you get a response from the server it sends a status code. Status code tells if the particular request that you sent is succeeded or it failed.

  • 1xx : The server wants to send some information in the response.

  • 2xx : Whatever you wanted to do happened successfully

  • 3xx : Redirect

  • 4xx : Client's fault

  • 5xx : Server fault

Cookies

  • It's nothing but a string/token.

  • Whenever we get a response. In response of the attribute is set-cookie.

  • These cookies are maintained in the browser. It's saved in memory.

  • To track if I have visited earlier or ...

  • used to simulate state

  • Whenever any req is sent to that domain in future all the cookies are sent automatically. Why Cookies?

  • Authorization

  • Track You

DNS

  • Humans are good at remembering names. But computers work on numbers.

  • There is need of a system that converts domain name to IP address.

  • Browser will connect to the DNS. Receive the ip address. & then connect to the server at that ip address.

How DNS works?

  • Works as a hierarchical database. It's like a tree like data structure.

  • Whenever a client sends a request it goes to a recursive resolver. It basically implements recursion. It talks with DNS Root Name Server .

  • These name servers are the source of truth. These name servers contain information about ip address of TLD (Top Level Domain) name servers.

  • Then TLD DNS. Then Authoritative Name Server which has complete ip address.

  • Client sends a request to recursive resolver

  • If for every request the DNS resolver has to make these 3 network calls, won't the internet become very slow for you. YES!. DNS involves a lot of caching.

  • Also ISPs like Airtel, and Jio.