LA2
LA2
LA2
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:
Materials Needed:
Activity Steps:
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
if __name__ == "__main__":
start_server()
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
if __name__ == "__main__":
start_client()
python surname_server.py
4. The server will start and wait for a connection from the client, displaying:
python surname_client.py
4. The client will connect to the server and receive the message:
Post-Laboratory Requirements:
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?