Name: Sonali Ahire
Roll.No: A-60
Batch: A4
Experiment No.10
Aim: To study and implementation of Socket Programming using TCP.
Software Requirements: Python
Lab Objective: After completion of this experiment, you will able ,
• Implement Socket Programming for clien server architecture.
Theory: Socket creates an endpoint for communication and returns a descriptor.
The domain parameter specifies a common domain this select the protocol
family which will be used for communication.
Algorithm:
1. Create a server socket.
2. Then create a client socket.
3. DataInputStream and DatOutputStream is to abstract different ways to input and
output
4. whether the stream is a file, a web page.
5. Specify the IP address and port number for socket.
6. Close all streams.
7. Close server and client socket.
8. Stop
Program:
Server-
import socket
# Create a socket object
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to a host and port
host = '127.0.0.1' # localhost
port = 12345
server_socket.bind((host, port))
# Listen for incoming connections
server_socket.listen(5)
print("Server is listening on", host, port)
while True:
# Accept a new connection
client_socket, addr = server_socket.accept()
print("Connected to:", addr)
# Send a message to the client
client_socket.send(b"Hello from server!")
# Receive a message from the client
message = client_socket.recv(1024).decode()
print("Message from client:", message)
# Close the connection
client_socket.close()
Client-
import socket
# Create a socket object
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to the server
host = '127.0.0.1' # localhost
port = 12345
client_socket.connect((host, port))
# Receive a message from the server
message = client_socket.recv(1024).decode()
print("Message from server:", message)
# Send a message to the server
client_socket.send(b"Hello from client!")
# Close the connection
client_socket.close()
Output:
Conclusion: Thus, we studied and implemented the client/server model of socket
programming.
Lab Outcome: Implement the socket programming for client server architecture.