Unit 1
Unit 1
Structure
1.0 Introduction
1. 1 Learning Outcomes
1.2 Basics of Networking
1.3 OSI Model and TCP/IP architectures
1.4 Addressing in ComputerNetworks
1.5 Transmission Control Protocol and User Datagram Protocol
1.6 Client-Server Architecture
1.7 Sockets
1.8 Socket Programming in Python
     1.8.1 Creating a Socket
     1.8.2 Server Socket Methods
     1.8.3 Client Socket Methods
     1.8.4 General socket Methods
     1.8.5 Connecting with the Server
     1.8.6 Creating a simple Server
     1.8.7 Creating a simple Client
1.9 Let Us Sum Up
1.10 Check your progress: The key
1.0 Introduction
This unit aims to cover the basics of networking, addressing computer networks,
programming in Python, and creating a simple chat program in Python using sockets.
                                                                                         2
      understand basics of networking, communication models, and addressing in
computer networks
 describe TCP and UDP transport layer protocols, the client-server architecture, and
 explain the usage of sockets in Python & creating a small chat program using sockets
in Python.
These are:
medium.
duplex.
                                                                                             3
                       Figure 1: Data flow in Simplex mode
       o Half-duplex mode: Each device can transmit and receive data, but not
simultaneously. When one device transmits, the other device can only receive
Example: telephone.
communication links, i.e., a network is two or more devices connected through links.
 Node: A node is any device capable of sending or receiving data generated by other
 Link: A communications pathway that transfers data from one device to another.
 There are two types of connections in which nodes can be connected to the same link
                                                                                       4
       o Point-to-point connection: It establishes a dedicated link between two devices
and reserves the link's capacity for transmission between the two devices.
                                                   (    )
           other. In a mesh topology, we need                duplex-mode links. Every device
                                                                                          5
                       Figure 7: Mesh topology
o Star topology: A specialized point-to-point link connects each device to a
together. Each device requires only one link and one I/O port to connect to
                                                                               6
                              Figure 97: Bus topology
       o Ring topology: Only the two devices on either side of the connection have a
receives a signal intended for another device, the repeater regenerates the bits
 There are two best-known network models – OSI (Open Systems Interconnection)
model, which defines a seven-layer network, and the internet model, also known as
                                                                                          7
       Networks are of two types – Local Area Networks (LAN) and Wide Area Networks
(WAN).
 Local Area Networks (LAN): It connects the devices in a single office, building, or
campus and is usually privately owned. Its range is limited to a few kilometers and
image, audio, and video information over large geographic areas such as a country,
 Metropolitan Area Networks (MAN): It has a size between that of a LAN and a
 Hostname: Each device in the network is associated with a unique system device
name.
   i)         How many I/O ports are needed by each device, and how many communication
              links are required to connect 𝑛 devices in case of:
             a) Mesh topology?
             b) Star topology?
      --------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------
                                                                                                                 8
1.3 OSI model and TCP/IP architecture
1. Physical layer: It is responsible for moving individual bits from one hop to the next.
2. Data Link layer: the transmission of frames from one hop to the next is handled by
3. Network layer: This layer sends individual packets from the source host to the
4. Transport layer: the delivery of the message from one process to another is handled
this layer.
7. Application layer: This layer acts as a window via which users and application
The TCP/IP protocol suite comprises five layers – Physical, Data link, Network,
Transport, and Application. TCP/IP represents the three uppermost layers of the OSI
model (Session, Presentation, and Application) by a single layer termed the Application
layer.
Physical addresses, logical addresses, and port addresses are the three types of addresses
 Physical (MAC) address: It is the unique identifier of each host and is associated
                                                                                               9
    usually written as 12 hexadecimal digits, and every byte, i.e., two hexadecimal digits,
is separated by a colon.
Example: 2C:54:91:88:C9:E3
 Logical address: It is the system's network address across the network. It can
two publicly addressed and visible hosts on the Internet can have the same IP
hexadecimal digits, separated using colons, and each group represents 16 bits.
 Port address: Today, computers are devices that can run multiple processes
simultaneously. So, the end objective of data communications is to deliver data to the
The data is transmitted to the correct target host using the physical and IP addresses
and then forwarded to the valid process executing on the destination system using the
port address. A port address is 16 bits in length, so we have 216 ports available. These
                                                                                         10
       Table 1: Port Category and Ranges
Category Range
number. The client socket address is used to identify the client process, whereas the
server socket address is used to determine the server process. The transport layer
requires a pair of socket addresses: one for the client and one for the server. The IP
header includes the client and server's IP addresses, whereas the TCP or UDP header
                                                                                          11
Transmission Control Protocol (TCP) and User Datagram Protocol (UDP) are transport
layer protocols.
 It is a connection-oriented protocol.
 It is a connectionless protocol.
 It is unreliable as it does not guarantee the delivery of packets to the destination host.
 It supports broadcasting.
                                                                                            12
 Check Your Progress 2
i. Mention the port numbers assigned to the services of HTTP, SMTP, and Telnet.
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
ii. What is the difference between the session layer and presentation layer of the OSI
model?
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
                                                                                                           13
A client is the one who requests a service, while a server is the one who provides the
manages, and delivers most of the client’s resources and services. Because all requests
and services are delivered across a network, it is known as the networking computing
model or client-server network. Other systems are connected across a network, and
resources are shared among the various computers under the client-server architecture or
workstations or personal computers, and servers are located elsewhere on the network,
 Mail servers: They are used for sending and receiving emails;
 File servers act as a centralized file location, and multiple users can access these files.
                                                                                            14
      Workstations: They are called client computers. They are subordinate to servers and
storage and retrieval centers for network files, applications, and databases. They have
plenty of storage space and memory to handle several requests coming in from
1.7 Sockets
communication (IPC). A socket is created to connect to the network on either end of the
connection. Every socket's address is unique and comprises an IP address and a port
number.
Sockets are utilized in the majority of client-server applications. The server creates a
socket, assigns it to a network port, and then waits for the client to communicate. After
generating a connection, the client tries to connect to the server socket. Data is
                                                                                            15
                               Figure 13: Socket Programming
 Datagram socket: These are connectionless sockets that use User Datagram Protocol
addressed and routed. Datagram sockets can not guarantee order or dependability.
 Stream socket: These are connection-oriented sockets that use Transmission Control
sequential, and unique flow of error-free data without the need for record boundaries,
an organized way for creating and terminating connections, and reporting errors. On
                                                                                           16
           the Internet, stream sockets are often implemented using TCP to allow applications to
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
Socket programming is a technique for allowing two network nodes to communicate. One
socket listens on a specific port at an IP address, while the other establishes a connection with
it. While the client connects to the server, the server creates the listener socket.
                                                                                              17
      You must use the socket() method in the socket module to create a socket. The
s = socket.socket(socket_family, socket_type,
protocol=0)
where,
For example,
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
The first parameter socket.AF_INET refers to the IPv4 family of IP addresses and
of the hostname and the port number. The socket must not already be bound.
 socket.listen(backlog): This method sets up and starts the TCP listener and
listens for any connections to the socket. The backlog argument is the upper
until the link arrives (blocking). The socket must be assigned an address and
                                                                                      18
          be ready to receive connections. The return value of this function is a pair
(conn, address), with conn being a new socket object for sending and
receiving data across the connection, and address is the address linked to the
 Socket. recv(bufsize[, flags]): The data from the socket is received using this
method. The data received is represented by a bytes object in the return value.
Bufsize specifies the maximum amount of data that can be received at once.
 Socket. send(bytes[, flags]): This method sends the data to the socket. A
remote socket must be connected to this socket. The optional flags argument
has the same significance as the recv() method. The number of bytes sent is
this method. The return value of this method is a pair (bytes, address), where
bytes represent the received data, and address signifies the socket’s address
 Socket. sendto(bytes[, flags], address): This method sends the data to the
socket. Because the target socket is identified by address, the socket should
not be connected to a remote socket. The optional flags argument has the
                                                                                      19
    same significance as the recv() method. It also returns the number of bytes
sent.
 Socket.close(): This method closes the socket. Following the execution of this
method, all subsequent operations on the socket object will fail. There will be
no more data sent to the remote end. When the garbage collector of Python is
                                                                                20
  Check Your Progress 4
socket module?
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
(ii) What does the parameter bufsize signify in the socket.recv() method?
…………………………………………………………………………………………
…………………………………………………………………………………………
……………………………………………………………………………………
(iii) What is the use of the backlog parameter in the socket.listen() method?
…………………………………………………………………………………………
…………………………………………………………………………………………
……………………………………………………………………………………
We can only connect with the server if we know its IP address. The
server’s IP address.
google_ip = socket.gethostbyname('www.google.com')
print(google_ip)
                                                                                          21
      Output:
173.194.211.106
Note that this IP address may change each time you run the above code because
everywhere. So, each server will respond to a range of addresses, and a given IP
address may move from one server to another depending on various load factors.
 To create a server, we use the socket method available in the socket module to
create a socket object. This socket object is then used to call other methods to
 The accept() function of the returned object is then called. This method waits
for a client to connect to the port you provided before returning a connection
import socket
s = socket.socket()
host = socket.gethostname()
port = 123
                                                                                       22
        # Bind to the port.
s.bind((host, port))
queued connections.
s.listen(3)
while True:
connection.close()
 To create a client, we use the socket method available in the socket module to
 Once the connection is opened, you can read from it like any I/O object.
                                                                                        23
    # Import the socket module first.
import socket
s = socket.socket()
host = socket.gethostname()
port = 123
s.connect((host, port))
print(str(s.recv(1024), 'utf-8'))
s.close()
obtained:
On server.py’s terminal:
On client.py’s terminal:
Let’s summarise:
We first created a socket object in the server program, fetched the local machine
name, bound a port number to specify the service, and then started listening for
                                                                               24
      connection requests. Once a request arrives, it is accepted, followed by sending a
The client program creates a socket object, and a connection request is sent to the
server on its address and the specified port number. Once the request is accepted, a
Once the communication is done, both client and server close their sockets.
We will now create a simple chat program consisting of two files – sender.py and
receiver.py.
sender.py will take a message as input and send it to receiver.py, which will print the
message received and then send back an acknowledgment message to sender.py. The
program is simple and very similar to the client and server programs we discussed in the
previous sections.
import socket
s = socket.socket()
host = socket.gethostname()
port = 123
s.bind((host, port))
s.listen(3)
while True:
                                                                                             25
    # Take the message as input
connection.send(message)
print('Message sent!')
connection.close()
import socket
s = socket.socket()
host = socket.gethostname()
port = 123
s.connect((host, port))
s.send(message)
print('Message sent!')
s.close()
                                                              26
   Output for the above program:
Terminal of sender.py:
Message sent!
Terminal of receiver.py:
Message sent!
Let’s summarise:
The procedure is almost similar to that of the previous program, except that here, the
sender (server) program takes the message as input from the user and sends it to the
receiver (client), which in turn, takes the reply message as input from the user, and sends
1.
a) In a mesh topology, (𝑛 − 1) I/O ports are needed by each device, and 𝑛(𝑛 −
b) In a star topology, a single I/O port is needed by each device, and 𝑛 links are
required in total.
                                                                                           27
 2.
i. The port numbers assigned to HTTP, SMTP, and Telnet services are 80, 25,
ii. The session layer manages dialog control and synchronization, whereas the
3.
i. The difference between a client and a server is that a client is the one who
requests a service, while a server is the one who provides the service.
4.
module is that the socket.recv() method is used to receive data from the socket in
the case of TCP. The client and server have already established a connection. The
return value of the socket.recv() method is a bytes object representing the data
received, whereas the socket.recvfrom() method is used to receive data from the
socket in the case of UDP, where no prior connection is established between the
client and the server. The return value of the socket.recvfrom() method is a pair
(bytes, address), where bytes represent the data received, and address denotes the
ii. The parameter bufsize in the socket.recv() method signifies the maximum amount
                                                                                              28
iii. The backlog parameter in the socket.listen() method specifies the maximum
29