Nothing Special   »   [go: up one dir, main page]

Networking

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Network Programming Interfaces (Socket

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.

State Diagram for Server and Client Model


We can understand the role of sockets in establishing a connection between two nodes over a network with the
help of the following diagram,
 The nodes are divided into two types, server node and client node.
 The client node sends the connection signal and the server node receives the connection signal sent by the
client node.
 The connection between a server and client node is established using the socket over the transport layer of the
internet.
 After a connection has been established, the client and server nodes can share information between them using
the read and write commands.
 After sharing of information is done, the nodes terminate the connection.

We will look deeply at how the above process can be performed using socket programming in C in the
following sections.

Stages for Server


Different stages must be performed on the server node to receive a connection sent by the client node. These
stages are discussed elaborately in this section.

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,

int setsockopt(int socket_descriptor, int level, int option_name, const void


*value_of_option, socklen_t option_length);

 The socket is the file descriptor returned by the socket() function.


 The level parameter represents the level at which the option for the socket must be applied. The
SOL_SOCKET represents the socket level and IPPROTO_TCP represents the TCP level.
 The option_name specifies the rules or options that should be modified for the socket. Some useful
options are,
o SO_DEBUG is used to enable the recording of debugging information.
o SO_REUSEADDR is used to enable the reusing of local addresses in the bind() function.
o SO_SNDBUF is used to set the maximum buffer size that can be sent using the socket
connection.
 The option_value is used to specify the value for the options set in the option_name parameter.
 The option_length is the length of the variable used to set the option value.
The function returns a value of 0 of data type int on the successful application of the option and a value of -1 on
failure.

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,

int bind(int socket_descriptor , const struct sockaddr *address, socklen_t length_of_address);

 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,

int connect(int socket_descriptor, const struct sockaddr *address, socklen_t length_of_address);

 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

Sockets share some common characteristics.

 A socket is represented by an integer. That integer is called a socket descriptor.


 A socket exists as long as the process maintains an open link to the socket.
 You can name a socket and use it to communicate with other sockets in a communication domain.
 Sockets perform the communication when the server accepts connections from them, or when it
exchanges messages with them.
 You can create sockets in pairs (only for sockets in the AF_UNIX address family).
 The connection that a socket provides can be connection-oriented or
connectionless. Connection-oriented communication implies that a connection is established,
and a dialog between the programs follows. The program that provides the service (the server
program) establishes the available socket that is enabled to accept incoming connection
requests. Optionally, the server can assign a name to the service that it supplies, which allows
clients to identify where to obtain and how to connect to that service. The client of the service
(the client program) must request the service of the server program. The client does this by
connecting to the distinct name or to the attributes associated with the distinct name that the
server program has designated. It is similar to dialing a telephone number (an identifier) and
making a connection with another party that is offering a service (for example, a plumber).
When the receiver of the call (the server, in this example, a plumber) answers the telephone,
the connection is established. The plumber verifies that you have reached the correct party,
and the connection remains active as long as both parties require it.
 Connectionless communication implies that no connection is established, over which a dialog or
data transfer can take place. Instead, the server program designates a name that identifies
where to reach it (much like a post-office box). If you send a letter to a post office box, you
cannot be absolutely sure that the receiver got the letter. You might need to wait for a
response to your letter. There is no active, real-time connection, in which data is exchanged.

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.

DATA LINK LAYER

What is the Data-Link Layer?

The “Data-Link layer,” is responsible for maintaining and terminating the established connection
between the network devices over the network channel.

The Data-Link Layer has two sublayers:

 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.

Working Of the Data-Link Layer


The data flow between the data-link layer and other layers in the OSI Model, i.e., to begin with, the
network layer will share the data packets with the data-link layer.

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.

Functions of the Data-Link Layer

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.

Sub-Layers of the Data-Link Layer

The data-link layer depending on the communication with the other OSI model is divided into two
types of sub-layers:

1. Logical Link Control (LLC)

This is the upper sub-layer of the data-link layer.

 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.

2. Media Access Control (MAC)

This is the lower sub-layer of the data-link layer.

 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.

Encapsulation in OSI and TCP/IP Models


The term encapsulation is used to describe a process of adding headers and trailers around some
data. This process can be explained with the four-layer TCP/IP model, with each step describing the
role of the layer. For example, here is what happens when you send an email using your favourite
email program (such as Outlook or Thunderbird):

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.

Data encapsulation in the OSI model


Just like with the TCP/IP layers, each OSI layer asks for services from the next lower layer. The lower
layer encapsulates the higher layer’s data between a header (Data Link protocols also add a trailer).

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.

Here is a graphical representation of all the PDUs in the OSI model:

Media Access control protocol

You might also like