Networking
Networking
Networking
programming)
A network programming interface is an application programming interface (API) that contains a set
of communications routines or system calls. An application can call these routines to communicate
with another application that is on the same or on different computers.
Note: An API is a software intermediary that allows two applications to talk to each other.
A socket is a communications connection point (endpoint) that you can name and address in a network.
Socket programming shows how to use socket APIs to establish communication links between remote and
local processes.
Socket Programming is a method to connect two nodes over a network to establish a means of communication
between those two nodes. A node represents a computer or a physical device with an internet connection.
A socket is the endpoint used for connecting to a node. The signals required to implement the connection
between two nodes are sent and received using the sockets on each node respectively.
We will look deeply at how the above process can be performed using socket programming in C in the
following sections.
A. Socket Creation
The first stage deals with the creation of a socket, which is the basic component for sending or receiving signals
between nodes. The sys/socket.h header has the necessary functions to create a socket in C. In socket
programming in C, a socket can be created by the socket() function with syntax
The domain represents the address family over which the communication will be performed. The
domain is pre-fixed values present in the sys/socket.h header. Some domains are,
o AF_LOCAL or AF_UNIX is used for local communication or in the case where the client and
server are on the same node. These sockets are called UNIX domain sockets.
o AF_INET is used to represent the IPv4 address of the client to which a connection should be
made. Similarly AF_INET6 is used for IPv6 addresses. These sockets are called internet domain
sockets.
o AF_BLUETOOTH is used for low-level Bluetooth connection.
The type represents the type of communication used in the socket. Some mostly used types of
communication are,
o SOCK_STREAM uses the TCP(Transmission Control Protocol) to establish a connection. This
type provides a reliable byte stream of data flow and is a connection-based protocol. These
sockets are called stream sockets.
o SOCK_DGRAM uses the UDP(User Datagram Protocol) which is unreliable and a
connectionless protocol. These sockets are also called datagram sockets.
The protocol represents the protocol used in the socket. This is represented by a number. When there is
only one protocol in the protocol family, the protocol number will be 0, or else the specific number for
the protocol has to be specified.
The socket() function creates a socket and returns a file descriptor which represents an open file that will be
utilized by the socket in reading and writing operations and the file descriptor is used to represent the socket in
later stages. In case of an error in creating the socket, -1 is returned by the socket() function.
B. Setsockopt
The setsockopt() function in socket programming in C is used to specify some options for the socket to control
the behavior of the socket. The syntax is,
C. Bind
The bind() function in socket programming in C is used to assign an address to a socket created using
the socket() function. The syntax of bind() function is,
The socket_descriptor is the value of the file descriptor returned by the socket() function.
The address is a structure of type sockaddr. We usually use a structure of type sockaddr_in to
represent this information, because information such as port and address can only be stored in this
structure. The sockaddr_in is cast to the sockaddr data type when calling the bind() function.
The length_of_address represents the size of the address passed as the second parameter.
The function returns 0 on binding the address and port successfully or returns -1 on failure.
D. Listen
The listen() function in socket programming is used to make the server node wait and listen for connections
from the client node on the port and address specified by the bind() function. The syntax is,
The socket_descriptor represents the value of the file descriptor returned by the socket() function.
The back_log marks the maximum number of connection requests that can be made to the server by
client nodes at a time. The number of requests made after the number specified by back_log may cause
an error or will be ignored by the server if the options for retransmission are set.
The function returns 0 on listening on the address and port specified or returns -1 on failure.
E. Accept
The accept() function is used to establish a connection between the server and the client nodes for the transfer
of data. The syntax is,
int accept(int socket_descriptor, struct sockaddr *restrict address, socklen_t *restrict length_of_address);
The socket_descriptor represents the value of the file descriptor returned by the socket() function.
The address is the variable of the sockaddr_in structure in which the address of the socket returned
from the function will be stored.
The length_of_address depicts the size of the address parameter.
The accept() function creates a new socket from the first connection request for the
specified socket_descriptor and returns the file descriptor of the new socket. The file descriptor of this new
socket is used in the read() and write() functions to send and receive data to and from the client node.
Stages for Client
The client-side sends the connection requests to the server-side. To perform these several stages have to be
performed on the client side,
A. Socket Connection
Similar to the server-side, the client-side also needs to create a socket using the socket() function and bind the
socket to an address using the bind() function. This will create a socket that can send the connection request to
the server.
B. Connect
The connect() function is used to send the connection request and connect to the server node. The syntax of the
function is,
The socket_descriptor represents the value of the file descriptor returned by the socket() function
during the creation of a socket on the client-side.
The address represents the structure with the information of the address and port number of the server
node to which the connection is to be made.
The length_of_address is the size of the address structure used in the second parameter.
The connect() function returns a value of 0 on successfully connecting with the server and returns a value of -1
on error or the connection fails.
Socket characteristics
Conclusion
Socket programming in C is used to create a connection between two nodes to share data over the
internet.
The server node features listening for a connection signal and establishes a connection between the
server and client node.
The client features sending of connection requests to the server.
There are different modes and options available to establish a connection between the client and server
node.
A socket bound with a port and address is used for sending and receiving the data between client and
server nodes.
The write() and read() functions are used to send and receive data through the sockets.
The “Data-Link layer,” is responsible for maintaining and terminating the established connection
between the network devices over the network channel.
The first is the “medium access control,” which uses the MAC addresses from the network devices
to transmit data between them.
The second layer is the “logical link layer,” which identifies, checks flow control, and performs the
error check for the transmitted data.
The data-link layer handles these data packets by integrating them with the frame structure. The
frame acts as a header for the data packet, containing information about the destination address,
sender address, and other related services.
The final data format in the data-link layer is known as the data frame, which is then transmitted to the
physical layer of the OSI Model.
Next, let’s look at the functions of the data-link layer in the OSI network model.
The working of the data-link layer in the OSI model requires the need functioning of the following
attributes:
1. Framing
The data packets received from the network layer are encapsulated in frames by the data-link layer for bit-
to-bit sharing over the channel.
It is also responsible for restructuring the framed data in the network model, and each data frame is
different from the others.
2. Addressing
The task of adding a physical address to the frame in the header format is known as addressing.
It acts as an identification service for transmitting the frames to multiple network models over the channel.
3. Flow Control
During data transmission, the sender or receiver's data flow may differ, causing network traffic in the
channel.
The Data-link layer in such situations acts as a flow control for the sender side to prevent data overflow at
the receiver side.
4. Access Control
In this network model, when multiple devices share the same communication channel, this leads to data
collision in the network channel.
To prevent such data collision, the data-link layer performs checks on the devices with the same network
channel to avoid data loss.
5. Error Control
During data transmission, due to noise or signal loss, errors might occur in the data being transmitted.
To minimize such data error rate, the data-link layer performs error detection and correction techniques on
the transmitted data.
The data-link layer depending on the communication with the other OSI model is divided into two
types of sub-layers:
The LLC sub-layer is responsible for handling and maintaining the communication between the other layers
of the OSI Model.
It is also responsible for handling error messages and reliability checks for the data.
The MAC sub-layer is responsible for framing the data received from the upper layers.
It also is responsible for data encapsulation and media access control for the data received.
1. the email is sent from the Application layer to the Transport layer.
2. the Transport layer encapsulates the data and adds its own header with its own information, such as which
port will be used and passes the data to the Internet layer
3. the Internet layer encapsulates the received data and adds its own header, usually with information about the
source and destination IP addresses. The Internet layer than passes the data to the Network Access layer
4. the Network Access layer is the only layer that adds both a header and a trailer. The data is then sent through
a physical network link.
Here is a graphical representation of how each layer add its own information:
Each packet (header + encapsulated data) defined by a particular layer has a specific name:
Frame – encapsulated data defined by the Network Access layer. A frame can have both a header and a
trailer.
Packet – encapsulated data defined by the Network layer. A header contains the source and destination IP
addresses.
Segment – encapsulated data as defined by the Transport layer. Information such as the source and
destination ports or sequence and acknowledgment numbers are included in the header.
NOTE
The term decapsulation refers to the process of removing headers and trailers as data passes from lower to upper
layers. This process happens on the computer that is receiving data.
While the TCP/IP model uses terms like segment, packet and frame to refer to a data packet defined
by a particular layer, the OSI model uses a different term: protocol data unit (PDU). A PDU
represent a unit of data with headers and trailers for the particular layer, as well as the encapsulated
data. Since the OSI model has 7 layers, PDUs are numbered from 1 to 7, with the Physical layer
being the first one. For example, the term Layer 3 PDU refers to the data encapsulated at the
Network layer of the OSI model.