0% found this document useful (0 votes)
27 views12 pages

APP Assignment

1. The document describes Python programs implementing TCP and UDP clients and servers using sockets. It includes programs for a TCP client to connect to a server and send a message, a TCP server to receive a list of numbers from a client and return the sum, UDP clients and servers to send and receive packets, and programs to measure connection times. 2. The programs cover basic TCP client-server interactions, sending and receiving lists and calculating sums, UDP packet sending and receiving, responding to messages with acknowledgments, and measuring connection performance. 3. The document provides code snippets for 14 Python programs implementing various TCP and UDP client-server examples using sockets.
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)
27 views12 pages

APP Assignment

1. The document describes Python programs implementing TCP and UDP clients and servers using sockets. It includes programs for a TCP client to connect to a server and send a message, a TCP server to receive a list of numbers from a client and return the sum, UDP clients and servers to send and receive packets, and programs to measure connection times. 2. The programs cover basic TCP client-server interactions, sending and receiving lists and calculating sums, UDP packet sending and receiving, responding to messages with acknowledgments, and measuring connection performance. 3. The document provides code snippets for 14 Python programs implementing various TCP and UDP client-server examples using sockets.
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/ 12

NAME : S.

ABHISESHA
KUMAARA
REG NO :
RA2211026010487
SECTION: Z2
Week 12- Tutorial Assignment
Implement Python program - TCP/UDP program using Sockets
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.
#PROGRAM
import
socket

# Server address and port


server_address = ('localhost', 12345)

# Create a socket object


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

# Connect to the server


client_socket.connect(server_address)

• Message to send to the


server message = "Hello,
Server!"

• Send the message to the server


client_socket.send(message.encode('utf-8'))

• Receive data from the server


response =
client_socket.recv(1024)

• Print the response from the server


print("Server response:", response.decode('utf-8'))
• Close the socket
client_socket.close
()

1. 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.
#PROGRAM
import socket

# Server address and port


server_address = ('localhost', 12345)

# 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_address)

• Listen for incoming connections


server_socket.listen(1)
print("Waiting for a connection...")

while True:
# Accept a client connection
client_socket, client_address = server_socket.accept()
print("Connected to:", client_address)

• Receive the list of numbers from the


client data = client_socket.recv(1024)
numbers = data.decode('utf-8').split(',')

• Calculate the sum of the numbers


total_sum = sum(map(int, numbers))

• Send the sum back to the client response


= str(total_sum)
client_socket.send(response.encode('utf-8'))

• Close the client socket


client_socket.close()

1. Create a Python UDP client that sends a "UDP Message" packet to a


UDP server. Demonstrate the sending and receiving of the packet.
//PROGRAM

#UDP Server Code:


import socket

# Server address and port

server_address = ('localhost', 12345)

# Create a socket object


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

• Bind the socket to the server address


server_socket.bind(server_address)

print("UDP server is listening on", server_address)

while True:
# Receive data from the client
data, client_address = server_socket.recvfrom(1024)

# Print the received message and client address


print(f"Received message '{data.decode('utf-8')}' from {client_address}")

#UDP Client Code:

import socket

# Server address and port


server_address = ('localhost', 12345)

# Create a socket object


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

• Message to send to the server


message = "UDP Message"

• Send the message to the server


client_socket.sendto(message.encode('utf-8'), server_address)

• Close the socket


client_socket.close()

1. 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.
#UDP Server Code :
import socket import
random

# Server address and port


server_address = ('localhost', 12345)

# Create a socket object


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

• Bind the socket to the server address


server_socket.bind(server_address)

print("UDP server is listening on", server_address)

while True:

# Receive the random number from the client


data, client_address = server_socket.recvfrom(1024)
random_number = int(data.decode('utf-8'))

# Check if the number is even or odd


result = "Even" if random_number % 2 == 0 else "Odd"

# Send the result back to the client


server_socket.sendto(result.encode('utf-8'), client_address)

#UDP Client Code:

import socket
import random

# Server address and port


server_address = ('localhost', 12345)

# Create a socket object


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

# Generate a random number


random_number = random.randint(1, 100)

# Send the random number to the server


client_socket.sendto(str(random_number).encode('utf-8'), server_address)

# Receive the result from the server


data, server_address = client_socket.recvfrom(1024)

# Print the result received from the server


print(f"The number {random_number} is {data.decode('utf-8')}")

• Close the socket


client_socket.close()

1. Write a Python program to create a UDP server that listens on port


54321. Ensure the server can receive UDP packets from clients.

#UDP Server Code


import socket

# Server address and port


server_address = ('localhost', 54321)

# Create a socket object


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

• Bind the socket to the server address


server_socket.bind(server_address)

print("UDP server is listening on", server_address)


while True:
# Receive data from the client
data, client_address = server_socket.recvfrom(1024)

# Print the received message and client address


print(f"Received message '{data.decode('utf-8')}' from {client_address}")

1. 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.

#UDP Server Code


import socket

# Server address and port


server_address = ('localhost', 54321)

# Create a socket object


server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
• Bind the socket to the server address
server_socket.bind(server_address)

print("UDP server is listening on", server_address)

while True:
# Receive data from the client
data, client_address = server_socket.recvfrom(1024)

received_message = data.decode('utf-8')

• Check if the received message is "UDP Message" if


received_message == "UDP Message":

• Send an acknowledgment message to the client ack_message =


"Acknowledgment" server_socket.sendto(ack_message.encode('utf-
8'), client_address)

print(f"Received 'UDP Message' from {client_address}. Acknowledgment sent.")

else:
print(f"Received an unexpected message:
'{received_message}' from {client_address}")

#UDP Client Code:


import socket

# Server address and port


server_address = ('localhost', 54321)

# Create a socket object


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

• Message to send to the server


message = "UDP Message"

• Send the message to the server


client_socket.sendto(message.encode('utf-8'), server_address)

# Receive the acknowledgment from the server


ack_data, server_address = client_socket.recvfrom(1024)
• Print the acknowledgment received from the server
print("Acknowledgment received:", ack_data.decode('utf-8'))

• Close the socket


client_socket.close()

1. 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.

#Server Code
import socket

# Server address and port


server_address = ('localhost', 12345)

# 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_address)

• Listen for incoming connections


server_socket.listen(1)

print("TCP server is listening on", server_address)

while True:
# Accept a client connection
client_socket, client_address = server_socket.accept()
print("Connected to:", client_address)

• Send a response to the client response =


"Hello, Client!"
client_socket.send(response.encode('utf-8'))

• Close the client socket


client_socket.close()
Client server
import socket
import time
# Server address and port
server_address = ('localhost', 12345)

# Create a socket object


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

• Record the start time


start_time = time.time()

• Connect to the server


client_socket.connect(server_address)

• Record the end time after


connecting end_time = time.time()

• Calculate the time taken to connect in seconds


connection_time = end_time - start_time
print(f"Time taken to connect to the server: {connection_time:.6f} seconds")

• Receive data from the server


response = client_socket.recv(1024)

• Record the end time after receiving the


response end_time = time.time()

• Calculate the time taken to receive the response


in seconds response_time = end_time - start_time
print(f"Time taken to receive the response: {response_time:.6f} seconds")

• Print the response from the server


print("Server response:", response.decode('utf-8'))

• Close the socket


client_socket.close()

1. 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.

#TCP SERVER CODE


import socket

# Server address and port


server_address = ('localhost', 12345)

# 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_address)

• Listen for incoming connections


server_socket.listen(1)
print("TCP server is listening on", server_address)

while True:
# Accept a client connection
client_socket, client_address = server_socket.accept()
print("Connected to:", client_address)

• Receive data from the client


data = client_socket.recv(1024)

if not data:
break # Exit the loop if no data received

• Send the received data back to the


client (echo) client_socket.send(data)

• Close the client socket


client_socket.close()

1. 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.

#SERVER
import socket

# Server address and port


server_address = ('localhost', 12345)

# 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_address)

• Listen for incoming connections


server_socket.listen(1)
print("TCP server is listening on", server_address)

while True:
# Accept a client connection
client_socket, client_address = server_socket.accept()
print("Connected to:", client_address)

• Receive data from the client


data = client_socket.recv(1024)

if not data:
break # Exit the loop if no data received

• Send the received data back to the


client (echo) client_socket.send(data)

• Close the client socket


client_socket.close()
#CLIENT SERVER
import socket

# Server address and port


server_address = ('localhost', 12345)

# Create a socket object


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

# Connect to the server


client_socket.connect(server_address)

while True:

• Get a message from the user to send to the server message


= input("Enter a message (or type 'exit' to quit): ")

if message == 'exit':
break

# Send the message to the server


client_socket.send(message.encode('utf-8'))
• Receive and print the echoed response from
the server response = client_socket.recv(1024)
print("Server response:", response.decode('utf-8'))

• Close the socket


client_socket.close()

1. Write a Python program to receive UDP packets and display their


content. Simulate sending UDP packets from a separate client program.
#TCP SERVER CODE
import socket

# Server address and port


server_address = ('localhost', 12345)
# Create a socket object
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect to the server


client_socket.connect(server_address)

while True:

• Get a message from the user to send to the server message


= input("Enter a message (or type 'exit' to quit): ")

if message == 'exit':
break

# Send the message to the server


client_socket.send(message.encode('utf-8'))

• Receive and print the echoed response from


the server response = client_socket.recv(1024)
print("Server response:", response.decode('utf-8'))

• Close the socket


client_socket.close()
#TCP CLIENT CODE
import socket

# Server address and port


server_address = ('localhost', 12345)

# Create a socket object


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

# Connect to the server


client_socket.connect(server_address)

while True:

• Get a message from the user to send to the server message


= input("Enter a message (or type 'exit' to quit): ")

• Send the message to the server


client_socket.send(message.encode('utf-8'))

• Receive and print the echoed response from


the server response = client_socket.recv(1024)
print("Server response:", response.decode('utf-8'))

• Close the socket


client_socket.close()

You might also like