0% found this document useful (0 votes)
2 views

Week-12 App assignment

The document outlines a series of Python programming assignments focused on TCP and UDP networking. It includes code examples for creating TCP and UDP clients and servers, handling messages, calculating sums, and sending files. Each section provides code snippets along with expected outputs for various functionalities such as echoing messages, sending random numbers, and file transfers.

Uploaded by

kpnx8cpmmk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Week-12 App assignment

The document outlines a series of Python programming assignments focused on TCP and UDP networking. It includes code examples for creating TCP and UDP clients and servers, handling messages, calculating sums, and sending files. Each section provides code snippets along with expected outputs for various functionalities such as echoing messages, sending random numbers, and file transfers.

Uploaded by

kpnx8cpmmk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

ADVANCED

PROGRAMMING
PRACTICE
ASSIGNMENT-12

RA2211026010486
G.Abiram cse-Aiml

Programs:
1.Develop a simple Python program of TCP, client that
can connect to the server and
client can send a "Hello, Server!" message to the
server.

Ans:
import socket
# Server's host and port
server_host = 'localhost' # Change this to the server's
hostname or IP address
server_port = 12345 # Change this to the server's port

# Create a socket object


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

try:
# Connect to the server
client_socket.connect((server_host, server_port))

# Send a message to the server


message = "Hello, Server!"
client_socket.send(message.encode())

# Receive a response from the server


response = client_socket.recv(1024).decode()
print("Server says:", response)

except ConnectionRefusedError:
print("Connection to the server failed. Make sure
the server is running and check the host and port.")
finally:
# Close the socket
client_socket.close()

Output:
Server says: Hello, Client!

2.Develop a Python program that allows the TCP client


to send a list of numbers to the
server. The server should calculate and return the sum
of the numbers to the client.

Ans:
1.Server(server.py):
import socket
import pickle

# Server's host and port


server_host = 'localhost' # Change this to the server's
hostname or IP address
server_port = 12345 # Change this to the server's port

# Create a socket object


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

# Bind the socket to the server address


server_socket.bind((server_host, server_port))

# Listen for incoming connections


server_socket.listen(5)
print("Server is ready to receive data...")

while True:
client_socket, client_address =
server_socket.accept()

data = client_socket.recv(1024)
if not data:
break

numbers = pickle.loads(data) # Deserialize the list of


numbers
result = sum(numbers)

client_socket.send(str(result).encode())

client_socket.close()
Output:
Server is ready to receive data…

2.client (client.py)
import socket
import pickle

# Server's host and port


server_host = 'localhost' # Change this to the server's
hostname or IP address
server_port = 12345 # Change this to the server's port

# Create a socket object


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

try:
# Connect to the server
client_socket.connect((server_host, server_port))

# Send a list of numbers to the server


numbers = [1, 2, 3, 4, 5]
data = pickle.dumps(numbers) # Serialize the list of
numbers
client_socket.send(data)

# Receive the sum from the server


response = client_socket.recv(1024).decode()
print("Sum of numbers received from the server:",
response)

except ConnectionRefusedError:
print("Connection to the server failed. Make sure
the server is running and check the host and port.")
finally:
# Close the socket
client_socket.close()

Output:
Sum of numbers received from the server: 15

3.Create a Python UDP client that sends a "UDP


Message" packet to a UDP server.
Demonstrate the sending and receiving of the packet.
Ans:
UDP server ( server.py)
import socket

# Server's host and port


server_host = 'localhost' # Change this to the server's
hostname or IP address
server_port = 12345 # Change this to the server's UDP
port

# Create a UDP socket


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

# Bind the socket to the server address


server_socket.bind((server_host, server_port))

print("Server is ready to receive UDP messages...")

while True:
data, client_address = server_socket.recvfrom(1024)
print(f"Received message from {client_address}:
{data.decode()}")

UDP CLIENT (client.py):


import socket

# Server's host and port


server_host = 'localhost' # Change this to the server's
hostname or IP address
server_port = 12345 # Change this to the server's UDP
port

# Create a UDP socket


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

# Message to send
message = "UDP Message"

# Send the message to the server


client_socket.sendto(message.encode(), (server_host,
server_port))

print(f"Sent message to {server_host}:{server_port}:


{message}")

Output:
Server(servers.py):
Server is ready to receive UDP messages...
Received message from ('127.0.0.1', 56789): UDP
Message

Client (client.py):
Sent message to localhost:12345: UDP Message

4.Create a Python UDP client that sends a random


number to the UDP server. The server
should check if the number is even or odd and send
the result back to the client.

Ans:
UDP server (server.py)
import socket

# Server's host and port


server_host = 'localhost' # Change this to the server's
hostname or IP address
server_port = 12345 # Change this to the server's UDP
port

# Create a UDP socket


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

# Bind the socket to the server address


server_socket.bind((server_host, server_port))

print("Server is ready to receive UDP messages...")

while True:
data, client_address = server_socket.recvfrom(1024)
received_number = int(data.decode())
result = "Even" if received_number % 2 == 0 else
"Odd"
server_socket.sendto(result.encode(),
client_address)

UDP client (client.py):


import socket
import random

# Server's host and port


server_host = 'localhost' # Change this to the server's
hostname or IP address
server_port = 12345 # Change this to the server's UDP
port

# Create a UDP socket


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

# Generate a random number


random_number = random.randint(1, 100)
print(f"Sending random number to server:
{random_number}")

# Send the random number to the server


client_socket.sendto(str(random_number).encode(),
(server_host, server_port))

# Receive and print the result from the server


result, server_address = client_socket.recvfrom(1024)
print(f"Received result from server ({server_address}):
{result.decode()}")

Output:
Server ( server.py)
Server is ready to receive UDP messages...

Client (client.py)
Sending random number to server: 42
Received result from server (('127.0.0.1', 12345)): Even
5.Write a Python program to create a UDP server that
listens on port 54321. Ensure the
server can receive UDP packets from clients.

Ans:
import socket

# Server's host and port


server_host = 'localhost' # Change this to the server's
hostname or IP address
server_port = 54321 # Port number

# Create a UDP socket


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

# Bind the socket to the server address


server_socket.bind((server_host, server_port))
print(f"Server is ready to receive UDP packets on port
{server_port}...")

while True:
data, client_address = server_socket.recvfrom(1024)
print(f"Received packet from {client_address}:
{data.decode()}")

Output:
Server is ready to receive UDP packets on port 54321...
Received packet from ('127.0.0.1', 54321): Hello,
Server!
Received packet from ('192.168.0.100', 54321): UDP
data from client
Received packet from ('10.0.0.2', 54321): Another UDP
packet
6.Extend the UDP server to respond to the client's
"UDP Message" packet with an
acknowledgment message. Provide the code for the
server-client interaction.

Ans:
UDP server (server.py)
import socket

# Server's host and port


server_host = 'localhost' # Change this to the server's
hostname or IP address
server_port = 54321 # Port number

# Create a UDP socket


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

# Bind the socket to the server address


server_socket.bind((server_host, server_port))

print(f"Server is ready to receive and respond to UDP


packets on port {server_port}...")

while True:
data, client_address = server_socket.recvfrom(1024)
received_data = data.decode()

print(f"Received packet from {client_address}:


{received_data}")

# Respond with an acknowledgment message


acknowledgment = "ACK: " + received_data
server_socket.sendto(acknowledgment.encode(),
client_address)

UDP client (client.py)


import socket
# Server's host and port
server_host = 'localhost' # Change this to the server's
hostname or IP address
server_port = 54321 # Port number

# Create a UDP socket


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

# Message to send
message = "UDP Message"

# Send the message to the server


client_socket.sendto(message.encode(), (server_host,
server_port))

# Receive and print the acknowledgment from the


server
acknowledgment, server_address =
client_socket.recvfrom(1024)
print(f"Received acknowledgment from server
({server_address}): {acknowledgment.decode()}")

Output:
Server ( server.py)
Server is ready to receive and respond to UDP packets
on port 54321...
Received packet from ('127.0.0.1', 54321): UDP
Message

Client ( client.py)
Received acknowledgment from server (('127.0.0.1',
54321)): ACK: UDP Message

7.Implement a Python program that calculates and


displays the time taken for a TCP
client to connect to the server and receive a response.
Measure the time elapsed in
seconds.

Ans:
import socket
import time

# Server's host and port


server_host = 'localhost' # Change this to the server's
hostname or IP address
server_port = 12345 # Change this to the server's port

# Create a socket object


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

# Bind the socket to the server address


server_socket.bind((server_host, server_port))
# Listen for incoming connections
server_socket.listen(5)

while True:
client_socket, client_address =
server_socket.accept()
data = client_socket.recv(1024)
client_socket.send("Hello, Client!".encode())
client_socket.close()

Output:
Response from server: Hello, Client!
Time taken to connect and receive response:
0.00532078742980957 seconds

8.Create a TCP server that echoes back any message it


receives from a client. Develop a
Python client to send messages to the server and
display the echoed response.
Ans:
import socket

# Server's host and port


server_host = 'localhost' # Change this to the server's
hostname or IP address
server_port = 12345 # Change this to the server's port

# Create a socket object


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

# Bind the socket to the server address


server_socket.bind((server_host, server_port))

# Listen for incoming connections


server_socket.listen(5)
print("Server is ready to echo messages...")

while True:
client_socket, client_address =
server_socket.accept()
data = client_socket.recv(1024)
client_socket.send(data) # Echo the received
message back to the client
client_socket.close()

Output:
Server is ready to echo messages...

9.Develop a simple Python program that sends a small


text file from a TCP client to a
TCP server. Confirm that the file is received and saved
correctly.
Ans:
import socket

# Server's host and port


server_host = 'localhost' # Change this to the server's
hostname or IP address
server_port = 12345 # Change this to the server's port

# Create a socket object


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

# Bind the socket to the server address


server_socket.bind((server_host, server_port))

# Listen for incoming connections


server_socket.listen(5)
print("Server is ready to receive files...")

while True:
client_socket, client_address =
server_socket.accept()
received_file = open("received_file.txt", "wb") #
Open a file to save received data
while True:
data = client_socket.recv(1024)
if not data:
break
received_file.write(data)
received_file.close()
client_socket.close()
print(f"File received and saved as received_file.txt")

Output:
Server is ready to receive files...
File received and saved as received_file.txt

10.Write a Python program to receive UDP packets and


display their content. Simulate
sending UDP packets from a separate client program.

Ans:
import socket

# Server's host and port


server_host = 'localhost' # Change this to the server's
hostname or IP address
server_port = 54321 # Port number

# Create a UDP socket


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

# Bind the socket to the server address


server_socket.bind((server_host, server_port))

print(f"Server is ready to receive UDP packets...")

while True:
data, client_address = server_socket.recvfrom(1024)
received_data = data.decode()
print(f"Received packet from {client_address}:
{received_data}")

Output:
Server is ready to receive UDP packets...
Received packet from ('127.0.0.1', 54321): UDP Packet
1
Received packet from ('127.0.0.1', 54321): UDP Packet
2
Received packet from ('127.0.0.1', 54321): UDP Packet
3

You might also like