Network Communication Protocols
Network Communication Protocols
Network Communication Protocols
Protocols
The rules you should care about
Protocols
Protocols
A communication protocol is a system of rules that allows two or more entities of a communications system to transmit
information via any kind of variation of a physical quantity. The protocol defines the rules, syntax, semantics and
synchronization of communication and possible error recovery methods.
Communicating systems use well-defined formats for exchanging various messages. Each message has an exact meaning
intended to elicit a response from a range of possible responses pre-determined for that particular situation. The specified
behavior is typically independent of how it is to be implemented. Communication protocols have to be agreed upon by the
parties involved
How data is sent
Circuit switching is a method of implementing a telecommunications network in which two network nodes establish a
dedicated communications channel (circuit) through the network before the nodes may communicate.
The circuit guarantees the full bandwidth of the channel and remains connected for the duration of the communication
session. The circuit functions as if the nodes were physically connected as with an electrical circuit. Circuit switching
originated in analog telephone networks where the network created a dedicated circuit between two telephones for the
duration of a telephone call.
How data is sent
Packet switching is a method of grouping data that is transmitted over a digital network into packets. Packets are made of
a header and a payload. Data in the header is used by networking hardware to direct the packet to its destination, where
the payload is extracted and used by application software. Packet switching is the primary basis for data communications in
computer networks worldwide.
Basically, that means that information is splitted into packets of predefined size and sent over network. In case there’s some
congestion on the way, the packets are switched through other routes, thankfully the networks have some redundancy.
Circuit VS Packet Switching
Difference between Circuit Switching and Packet Switching
Internet organizations
List of Internet organizations
● IAB (Internet Architecture Board)
● IANA (Internet Assigned Numbers Authority)
● ICANN (Internet Corporation for Assigned Names and Numbers)
● IESG (Internet Engineering Steering Group)
● IETF (Internet Engineering Task Force)
● IRTF (Internet Research Task Force)
● ISOC (Internet Society)
● NANOG (North American Network Operators' Group)
● NRO (Number Resource Organization)
● W3C (World Wide Web Consortium)
● OTF (Open Technology Fund)
● EFF (Electronic Frontier Foundation)
IEEE 802
IEEE 802 is a family of Institute of Electrical and Electronics Engineers (IEEE) standards for local area networks (LAN), personal area
network (PAN), and metropolitan area networks (MAN). The IEEE 802 family of standards has twelve members, numbered 802.1 through
802.12, with a focus group of the LMSC devoted to each.
The services and protocols specified in IEEE 802 map to the lower two layers (data link and physical) of the seven-layer Open Systems
Interconnection (OSI) networking reference model. IEEE 802 divides the OSI data link layer into two sub-layers: logical link control (LLC) and
medium access control (MAC).
Protocol suite
Open Systems Interconnection model -> OSI model
Sockets
A socket is an abstract representation (handle) for the local endpoint of a network communication path.
The Berkeley sockets API represents it as a file descriptor (file handle) that provides a common interface
for input and output to streams of data.
What is a network socket?
BSD -> Berkeley Software Distribution Socket
BSD Sockets are an API for dealing with system sockets, and therefore communication over network.
The API was first introduced in 1983, and still used today.
Client Server
Create a socket Create a socket
connect to some host/port bind to an address
send/receive data Set to listen
close connection accept clients
send/receive data
close connection
TCP / IP
Transmission Control Protocol a communications standard that enables application programs and computing devices to
exchange messages over a network. It is designed to send packets across the internet and ensure the successful delivery
of data and messages over networks.
The Internet Protocol (IP) is the method for sending data from one device to another across the internet. Every device has
an IP address that uniquely identifies it and enables it to communicate with and exchange data with other devices
connected to the internet.
IP is responsible for defining how applications and devices exchange packets of data with each other. It is the principal
communications protocol responsible for the formats and rules for exchanging data and messages between computers on a
single network or several internet-connected networks. It does this through the Internet Protocol Suite (TCP/IP), a group of
communications protocols that are split into four abstraction layers.
OSI vs TCP/IP
TCP/IP vs TCP and IP
What is the difference between TCP/IP protocol
and TCP model?
TCP/IP and TCP and IP difference?
TCP/IP is a protocol stack which contains different protocols required for the
data transfer from sender to receiver.
TCP/IP is also a layered protocol but does not use all of the OSI layers,
though the layers are equivalent in operation and function. The network
access layer is equivalent to OSI layers 1 and 2. The Internet Protocol layer
is comparable to layer 3 in the OSI model. The host-to-host layer is
equivalent to OSI layer 4. These are the TCP and UDP (user datagram
protocol) functions. Finally, the application layer is similar to OSI layers 5, 6,
and 7 combined.
The TCP layer packages the data into packets. A header that’s added to the
data includes source and destination addresses, a sequence number, an
acknowledgment number, a check sum for error detection and correction,
and some other information. The header is 20 octets (octet = 8 bits) grouped
in 32-bit increments.
TCP Handshake
● Step 1 (SYN) : In the first step, client wants to establish a
connection with server, so it sends a segment with SYN(Synchronize
Sequence Number) which informs server that client is likely to start
communication and with what sequence number it starts segments
with
● Step 2 (SYN + ACK): Server responds to the client request with
SYN-ACK signal bits set. Acknowledgement(ACK) signifies the
response of segment it received and SYN signifies with what
sequence number it is likely to start the segments with
● Step 3 (ACK) : In the final part client acknowledges the response of
server and they both establish a reliable connection with which they
will start the actual data transfer
The steps 1, 2 establish the connection parameter (sequence
number) for one direction and it is acknowledged. The steps 2, 3
establish the connection parameter (sequence number) for the other
direction and it is acknowledged. With these, a full-duplex
communication is established.
TCP Communication
TCP characteristics:
- stream-oriented - data is sent as a stream, thus chunked into packets implicitly, not by the user
- congestion control - a mechanism that ensures that no more packets are sent than the
connection allows. Think a way to quickly get rid of traffic jams.
- in-order data delivery, error-checking, and re-transmission should be self explanatory
How Transmission Control Protocol works
TCP connection is established and maintained until the applications at each end have finished exchanging messages.
TCP performs the following actions:
● determines how to break application data into packets that networks can deliver;
● sends packets to, and accepts packets from, the network layer;
● manages flow control;
● handles retransmission of dropped or garbled packets, as it's meant to provide error-free data transmission
● acknowledges all packets that arrive.
When a web server sends an HTML file to a client, it uses the hypertext transfer protocol (HTTP) to do so. The HTTP program
layer asks the TCP layer to set up the connection and send the file. The TCP stack divides the file into data packets, numbers
them and then forwards them individually to the IP layer for delivery.
Although each packet in the transmission has the same source and destination IP address, packets may be sent along multiple
routes. The TCP program layer in the client computer waits until all packets have arrived. It then acknowledges those it receives
and asks for the retransmission of any it does not, based on missing packet numbers. The TCP layer then assembles the packets
into a file and delivers the file to the receiving application.
TCP/IP Packet Format
When sending packets using TCP/IP, the data portion of each IP packet
is formatted as a TCP segment.
Each TCP segment contains a header and data. The TCP header
contains many more fields than the UDP header and can range in size
from 20 to 60 bytes, depending on the size of the options field.
The TCP header shares some fields with the UDP header: source port
number, destination port number, and checksum. To remember how
those are used, review the UDP article.
Process of TCP packets transmission
Establish connection
When two computers want to send data The SYN and ACK bits are both part of the TCP header:
to each other over TCP, they first need to
establish a connection using a three-way
handshake.
The retransmission may lead to the recipient receiving duplicate packets, if a packet was not actually lost
but just very slow to arrive or be acknowledged. If so, the recipient can simply discard duplicate packets.
It's better to have the data twice than not at all!
TCP packets order
Sometimes the missing packet is simply taking a
slower route through the Internet and it arrives soon
after.
When the recipient sees a higher sequence In both situations, the recipient has to deal with out of
number than what they have acknowledged so order packets. Fortunately, the recipient can use the
far, they know that they are missing at least one
sequence numbers to reassemble the packet data in
the correct order.
packet in between.
In the situation pictured above, the recipient
sees a sequence number of #73 but expected a
sequence number of #37. The recipient lets the
sender know there's something amiss by
sending a packet with an acknowledgement
number set to the expected sequence number.
TCP Error Control
1. Checksum – Every segment contains a checksum field which is used to find corrupted segments. If the segment is
corrupted, then that segment is discarded by the destination TCP and is considered lost.
2. Acknowledgement – TCP has another mechanism called acknowledgement to affirm that the data segments have been
delivered. Control segments that contain no data but have sequence numbers will be acknowledged as well but ACK
segments are not acknowledged.
3. Retransmission – When a segment is missing, delayed to deliver to a receiver, corrupted when it is checked by the
receiver then that segment is retransmitted again. Segments are retransmitted only during two events: when the sender
receives three duplicate acknowledgments (ACK) or when a retransmission timer expires.
1. Retransmission after RTO: TCP always preserves one retransmission time-out (RTO) timer for all sent but not
acknowledged segments. When the timer runs out of time, the earliest segment is retransmitted. Here no timer
is set for acknowledgment. In TCP, the RTO value is dynamic in nature and it is updated using the round trip
time (RTT) of segments. RTT is the time duration needed for a segment to reach the receiver and an
acknowledgment to be received by the sender.
2. Retransmission after Three duplicate ACK segments: RTO method works well when the value of RTO is
small. If it is large, more time is needed to get confirmation about whether a segment has been delivered or not.
Sometimes one segment is lost and the receiver receives so many out-of-order segments that they cannot be
saved. In order to solve this situation, three duplicate acknowledgement method is used and missing segment is
retransmitted immediately instead of retransmitting already delivered segment. This is a fast retransmission
because it makes it possible to quickly retransmit lost segments instead of waiting for timer to end.
TCP Congestion Control
TCP estimates congestion control using congestion window (CWND) and receiver window (RWND)
counters.
TCP is using sliding windows for both congestion and flow control.
A TCP cwnd window is the amount of unacknowledged data a sender can send on a particular
connection before it gets an acknowledgment back from the receiver, that it has received some of the
data.
The receiving device should acknowledge each packet it received, indicating the sequence number of
the last well-received packet. After receiving the ACK from the receiving device, the sending device
slides the window to right side.
TCP Congestion Control
TCP not-acknowledges packets by sending
duplicate ACKs of for the last continuous series
of packets. In such situations, all packets
between the not acknowledged one and the
ones already sent are retransmitted.
TCP Overview Part 1 || TCP Explained || IIT Computer Networks Lecture Series
User Datagram Protocol -> UDP
User Datagram Protocol (UDP) is a Transport Layer protocol. UDP is a
part of the Internet Protocol suite, referred to as UDP/IP suite. Unlike TCP,
it is an unreliable and connectionless protocol. So, there is no need to
establish a connection prior to data transfer.
UDP does not provide any communications security. Applications that need to
protect their communications against eavesdropping, tampering, or message
forgery therefore need to separately provide security services using additional
protocol mechanisms.
UDP
Application designers are generally aware that UDP does not
provide any reliability, e.g., it does not retransmit any lost packets.
Often, this is a main reason to consider UDP as a transport.
Applications that do require reliable message delivery therefore need
to implement appropriate protocol mechanisms in their applications
The Internet may also significantly delay some packets with respect
to others, e.g., due to routing transients, intermittent connectivity, or
mobility. This can cause reordering, where UDP datagrams arrive at
the receiver in an order different from the transmission order.
Applications that require ordered delivery must restore datagram
ordering themselves.
UDP benefits and downsides
UDP has a number of benefits for different types of applications, including:
● No retransmission delays – UDP is suitable for time-sensitive applications that can’t afford retransmission delays for
dropped packets. Examples include Voice over IP (VoIP), online games, and media streaming.
● Speed – UDP’s speed makes it useful for query-response protocols such as DNS, in which data packets are small
and transactional.
● Suitable for broadcasts – UDP’s lack of end-to-end communication makes it suitable for broadcasts, in which
transmitted data packets are addressed as receivable by all devices on the internet. UDP broadcasts can be
received by large numbers of clients without server-side overhead.
At the same time, UDP’s lack of connection requirements and data verification can create a number of issues when
transmitting packets. These include:
When sending packets using UDP over IP, the data portion of each IP packet is formatted as a
UDP segment.
Each UDP segment contains an 8-byte header and variable length data.
UDP Packet Format
Port numbers
The first four bytes of the UDP header store the port numbers for the source and destination.
A networked device can receive messages on different virtual ports, similar to how an ocean harbor can receive boats on
different ports. The different ports help distinguish different types of network traffic.
Segment Length
The next two bytes of the UDP header store the length (in bytes) of the segment (including the header).
Two bytes is 161616 bits, so the length can be as high as this binary number:
111111111111111111111111111111111111111111111111
In decimal, that's (2^16 - 1) or 65535. Thus, the maximum length of a UDP segment is 65535 bytes.
UDP Packet Format
Checksum
The final two bytes of the UDP header is the checksum, a field that's
The entire UDP segment could look like
used by the sender and receiver to check for data corruption. this:
But because UDP avoids the unnecessary overheads of TCP transport, it’s incredibly
efficient in terms of bandwidth, and much less demanding of poor performing networks, as
well.
Email Protocols
There are 3 main types of email protocols:
The main work of SMTP is to send emails. It can be used by the client to send emails to the server or used by one server to send
emails to another server. Precisely, in an SMTP scenario, the server decides the operations it wants to execute/operate. The
other server authorizes the operation and this is how the message is sent.
● Port 25:- This is the default port for SMTP and it is non-encrypted.
● Port 2525:- In case, port 25 is filtered and the user sends non-encrypted emails.
● Port 465:- This port of SMTP is used for establishing a secure connection
SMTP is an application layer protocol. The client who wants to send the mail opens a TCP connection to the
SMTP server and then sends the mail across the connection. The SMTP server is an always-on listening mode.
As soon as it listens for a TCP connection from any client, the SMTP process initiates a connection through port
25. After successfully establishing a TCP connection the client process sends the mail instantly.
SMTP
In the SMTP model user deals with the user agent (UA), for example, Microsoft Outlook, Netscape, Mozilla, etc. In
order to exchange the mail using TCP, MTA (Message transfer agent) is used. The user sending the mail doesn’t
have to deal with MTA as it is the responsibility of the system admin to set up a local MTA.
An MTA is just an element of the email delivery process. It receives an email from the mail/message submission
agent (MSA), which, in turn, receives it from the mail user agent (MUA). The MUA is commonly known as an email
client – an app you use to handle the email-related stuff.
The MTA maintains a small queue of mails so that it can schedule repeat delivery of mails in case the receiver is
not available. The MTA delivers the mail to the mailboxes and the information can later be downloaded by the user
agents.
POP3 -> Post Office Protocol V3
Represents Post Office Protocol version 3. It is used to download emails from the remote server to the local client. POP
allows us to download email messages from a local computer and delete emails from the server. This leads to less space
usage on the server. Although it supports offline email processing, it lacks advanced features like contacts, etc. It only
downloads new emails which have been received.
● Port 110:- This is the default port for POP3 and it is non-encrypted.
● Port 995:- This port of POP3 is used for establishing a secure connection.
Key Points
This email protocol was designed to enable users to read, draft, and delete emails when there is no smooth access to the
internet. With POP3, emails are delivered as soon as the user connects to the internet.
IMAP -> Internet Message Access Protocol
IMAP is used to receive emails from a server. IMAP settings have the option of storing emails on the mail server. It provides
an edge for the user to access emails from multiple locations, and it also allows simultaneous access by multiple clients.
This is why IMAP is more apt if the user accesses his/her email from different locations or if the messages of the user are
handled by multiple people.
The end-user can delete or allow the post as it is in their inbox after going through emails. Offline mode is also supported
for this setup, which means that even when offline changes are visible after reconnection. To understand it accurately, a
copy of recent emails and old emails will be available online.
● Port 143:- This is the default port for IMAP and it is non-encrypted.
● Port 993:- This port of IMAP is used for establishing a secure connection.
IMAP
IMAP features:
● It is capable of managing multiple mailboxes and organizing them into various categories.
● Provides adding of message flags to keep track of which messages are being seen.
● It is capable of deciding whether to retrieve email from a mail server before downloading.
● It makes it easy to download media when multiple files are attached.
Advantages :
Disadvantages :
USER – This command sends the user identification to the server. 200 Command okay.
PASS – This command sends the user password to the server. 530 Not logged in.
CWD – This command allows the user to work with a different directory or dataset for file 331 User name okay, need a password.
storage or retrieval without altering his login or accounting information.
225 Data connection open; no transfer in progress.
RMD – This command causes the directory specified in the path-name to be removed as
221 Service closing control connection.
a directory.
551 Requested action aborted: page type unknown.
MKD – This command causes the directory specified in the pathname to be created as a
directory. 502 Command not implemented.
PWD – This command causes the name of the current working directory to be returned in 503 Bad sequence of commands.
the reply.
504 Command not implemented for that parameter.
RETR – This command causes the remote host to initiate a data connection and to send
the requested file over the data connection.
STOR – This command causes to store a file into the current directory of the remote host.
LIST – Sends a request to display the list of all the files present in the directory.
ABOR – This command tells the server to abort the previous FTP service command and
any associated transfer of data.
QUIT – This command terminates a USER and if file transfer is not in progress, the server
closes the control connection.
FTP -> Passive vs Active
FTP sessions work in active or passive modes:
SCTP applications submit data for transmission in messages (groups of bytes) to the SCTP transport layer. SCTP places
messages and control information into separate chunks (data chunks and control chunks), each identified by a chunk
header. The protocol can fragment a message into multiple data chunks, but each data chunk contains data from only one
user message. SCTP bundles the chunks into SCTP packets. The SCTP packet, which is submitted to the Internet Protocol,
consists of a packet header, SCTP control chunks (when necessary), followed by SCTP data chunks (when available).
SCTP
In general, SCTP may provide more flexibility for certain applications, like Voice over IP (VoIP), that require the reliable but
message-oriented data transfer. For this category of applications, SCTP is most likely better-suited than TCP or UDP.
● TCP provides reliable and strict order-of-transmission data delivery. For applications that need reliability, but can
tolerate unordered or partially ordered data delivery, TCP may cause some unnecessary delay because of
head-of-line blocking. With the concept of multiple streams within a single connection, SCTP can provide strictly
ordered delivery within a stream while logically isolating data from different streams.
● SCTP is message-oriented, unlike TCP, which is byte-oriented. Because of the byte-oriented nature of TCP, the
application has to add its own record marking to maintain message boundaries.
● SCTP provides some degree of fault tolerance by using the Multihoming feature. A host is considered multihomed
when it has more than one network interface attached, either on the same or different networks. An SCTP
association can be established between two multihomed hosts. In this case, all IP addresses of both endpoints are
exchanged at association startup; this allows each endpoint to use any of these addresses over the life of the
connection if one of the interfaces is down for any reason, as long as the peer is reachable through the alternate
interfaces.
SCTP Conceptual Model
HTTP -> Hypertext Transfer Protocol
The HTTP is high level, textual, application layer protocol
It was designed for communication between web browsers and web servers, but it can also be used for other purposes.
HTTP follows a classical client-server model, with a client opening a connection to make a request, then waiting until it
receives a response. HTTP is a stateless protocol, meaning that the server does not keep any data (state) between two
requests.
HTTP Basics
Clients and servers communicate by exchanging individual messages (as opposed to a stream of data). The messages
sent by the client are called requests and the messages sent by the server as an answer are called responses.
Designed in the early 1990s, HTTP is an extensible protocol which has evolved over time. It is an application layer protocol
that is sent over TCP, or over a TLS-encrypted TCP connection, though any reliable transport protocol could theoretically be
used. Due to its extensibility, it is used to not only fetch hypertext documents, but also images and videos or to post content
to servers, like with HTML form results. HTTP can also be used to fetch parts of documents to update Web pages on
demand.
Basic aspects of HTTP
HTTP is simple
HTTP is generally designed to be simple and human readable, even with the added complexity introduced in HTTP/2 by
encapsulating HTTP messages into frames. HTTP messages can be read and understood by humans, providing easier
testing for developers, and reduced complexity for newcomers.
HTTP is extensible
Introduced in HTTP/1.0, HTTP headers make this protocol easy to extend and experiment with. New functionality can even
be introduced by a simple agreement between a client and a server about a new header's semantics.
1. Open a TCP connection: The TCP connection is used to send a request, or several, and receive an answer. The
client may open a new connection, reuse an existing connection, or open several TCP connections to the servers.
2. Send an HTTP message: HTTP messages (before HTTP/2) are human-readable. With HTTP/2, these simple
messages are encapsulated in frames, making them impossible to read directly, but the principle remains the same.
For example:
HTTP messages are composed of textual information encoded in ASCII, and span over multiple lines. In HTTP/1.1, and
earlier versions of the protocol, these messages were openly sent across the connection. In HTTP/2, the once
human-readable message is now divided up into HTTP frames, providing optimization and performance improvements.
HTTP headers
HTTP headers let the client and the server pass additional information with an HTTP request or response.
An HTTP header consists of its case-insensitive name followed by a colon (:), then by its value. Whitespace before the
value is ignored.
Custom proprietary headers have historically been used with an X- prefix, but this convention was deprecated in June 2012
because of the inconveniences it caused when nonstandard fields became standard in RFC 6648
● Request headers contain more information about the resource to be fetched, or about the client requesting the
resource.
● Response headers hold additional information about the response, like its location or about the server providing it.
● Representation headers contain information about the body of the resource, like its MIME type, or
encoding/compression applied.
● Payload headers contain representation-independent information about payload data, including content length and
the encoding used for transport.
HTTP headers
First we’ll look at the 5 headers that you will most often interact with:
1. Authorization: This client-side header communicates the authorization status of the user. This is a token with the authorization
status of the user. It is different from authentication, which is concerned with the actual validation of the user's identity. The
authorization header is generally used to send a token that the server can process to determine if the user is allowed to access
the given resource.
2. Content-Type: This client and server-side header tells the recipient of the request what the body of your request contains. For
example, is the data type JSON, text, etc...
3. Accept: This Client-side header determines the data type that you expect to get back from the server, and thus which your
application is expected to be able to understand. It can be useful to provide this information to the server so that it won’t waste
time processing a request from a client that can’t receive a given data type.
4. Cookie: This client-side header contains other metadata stored in cookies on the users browser. these are often, but not
exclusively, originally set by the server with the Set-Cookie header, and can be used to communicate any type of metadata
about the user, including browsing history or login status.
5. Access-Control-Allow-Origin: This is a server-side header. It contains information about the servers accepted web addresses,
as per a protocol called CORs. Essentially it is the servers built in whitelist of clients that it expects to interact with. If you see an
error referencing “Cross-origin-request”s in your client side code, it likely has to do with this header. For more information on
CORS I recommend this article.
HTTP request methods
HTTP defines a set of request methods to indicate the desired action to be performed for a given resource. Although they
can also be nouns, these request methods are sometimes referred to as HTTP verbs.
Each of them implements a different semantic, but some common features are shared by a group of them: e.g. a request
method can be safe, idempotent, or cacheable.
GET
PUT
The GET method requests a representation of the
The PUT method replaces all current
specified resource. Requests using GET should only
representations of the target resource with the
retrieve data.
request payload.
HEAD
DELETE
The HEAD method asks for a response identical to a
The DELETE method deletes the specified
GET request, but without the response body.
resource.
POST
CONNECT
The POST method submits an entity to the specified
The CONNECT method establishes a tunnel to
resource, often causing a change in state or side
the server identified by the target resource.
effects on the server.
HTTP request methods idempotency -> POST VS PUT
An HTTP method is idempotent if an identical request can be made once or several times in a row with the same effect
while leaving the server in the same state. In other words, an idempotent method should not have any side-effects (except
for keeping statistics). Implemented correctly, the GET, HEAD, PUT, and DELETE methods are idempotent, but not the POST
method.
OPTIONS
The OPTIONS method describes the
To be idempotent, only the actual back-end state of the server is
communication options for the target resource.
considered, the status code returned by each request may differ: the
first call of a DELETE will likely return a 200, while successive ones
will likely return a 404. Another implication of DELETE being
TRACE idempotent is that developers should not implement RESTful APIs
The TRACE method performs a message with a delete last entry functionality using the DELETE method.
loop-back test along the path to the target
Note that the idempotence of a method is not guaranteed by the
resource. server and some applications may incorrectly break the idempotence
constraint.
PATCH
The PATCH method applies partial modifications
to a resource.
HTTP response status codes
HTTP response status codes indicate whether a specific HTTP request has been
successfully completed. Responses are grouped in five classes:
Full list of HTTP status code: HTTP response status codes - HTTP | MDN
HTTP Message Structure
HTTP requests, and responses, share similar structure and are composed of:
1. A start-line describing the requests to be implemented, or its status of whether successful or a failure. This start-line is always a
single line.
2. An optional set of HTTP headers specifying the request, or describing the body included in the message.
3. A blank line indicating all meta-information for the request has been sent.
4. An optional body containing data associated with the request (like content of an HTML form), or the document associated with a
response. The presence of the body and its size is specified by the start-line and HTTP headers.
The start-line and HTTP headers of the HTTP message are collectively known as the head of the requests, whereas its payload is
known as the body.
HTTP Message Start Line
The start line of an HTTP request contain three elements:
1. An HTTP method, a verb (like GET, PUT or POST) or a noun (like HEAD or OPTIONS), that describes the action to
be performed. For example, GET indicates that a resource should be fetched or POST means that data is pushed to
the server (creating or modifying a resource, or generating a temporary document to send back).
2. The request target, usually a URL, or the absolute path of the protocol, port, and domain are usually characterized by
the request context. The format of this request target varies between different HTTP methods.
3. The HTTP version, which defines the structure of the remaining message, acting as an indicator of the expected
version to use for the response.
The start line of an HTTP response, called the status line, contains the following information:
● Single-resource bodies, consisting of one single file, defined by the two headers: Content-Type and
Content-Length.
● Multiple-resource bodies, consisting of a multipart body, each containing a different bit of information. This is typically
associated with HTML Forms.
The last part of a response is the body. Not all responses have one: responses with a status code that sufficiently answers
the request without the need for corresponding payload (like 201 Created or 204 No Content) usually don't.
● Single-resource bodies, consisting of a single file of known length, defined by the two headers: Content-Type and
Content-Length.
● Single-resource bodies, consisting of a single file of unknown length, encoded by chunks with Transfer-Encoding
set to chunked.
● Multiple-resource bodies, consisting of a multipart body, each containing a different section of information. These are
relatively rare.
HTTP/2 Frames
HTTP/1.x messages have a few drawbacks for
performance:
● short-lived connections,
● persistent connections
● HTTP pipelining.
● If the server does not have any data available for the
client, instead of sending an empty response, the server
holds the request and waits until some data becomes
available.
● Once the data becomes available, a full response is sent
to the client. The client then immediately re-request
information from the server so that the server will almost
always have an available waiting request that it can use
to deliver data in response to an event.
4. The client receives the response.
The Long-Polling process consists of the following steps:
5. The client usually makes a new request right away or after
1. The client makes a request to the server. some defined interval so that the connection with the server is
2. The server receives the request and delays sending established again.
anything to the client until the requested data is available
6. Each Long-Poll request has a timeout. The client has to
or there is any other update related to data.
reconnect periodically after the connection is closed due to
3. When the data is available, the response is sent to the
timeouts.
client.
Server Sent Events -> SSE
SSE is a way of establishing long-term
communication between client and server that
enables the server to proactively push data to the
client.
A WebSocket server is nothing more than an application listening on any port of a TCP server that follows a specific
protocol. After creating the initial HTTP/1.1 session, you need to request the upgrade by adding to a standard request the
Upgrade and Connection headers. The client will send a pretty standard HTTP request with headers that looks like this
(the HTTP version must be 1.1 or greater, and the method must be GET):
If the server decides to upgrade the connection, it sends back a 101 Switching Protocols response status with an
Upgrade header that specifies the protocol(s) being switched to. If it does not (or cannot) upgrade the connection, it ignores the
Upgrade header and sends back a regular response (for example, a 200 OK).
Right after sending the 101 status code, the server can begin speaking the new protocol, performing any additional
protocol-specific handshakes as necessary. Effectively, the connection becomes a two-way pipe as soon as the upgraded
response is complete, and the request that initiated the upgrade can be completed over the new protocol.
WebSocket connection upgrade (handshake)
When the server receives the handshake request, it
should send back a special response that indicates
that the protocol will be changing from HTTP to
WebSocket. That header looks something like the
following
This doesn't directly relate to the WebSocket protocol, but it's worth mentioning here: your server must keep track of clients'
sockets so you don't keep handshaking again with clients who have already completed the handshake. The same client IP
address can try to connect multiple times. However, the server can deny them if they attempt too many connections in order
to save itself from Denial-of-Service attacks.
For example, you might keep a table of usernames or ID numbers along with the corresponding WebSocket and other data
that you need to associate with that connection.
Usually all this things are done by websockets implementation libraries, but if you decide to implement some custom server
from scratch you will have keep track of clients.
WebSocket Messages
WebSocket is a framed protocol, meaning that a chunk of data (a message) is divided into a number of discrete chunks,
with the size of the chunk encoded in the frame. The frame includes a frame type, a payload length, and a data portion. An
overview of the frame is given in RFC 6455 and reproduced here.
Fin Bit
The first bit of the WebSocket header is the Fin bit.
This bit is set if this frame is the last data to complete
this message.
Payload data
The Payload data includes arbitrary application data
and any extension data that has been negotiated
between the client and the server. Extensions are
negotiated during the initial handshake and allow
you to extend the WebSocket protocol for additional
uses.
WebSocket Messages
To close a WebSocket connection, a closing frame is sent (opcode 0x08). In addition to the opcode, the close frame may
contain a body that indicates the reason for closing. If either side of a connection receives a close frame, it must send a
close frame in response, and no more data should be sent over the connection. Once the close frame has been received by
both parties, the TCP connection is torn down. The server always initiates closing the TCP connection.
HTTP Polling vs SSE vs WebSocket
REST cons: Decoupled client and server. Decoupling the client and the server as
much as possible, REST allows for a better abstraction than RPC. A
No single REST structure. There’s no exact right way to build a REST API. system with abstraction levels is able to encapsulate its details to better
How to model resources and which resources to model will depend on each identify and sustain its properties. This makes a REST API flexible enough
scenario. This makes REST simple in theory, but difficult in practice. to evolve over time while remaining a stable system.
Big payloads. REST returns a lot of rich metadata so that the client can Discoverability. Communication between the client and server describes
understand everything necessary about the state of the application just from everything so that no external documentation is required to understand
its responses. And this chattiness is no big deal for a big network pipe with how to interact with the REST API.
lots of bandwidth capacity. But that’s not always the case.
Cache-friendly. Reusing a lot of HTTP tools, REST is the only style that
Over- and under-fetching problems. Containing either too much data or allows caching data on the HTTP level. In contrast, caching
not enough of it, REST responses often create the need for another request. implementation on any other API will require configuring an additional
cache module.
Management APIs. APIs focused on managing objects in a system and intended for many consumers are the most common API type. REST helps
such APIs to have strong discoverability, good documentation, and it fits this object model well.
Simple resource-driven apps. REST is a valuable approach for connecting resource-driven apps that don’t need flexibility in queries.
Remote Procedure Call (RPC): invoking a function on
another system
A Remote Procedure Call is a specification that allows for remote
execution of a function in a different context. RPC extends the notion of
local procedure calling but puts it in the context of an HTTP API.
The two machines must have reached prior agreement on the meaning of control information and on
the procedures used to start, suspend, resume, and conclude transmissions. In addition, if
transmission errors are possible, control information must be exchanged to guard the transfer of the
data.
All rules, formats, and procedures that have been agreed upon between A and B are collectively called
a protocol:The protocol, then, can contain agreements on the methods used for:
● Initiation and termination of data exchanges
● Synchronization of senders and receivers
● Detection and correction of transmission errors
● Formatting and encoding of data
Protocols representation
An important part in designing protocols is to
specify in a very detailed, non-ambiguous way,
how it works. Natural language is ambiguous, and
in order to alleviate this problem, special domain
language with very strict definitions, and tons of
documentation are usually needed.
SERVICE SPECIFICATION
The purpose of the protocol is to transfer text files as sequences of
characters across a telephone line while protecting against
transmission errors, assuming that all transmission errors can in fact
be detected. The protocol is defined for full-duplex file transfer, that
is, it should allow for transfers in two directions simultaneously.
Positive and negative acknowledgments for traffic from A to B are
sent on the channel from B to A, and vice versa. Every message
contains two parts: a message part, and a control part that applies
to traffic on the reverse channel.
Protocol Assumption About The Environment
The ‘‘environment’’ in which the protocol is to be executed consists minimally of
two users of the file transfer service and a transmission channel.
The users can be assumed to simply submit a request for file transfer and await its
completion.
The transmission channel is assumed to cause arbitrary message distortions, but
not to lose, duplicate, insert, or reorder messages.
Protocol Vocabulary
The protocol vocabulary defines three distinct types of messages:
Each message type can further be refined into a class of lower-level messages,
consisting for instance of one sub-type for each character code to be transmitted.
Protocol Message Format
Each message consists of a control field identifying the message type and a data field with
the character code.
For the example we assume that the data and control fields are of a fixed size.
The general form of each message can now be represented symbolically as a simple
structure of two fields:
{ control_tag, data }
which in a C-like specification may be specified in more detail as follows:
enum control { ack, nak, err };
struct message {
enum control tag;
unsigned char data;
};
Protocol Procedure Rules
The procedure rules for the protocol were
informally described as follows:
The check bits have been placed in the code word in such a
way that the sum of the bit positions they occupy points at the
erroneous bit for any single bit error. To catch a single bit error
the check bits are used as parity bits.