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

LA2

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

Laboratory Activity: Basic Client-Server Socket Programming

Objective:

In this laboratory activity, you will learn how to implement a basic client-server communication
system using TCP sockets in Python. You will create both a server that listens for client
connections and a client that connects to the server and receives a message.

Learning Outcomes:

By the end of this activity, you should be able to:

1. Understand how TCP/IP socket programming works in Python.


2. Implement a basic server that listens for incoming client connections.
3. Create a client that connects to the server and receives data.
4. Apply error-handling techniques to ensure the reliability of the client-server
communication.

Materials Needed:

1. A computer with Python installed (preferably version 3.x).


2. Text editor (e.g., VS Code, notepad) to write and run the code.

Activity Steps:

Step 1: Create the Server (surname_server.py)

1. Open a text editor and create a file named surname_server.py.


2. In the surname_server.py file, write the following code:

import socket

def start_server():
# Define host and port for the server
host = '127.0.0.1' # Localhost (the server will run on your machine)
port = 12345 # Port number for the server to listen on

# Create a TCP/IP socket


server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the address and port


server_socket.bind((host, port))

# Start listening for incoming connections


server_socket.listen(1)

print(f"Server listening on {host}:{port}...")


# Wait for a client to connect
client_socket, client_address = server_socket.accept()
print(f"Connection established with {client_address}")

# Send a message to the client


message = "Hello from the server!"
client_socket.send(message.encode())

# Close the connection


client_socket.close()

if __name__ == "__main__":
start_server()

Step 2: Create the Client (surname_client.py)

1. Open a text editor and create a file named surname_client.py.


2. In the surname_client.py file, write the following code:

import socket

def start_client():
# Define the server's host and port
host = '127.0.0.1' # The server's IP address
port = 12345 # The port number the server is listening on

# Create a TCP/IP socket


client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect to the server


client_socket.connect((host, port))

# Receive the message from the server


message = client_socket.recv(1024)
print("Received from server:", message.decode())

# Close the connection


client_socket.close()

if __name__ == "__main__":
start_client()

Step 3: Run the Server

1. Open a terminal or command prompt.


2. Navigate to the folder where you saved the surname_server.py file.
3. Run the server by typing:

python surname_server.py

4. The server will start and wait for a connection from the client, displaying:

Server listening on 127.0.0.1:12345...


Step 4: Run the Client

1. Open another terminal or command prompt.


2. Navigate to the folder where you saved the surname_client.py file.
3. Run the client by typing:

python surname_client.py

4. The client will connect to the server and receive the message:

Received from server: Hello from the server!

Post-Laboratory Requirements:

1. Screenshot output of client and server.

Post-Laboratory Questions:

2. What happens if you try to run the client before starting the server?
3. Modify the server to handle multiple clients. How does the program behavior change
when multiple clients connect simultaneously?
4. What would happen if the server crashes while the client is connected? How can you
handle such situations using exception handling in Python?
5. What is the significance of the IP address 127.0.0.1? Can you change it to 0.0.0.0 or a
different IP? Explain.
6. What does the client_socket.recv(1024) function do? What would happen if you
changed the size parameter?

Additional Tasks (for advanced students):

1. Client-Server with Dynamic Interaction:


o Modify the program to allow the client to send a message to the server, and the
server should reply back.
o Use a loop to keep the server running and interact with multiple clients.
2. Error Handling:
o Add error handling in both the client and server to manage situations like
network errors, timeouts, or connection errors.
3. Use Multi-threading on the Server:
o Modify the server code to handle multiple clients simultaneously using threads.
o Each client should run in its own thread so that the server can handle multiple
connections concurrently.

You might also like