0% found this document useful (0 votes)
17 views5 pages

Client Lab4

The document outlines steps for implementing basic UDP and TCP Client-Server communication, including programming tasks for connectionless communication and reliable data transfer. It also covers a UDP Pinger lab for measuring Round Trip Time (RTT) and a Multi-Threaded TCP Server for handling multiple clients concurrently. Each section includes code examples and learning outcomes related to networking concepts.

Uploaded by

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

Client Lab4

The document outlines steps for implementing basic UDP and TCP Client-Server communication, including programming tasks for connectionless communication and reliable data transfer. It also covers a UDP Pinger lab for measuring Round Trip Time (RTT) and a Multi-Threaded TCP Server for handling multiple clients concurrently. Each section includes code examples and learning outcomes related to networking concepts.

Uploaded by

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

Client-Server Communication Labs

This document contains the steps for basic UDP and TCP Client-Server communication along
with additional labs for UDP pinger and multi-threaded TCP server. The activities include
programming tasks to demonstrate connectionless communication, reliable data transfer, round
trip time measurement, and concurrency in networking.

1. Basic UDP Client-Server Communication

Objective: Introduce students to UDP-based communication.


Activity Steps:
1. Implement a UDP client that reads a line of text from the keyboard and sends it to the UDP
server.
2. The server receives the message, converts it to uppercase, and sends it back to the client.
3. The client receives the modified message and displays it on the screen.
Learning Outcome: Understanding connectionless communication and datagram-based
messaging.

UDP Client Code

import socket

# UDP Client
server_address = ('localhost', 12345) # Server address
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP socket

while True:
message = input("Enter message to send: ") # User input
if message.lower() == 'exit': # Exit condition
break
sock.sendto(message.encode(), server_address) # Send message
data, _ = sock.recvfrom(4096) # Receive server response
print(f"Received from server: {data.decode()}")

sock.close()

UDP Server Code

import socket
# UDP Server
server_address = ('localhost', 12345)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP socket
sock.bind(server_address)

while True:
data, client_address = sock.recvfrom(4096) # Receive message
if data:
modified_message = data.decode().upper() # Convert to uppercase
sock.sendto(modified_message.encode(), client_address) # Send response

2. Basic TCP Client-Server Communication

Objective: Introduce students to TCP-based communication and reliable data transfer.


Activity Steps:
1. Implement a TCP client that establishes a connection with a TCP server.
2. The client sends a lowercase string to the server.
3. The server converts it to uppercase and sends it back.
4. The client displays the modified string.
Learning Outcome: Understanding connection-oriented communication and the three-way
handshake.

TCP Client Code

import socket

# TCP Client
server_address = ('localhost', 12345)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # TCP socket

sock.connect(server_address) # Establish connection

message = input("Enter message to send: ").lower() # User input


sock.sendall(message.encode()) # Send message
data = sock.recv(1024) # Receive server response

print(f"Received from server: {data.decode()}")


sock.close()

TCP Server Code

import socket

# TCP Server
server_address = ('localhost', 12345)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # TCP socket
sock.bind(server_address)
sock.listen(1)

print("Waiting for a connection...")


connection, client_address = sock.accept() # Accept connection
print(f"Connection from {client_address}")

data = connection.recv(1024) # Receive message


if data:
modified_message = data.decode().upper() # Convert to uppercase
connection.sendall(modified_message.encode()) # Send response

connection.close()

3. UDP Pinger Lab

Objective: Students how to measure Round Trip Time (RTT) and handle packet loss using UDP.
Activity Steps:
1. The client sends 10 ping messages to the server.
2. The server responds with pong messages.
3. The client calculates RTT and prints the response time for each ping.
4. If a packet is lost, the client prints a timeout message.
Learning Outcome: Understanding reliability issues in UDP and handling packet loss.

UDP Pinger Client Code

import socket
import time

server_address = ('localhost', 12345)


sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

for i in range(10):
message = f"Ping {i+1}"
start_time = time.time() # Record start time
sock.sendto(message.encode(), server_address) # Send ping message
try:
data, _ = sock.recvfrom(4096) # Receive pong response
round_trip_time = time.time() - start_time # Calculate RTT
print(f"Received {data.decode()} - RTT: {round_trip_time:.4f} seconds")
except socket.timeout:
print(f"Ping {i+1} - Timeout (packet lost)")

sock.close()

UDP Pinger Server Code

import socket

server_address = ('localhost', 12345)


sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(server_address)

while True:
data, client_address = sock.recvfrom(4096) # Receive ping
if data:
sock.sendto(f"Pong: {data.decode()}".encode(), client_address) # Send pong

4. Multi-Threaded TCP Server

Objective: Show students how to handle multiple TCP clients concurrently.


Activity Steps:
1. Implement a TCP server that can handle multiple clients using threads.
2. Each client sends a request, and the server responds uniquely to each client.
3. The server should log each connection with the client's IP address and port.
Learning Outcome: Understanding concurrency in networking and socket programming.

Multi-Threaded TCP Server Code

import socket
import threading

def handle_client(client_socket, client_address):


print(f"Handling client {client_address}")
data = client_socket.recv(1024) # Receive message
if data:
modified_message = data.decode().upper() # Convert to uppercase
client_socket.sendall(modified_message.encode()) # Send response
client_socket.close()

server_address = ('localhost', 12345)


server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(server_address)
server_socket.listen(5)

print("Server is listening for connections...")

while True:
client_socket, client_address = server_socket.accept() # Accept client connection
client_handler = threading.Thread(target=handle_client, args=(client_socket, client_address))
client_handler.start() # Handle each client in a new thread

You might also like