EXP6
EXP6
EXP6
Introduction
Required Tools
• Python 3
• The socket library (included with Python)
• The thread library (included with Python)
• An Integrated Development Environment (IDE), such as Visual Studio Code or
PyCharm
Objectives
[email protected]
Experiment Steps
[email protected]
Server Code
Save the following code in a file, e.g., server.py:
import socket
# Server settings
server_ip = '127.0.0.1' # Localhost
server_port = 12345 # Arbitrary port, can be changed
# Set up Server Socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((server_ip, server_port))
server_socket.listen(1) # Allow one connection
print(f"Server is listening on {server_ip}:{server_port}...")
# Accept a connection from the client
client_socket, client_address = server_socket.accept()
print(f"Connected to {client_address}")
# Receive a message from the client
message = client_socket.recv(1024).decode() # Receive up to 1024 bytes
print("Received message from Client:", message)
# Close the connection
client_socket.close()
server_socket.close()
[email protected]
[email protected]
Client Code
Save the following code in a file, e.g., client.py:
import socket
# Server connection settings
server_ip = '127.0.0.1'
server_port = 12345
# Set up Client Socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to the server
client_socket.connect((server_ip, server_port))
print(f"Connected to server at {server_ip}:{server_port}")
# Send a message to the server
message = "Hello from Client!"
client_socket.sendall(message.encode())
print("Message sent to Server:", message)
# Close the connection
client_socket.close()
[email protected]
[email protected]
Server Received Massage
[email protected]
Code for client- server. In the same pc
import socket
import threading
server_ip = '0.0.0.0'
server_port = 6000
def run_server():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((server_ip, server_port))
server_socket.listen(1)
print(f"Server is listening on {server_ip}:{server_port}...")
client_socket, client_address = server_socket.accept()
print(f"Connected to {client_address}")
message = client_socket.recv(1024).decode()
print(f"Received message from Client: {message}")
client_socket.close()
server_socket.close()
def run_client():
import time
time.sleep(1)
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((server_ip, server_port))
print(f"Connected to server at {server_ip}:{server_port}")
message = "Hello from Client on the same machine!"
client_socket.sendall(message.encode())
print(f"Message sent to Server: {message}")
client_socket.close()
server_thread = threading.Thread(target=run_server)
client_thread = threading.Thread(target=run_client)
server_thread.start()
client_thread.start()
server_thread.join()
client_thread.join()
[email protected]
[email protected]
. Observing the Results
• When you run the server, it will display that it is waiting for a connection.
• After running the client, a connection message will appear on the server’s
terminal along with the received message.
• The client terminal will also display a message confirming that the message was
sent to the server.
Code Explanation
• Server:
• Listens for connections on 127.0.0.1:12345.
• Accepts the client connection and receives the message.
• Prints the received message and closes the connection.
• Client:
• Connects to the server at 127.0.0.1:12345.
• Sends a text message.
• Closes the connection after sending the message.
Important Notes
[email protected]
Modifying the Server Code to Receive Multiple Messages
In the server code, we’ll add a while loop to allow the server to receive multiple
messages from the client. The loop will end if the server receives a message with a
specific word, such as "exit".
import socket
# IP and port settings
server_ip = '127.0.0.1' # local address
server_port = 12345
except Exception as e:
print(f"An error occurred: {e}")
# Close the connection
client_socket.close()
server_socket.close()
[email protected]
[email protected]
Modifying the Client Code to Send Multiple Messages
In the client code, we’ll also add a while loop to allow the user to input messages
and send them to the server continuously. The client will keep sending messages
until the user types "exit".
import socket
try:
while True:
# User inputs a message
message = input("Enter message (or 'exit' to quit): ")
except Exception as e:
print(f"An error occurred: {e}")
finally:
# Close the client connection
client_socket.close()
[email protected]
[email protected]
Server Received Multiple Messages from client
[email protected]
Modifying to send and receive (client- server) In the same pc
import socket
import threading
import time
[email protected]
# Wait a few seconds to ensure the server starts first
time.sleep(1)
try:
while True:
# Get message input from the user
message = input("Enter message (or 'exit' to disconnect): ")
client_socket.sendall(message.encode())
if message.lower() == "exit":
print("Disconnecting from server.")
break
[email protected]
[email protected]
Explanation of Modifications
1. In the Server:
• The while loop (while True) allows the server to keep receiving messages from
the client.
• If the server receives the message "exit", it will end the connection.
• The server sends a response back to the client after receiving each message,
confirming its receipt.
2. In the Client:
• Adding a while True loop allows the user to input messages and send them
repeatedly to the server.
• Exiting the loop by typing "exit" will close the connection with the server.
• After sending each message, the client waits for a response from the server and
prints it.
1. Start the server first using the modified server code (server.py).
2. Start the client using the modified client code (client.py).
3. Enter multiple messages in the client window to see how they are received and
processed in the server window.
4. To end the connection, type "exit" in the client window, and both the client and
server will close the connection properly.
[email protected]
GOOD LUCK