0% found this document useful (0 votes)
2 views3 pages

Experiment 13

The document outlines the steps to create a client-server program in Python that facilitates communication between two systems. It provides code examples for both the server and client, detailing how to establish a connection, send and receive messages, and display the output. The expected results include successful message exchange and appropriate output on both the server and client sides.

Uploaded by

Tushar Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Experiment 13

The document outlines the steps to create a client-server program in Python that facilitates communication between two systems. It provides code examples for both the server and client, detailing how to establish a connection, send and receive messages, and display the output. The expected results include successful message exchange and appropriate output on both the server and client sides.

Uploaded by

Tushar Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment-13

Write a Program to connect server with client and passes information from one system to
another and vice versa that by creating / establishing connection

1. Set up the Server Program:


 The server will listen on a specific port for incoming connections from clients.
 Once a connection is established, the server will accept data from the client, process it,
and send a response.
2. Set up the Client Program:
 The client will initiate a connection to the server using the server's IP address and port.
 The client will send a message to the server, receive a response, and display it.

Python Code for Server:


python
Copy
import socket

def start_server():
# Create a socket object
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Get the local machine name


host = '127.0.0.1' # Localhost
port = 12345 # Port to bind to

# Bind the socket to the address and port


server_socket.bind((host, port))

# Enable the server to accept connections (max 5 clients in the queue)


server_socket.listen(5)
print("Server listening on port:", port)

while True:
# Wait for a connection from a client
client_socket, addr = server_socket.accept()
print("Got a connection from", addr)

# Receive data from the client (max 1024 bytes)


data = client_socket.recv(1024)
print("Received from client:", data.decode('utf-8'))

# Send a response to the client


response = "Hello from the server!"
client_socket.send(response.encode('utf-8'))

# Close the connection with the client


client_socket.close()

if name == " main ":


start_server()

Python Code for Client:


python
Copy
import socket

def start_client():
# Create a socket object
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Define the server's IP address and port


host = '127.0.0.1' # Localhost or server IP
port = 12345 # Same port as the server

# Connect to the server


client_socket.connect((host, port))

# Send data to the server


message = "Hello from the client!"
client_socket.send(message.encode('utf-8'))

# Receive the server's response


response = client_socket.recv(1024)
print("Received from server:", response.decode('utf-8'))

# Close the connection


client_socket.close()

if name == " main ":


start_client()

Steps for Execution:


1. Run the Server Program:
o Open a terminal or command prompt.
o Run the server program by executing: python server.py.
o The server will start listening for client connections.
2. Run the Client Program:
o Open a new terminal or command prompt.
o Run the client program by executing: python client.py.
o The client will connect to the server, send a message, and display the server’s
response.
3. Observe the Output:
o Server Output: The server will display the message it receives from the client and
the message it sends back.
o Client Output: The client will display the message it receives from the server.

Expected Results:
 The client and server successfully exchange messages. The server receives a message from the
client and responds back with a predefined message.
 You should see the following output on the server:
pgsql
Copy
Server listening on port: 12345
Got a connection from ('127.0.0.1', <port_number>)
Received from client: Hello from the client!
 The client should output:
pgsql
Copy
Received from server: Hello from the server!

You might also like