You are on page 1of 7

EXPERIMENT 3.

Student Name: Rajdeep Das UID:20BCS6058


Branch: BE-CSE Section/Group: AIML 1- B
Semester: 5TH Date of Performance:31-10-22
Subject Name: Computer Networks lab
Subject Code:20 CSP-342

1. Aim/Overview of the practical:


Using Socket Programming implement the connectionless oriented service using
standard ports in any programming language (we use Python).

2. Tasks to be done:

Identify the connection establishment techniques and features.

3. Algorithm/Steps of Experiment:

a. Make a file for server and implement a socket.


b. Bind the socket with localhost and an unused system port.
c. Then we make a client file in it we implement a socket with the same host and port as
the server file.
d. We first run the server file and let the socket to get created and then we run
the client file and let the connection of the socket get established via host and
port.
e. Once the server and client get successfully connected, we can send text to
server and the server will respond with the acknowledgement and displays the
message which we sent.
4. Theory:
a. Socket:
A socket is one endpoint of a two-way communication link between two
programs running on the network. The socket mechanism provides a means of
inter-process communication (IPC) by establishing named contact points
between which the communication take place.

Socket are generally employed in client server applications. The server creates a
socket, attaches it to a network port address then waits for the client to contact
it. The client creates a socket and then attempts to connect to the server socket.
When the connection is established, transfer of data takes place.

b. The client-server model:


The Client-server model is a distributed application structure that partitions task
or workload between the providers of a resource or service, called servers, and
service requesters called clients. In the client-server architecture, when the
client computer sends a request for data to the server through the internet, the
server accepts the requested process and deliver the data packets requested
back to the client. Clients do not share any of their resources. Examples of
Client-Server Model are Email, World Wide Web, etc.

c. TCP:
Transmission Control Protocol (TCP) is a standard that defines how to establish
and maintain a network conversation by which applications can exchange data.
TCP is important because it establishes the rules and standard procedures for
the way information is communicated over the internet. It is the foundation for
the internet as it currently exists and ensures that data transmission is carried
out uniformly, regardless of the location, hardware or software involved.

5. Coding:
a. Server.py
import socket as skt

try:
server_socket = skt.socket(skt.AF_INET, skt.SOCK_STREAM)
server_socket.bind(("localhost", 9876))

except:
print("Error while creating a server socket")

print("Waiting for a connection...")


server_socket.listen(3)
while True:
try:
client_socket, client_address = server_socket.accept()

except:
print("Error while connecting to the client")

print("Connection established to client ", client_address)


received = client_socket.recv(1024).decode()

print(f"Received:", received)
sending_message = "Hello " + received
client_socket.send(bytes(sending_message, "utf-8"))

client_socket.close()

b. Client.py
import socket as skt

try:
client_socket = skt.socket(skt.AF_INET, skt.SOCK_STREAM)

except:
print("Error while creating the socket")

print("Connecting to the server")

try:
client_socket.connect(("localhost", 9876))

except:
print("Error while connecting to the server")

print("Connected to the server")


name = input("Enter your name: ")

client_socket.send(bytes(name, "utf-8"))

received_message = client_socket.recv(1024).decode()
print(received_message)
6. Result/Output/Writing Summary:

7. Learning outcomes (What I have learnt):


a. Came to know about socket.
b. How to communicate via a host and port.
c. Learnt how to code a TCP server-client model using python

Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):

Sr. No. Parameters Marks Obtained Maximum Marks


1.
2.
3.

You might also like