Experiment No.
2
Student Name: Mohd Fahad Chougle
PRN: 211214
Course Name : Distributed Computing
Course Code: CSL801
Department: Computer Engineering
Name Of Organization: M.H. Saboo Siddik College of Engineering
Aim: Implementation of Client/Server communication using socket.
Theory:
Client-server communication is a network architecture where a client requests services, and a server
processes and responds to those requests. This model is commonly used in web applications, databases,
cloud computing, and distributed systems to enable efficient data exchange between different machines or
applications.
How Client-Server Communication Works with Sockets:
A socket is an endpoint for communication between two devices over a network. It allows clients and
servers to send and receive data. Communication can be established using two main protocols:
● TCP (Transmission Control Protocol) – Connection-oriented, ensures reliable data transfer.
● UDP (User Datagram Protocol) – Connectionless, faster but does not guarantee data
delivery. Steps in Client-Server Communication:
1. Server Initialization: The server creates a socket, binds it to an IP address and port, and listens for
client requests.
2. Client Connection: The client creates a socket and attempts to connect to the server.
3. Data Exchange: The client sends a request, and the server processes it and responds.
4. Connection Termination: The connection is closed after the communication is complete.
Applications of Client-Server Communication
● Web Browsing: A browser (client) requests web pages from a web server.
● Email Services: Email clients communicate with mail servers for sending/receiving emails.
● Online Gaming: Game clients communicate with central game servers.
● Database Access: Applications connect to database servers for storing and retrieving data.
● Cloud Computing: Cloud services (like Google Drive) use client-server models to store and
access data remotely.
Advantages of Client-Server Communication
● Centralized Control: The server manages data and resources efficiently.
● Security: Access control and authentication can be implemented at the server level.
● Scalability: More clients can connect as needed.
● Efficient Resource Management: Multiple clients can share server resources.
Disadvantages of Client-Server Communication
● Server Dependency: If the server fails, clients cannot access services.
● Network Congestion: High traffic may slow down performance.
● Higher Costs: Maintaining powerful servers requires investment.
● Complex Setup: Security, load balancing, and data management require careful configuration.
Program:
server.py:
import socket
server_socket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host=socket.gethostname()
port=9999
server_socket.bind((host,
port))
server_socket.listen(8)
print("(server) listening on {}:{}".format(host,port))
client_socket,addr=server_socket.accept()
print("(server) got connection from client {0}".format(addr))
while True:
data=client_socket.recv(1024).decode("utf-8")
if data=='exit':
print("Exit") break
else:
print("client's msg: ",data)
message=input('(server) enter a response:') if
message=='exit':
client_socket.send(message.encode()) break
else:
client_socket.send(bytes(message,"u
tf-8")) client.py:
import socket
client_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host=socket.gethostname()
port=9999
client_socket.connect((ho
st,port)) while True:
message=input('(client) enter a message:')
if message=='exit':
client_socket.send(message.encode())
break
else:
client_socket.send(bytes(message,"utf-8"))
data=client_socket.recv(1024).decode("utf-8")
if data=='exit':
print("Exit")
break
else:
print("server's msg:",data)
Output:
Server:
> python server.py
(server) listening on DESKTOP-2CBD4IJ:9999
(server) got connection from client ('192.168.104.104', 65265)
client's msg: hii
(server) enter a response:hello
client's msg: i am client!
(server) enter a response:i am server!
client's msg: ok bye
(server) enter a response:exit
Client:
> python client.py
(client) enter a message:hii
server's msg: hello
(client) enter a message:i am client!
server's msg: i am server!
(client) enter a message:ok bye
Exit
Conclusion:
Hence, we have successfully implemented client-server communication using sockets, enabling efficient
data exchange between a client and server in a networked environment.