Lab Viva 2024
Lab Viva 2024
deliver;
sends packets to, and accepts packets from, the network layer;
TCP is the preferred protocol where reliability of data weighs more than
transmission speed. UDP is connectionless and its header size is lightweight,
which is why it is fast, but not as reliable as TCP.
3. What is a Socket?
A socket is the endpoint of communication. It is where a connection starts or
begins. There must be two sockets on either connection point for any
communication to exist—one from the sending device or server and another
from the receiving device or client. A socket is made up of an IP Address and a
port number.
A socket is one endpoint of a two-way communication link between two
programs running on the network. A socket is bound to a port number so that
the TCP layer can identify the application that data is destined to be sent to.
Socket types and associated data
SOCK_STREAM - Transmission Control Protocol (TCP)- The stream socket
(SOCK_STREAM) interface defines a reliable connection-oriented service. Data
is sent without errors or duplication and is received in the same order as it is
sent.
SOCK_DGRAM- User Datagram Protocol (UDP)- The datagram socket
(SOCK_DGRAM) interface defines a connectionless service for datagrams, or
messages. Datagrams are sent as independent packets. The reliability is not
guaranteed, data can be lost or duplicated, and datagrams can arrive out of
order. However, datagram sockets have improved performance capability over
stream sockets and are easier to use.
SOCK_RAW - IP, ICMP, RAW- The raw socket (SOCK_RAW) interface
allows direct access to lower-layer protocols such as Internet Protocol (IP).
The TCP/IP model covers many internet protocols, which define how data is
addressed and sent over the internet. Common internet protocols include
HTTP, FTP, and SMTP, and all three are often used in conjunction with the
TCP/IP model.
HTTP (Hypertext Transfer Protocol) governs the workings of web
browsers and websites.
FTP (File Transfer Protocol) defines how files are sent over a network.
SMTP (Simple Mail Transfer Protocol) is used to send and receive email.
What is the difference between TCP and IP?
TCP and IP are separate computer network protocols. The difference between
TCP (Transmission Control Protocol) and IP (Internet Protocol) is their role in
the data transmission process. IP obtains the address where data is sent (your
computer has an IP address). TCP ensures accurate data delivery once that IP
address has been found. Together, the two form the TCP/IP protocol suite.
In other words, IP sorts the mail, and TCP sends and receives the mail. While
the two protocols are usually considered together, other protocols, such as
UDP (User Datagram Protocol), can send data within the IP system without the
use of TCP. But TCP requires an IP address to send data. That’s another
difference between IP and TCP.
What are the layers of the TCP/IP model?
There are four layers of the TCP/IP model: network access, internet, transport,
and application. Used together, these layers are a suite of protocols. The
TCP/IP model passes data through these layers in a particular order when a
user sends information, and then again in reverse order when the data is
received.
The open systems interconnection model:
The OSI model is important in IoT model operation. There are seven layers to
the OSI model and these are discussed briefly here [18]. It consists of seven
layers, namely:
1.Physical layer
2.Data link layer
3.Network layer
4.Transport layer
5.Session layer
6.Presentation layer
7.Application layer
TCP provides reliability. When a TCP client send data to the server, it requires
an acknowledgement in return. If an acknowledgement is not received, TCP
automatically retransmit the data and waits for a longer period of time.
Socket addresses
struct in_addr{
in_addr_t s_addr; /*32 bit IPv4 network byte ordered address*/
};
struct sockaddr_in {
uint8_t sin_len; /* length of structure (16)*/
sa_family_t sin_family; /* AF_INET*/
in_port_t sin_port; /* 16 bit TCP or UDP port number */
struct in_addr sin_addr; /* 32 bit IPv4 address*/
char sin_zero[8]; /* not used but always set to zero */
};
There are two ways to store two bytes in memory: with the lower-order byte
at the starting address (little-endian byte order) or with the high-order byte at
the starting address (big-endian byte order). We call them collectively host
byte order. For example, an Intel processor stores the 32-bit integer as four
consecutives bytes in memory in the order 1-2-3-4, where 1 is the most
significant byte. IBM PowerPC processors would store the integer in the byte
order 4-3-2-1.
Networking protocols such as TCP are based on a specific network byte order.
The Internet protocols use big-endian byte ordering.
The htons(), htonl(), ntohs(), and ntohl() Functions
The follwowing functions are used for the conversion:
#include <netinet/in.h>
uint16_t htons(uint16_t host16bitvalue);
uint32_t htonl(uint32_t host32bitvalue);
uint16_t ntohs(uint16_t net16bitvalue);
uint32_t ntohl(uint32_t net32bitvalue);
The first two return the value in network byte order (16 and 32 bit,
respectively). The latter return the value in host byte order (16 and 32 bit,
respectively).
The sequence of function calls for the client and a server participating in a TCP
connection:
The bind() Function
The bind() assigns a local protocol address to a socket. With the Internet
protocols, the address is the combination of an IPv4 or IPv6 address (32-bit or
128-bit) address along with a 16 bit TCP port number.
#include <sys/socket.h>
int bind(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen);
#include <sys/socket.h>
int listen(int sockfd, int backlog);
where sockfd is the socket descriptor and backlog is the maximum number of
connections the kernel should queue for this socket. The backlog argument
provides an hint to the system of the number of outstanding connect requests
that is should enqueue in behalf of the process. Once the queue is full, the
system will reject additional connection requests. The backlog value must be
chosen based on the expected load of the server.
The accept() is used to retrieve a connect request and convert that into a
request. It is defined as follows:
#include <sys/socket.h>
int accept(int sockfd, struct sockaddr *cliaddr,
socklen_t *addrlen);
where sockfd is a new file descriptor that is connected to the client that called
the connect(). The cliaddr and addrlen arguments are used to return the
protocol address of the client. The new socket descriptor has the same socket
type and address family of the original socket. The original socket passed
to accept() is not associated with the connection, but instead remains available
to receive additional connect requests. The kernel creates one connected
socket for each client connection that is accepted.
#include <sys/socket.h>
ssize_t send(int sockfd, const void *buf, size_t nbytes, int flags);
where buf and nbytes have the same meaning as they have with write. The
additional argument flags is used to specify how we want the data to be
transmitted. We will not consider the possible options in this course. We will
assume it equal to 0.
The recv() function is similar to read(), but allows to specify some options to
control how the data are received. We will not consider the possible options in
this course. We will assume it is equal to 0.
#include <sys/socket.h>
ssize_t recv(int sockfd, void *buf, size_t nbytes, int flags);
The function returns the length of the message in bytes, 0 if no messages are
available and peer had done an orderly shutdown, or -1 on error.
It is a data-link layer protocol which is used for transmitting the data over the
noiseless channels. It provides unidirectional data transmission which means
that either sending or receiving of data will take place at a time. It provides
flow-control mechanism but does not provide any error control mechanism.
The idea behind the usage of this frame is that when the sender sends the
frame then he waits for the acknowledgment before sending the next frame.
1. Go-Back-N ARQ
If the receiver receives a corrupted frame, it cancels it. The receiver does not
accept a corrupted frame. When the timer expires, the sender sends the correct
frame again.
In the protocol Go-Back-N ARQ (Automatic Repeat Request), the send window
size is determined by the number of bits used for the sequence number. Given
that 5 bits are being used for the sequence number, we would get 2^5 or 32
possible sequence numbers. However, in order to make sure there is no
confusion by wrapping around the sequence numbers, only half of the
sequence number space is used for the window size at any point. This means
that the maximum size of the send window must be 2^5 / 2 which is 16.
Selective Repeat ARQ is also known as the Selective Repeat Automatic Repeat
Request. It is a data link layer protocol that uses a sliding window method. The
Go-back-N ARQ protocol works well if it has fewer errors. But if there is a lot of
error in the frame, lots of bandwidth loss in sending the frames again. So, we
use the Selective Repeat ARQ protocol. In this protocol, the size of the sender
window is always equal to the size of the receiver window. The size of the sliding
window is always greater than 1.
If the receiver receives a corrupt frame, it does not directly discard it. It sends a
negative acknowledgment to the sender. The sender sends that frame again as
soon as on the receiving negative acknowledgment. There is no waiting for any
time-out to send that frame.
If a frame is corrupted or lost in it,all In this, only the frame is sent again, which is
subsequent frames have to be sent corrupted or lost.
again.
It does not require sorting. In this, sorting is done to get the frames in the
correct order.
Let dx(y) be the cost of the least-cost path from node x to node y. The least costs
are related by Bellman-Ford equation,
Where the minv is the equation taken for all x neighbors. After traveling from x
to v, if we consider the least-cost path from v to y, the path cost will be
c(x,v)+dv(y). The least cost from x to y is the minimum of c(x,v)+dv(y) taken over
all neighbors.
With the Distance Vector Routing algorithm, the node x contains the following
routing information:
o For each neighbor v, the cost c(x,v) is the path cost from x to directly
attached neighbor, v.
o The distance vector x, i.e., Dx = [ Dx(y) : y in N ], containing its cost to all
destinations, y, in N.
o The distance vector of each of its neighbors, i.e., Dv = [ Dv(y) : y in N ] for
each neighbor v of x.