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

A - U23cs068 - Suyash - Yadav - Lab Assignment 4

The document outlines the design and implementation of a file transfer application using UDP sockets by a student named Suyash Yadav. It details the server-side code for receiving a file and the client-side code for sending a text file, including the use of acknowledgments and end signals. The application operates on localhost with a specified port and buffer size.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

A - U23cs068 - Suyash - Yadav - Lab Assignment 4

The document outlines the design and implementation of a file transfer application using UDP sockets by a student named Suyash Yadav. It details the server-side code for receiving a file and the client-side code for sending a text file, including the use of acknowledgments and end signals. The application operates on localhost with a specified port and buffer size.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

LAB 4 :

NAME : SUYASH YADAV


ROLL_NO : U23CS068

1. Design and implement a file transfer application using UDP sockets.


a. Your input can be a text file.

Steps i performed are ​


1) writing the server side code for receiving the file
2) writing the client side code to send the file
3) after running the server code i implemented the client code later getting the
received file msg as coded

SERVER SIDE :
import socket

HOST = '127.0.0.1' # Localhost


PORT = 3002 # Port number
BUFFER_SIZE = 1024 # Buffer size for packets

# creating UDP socket with a fixed host and port


server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.bind((HOST, PORT))
print(f"Server started at {HOST}:{PORT}, waiting for file...")

data, client_addr = server_socket.recvfrom(BUFFER_SIZE)


filename = data.decode()
print(f"Receiving file: {filename}")

# open a new file and create the file called recieved_suyash ( this shows
we received on the server )
with open(f"recieve_{filename}", "wb") as file:
while True:
data, addr = server_socket.recvfrom(BUFFER_SIZE)
if data == b"END": # End of file signal
break
file.write(data)
server_socket.sendto(b"ACK", addr) # Sending acknowledgment on
receiving the file

print("File received successfully.")


server_socket.close()

CLIENT SIDE:
import socket

HOST = '127.0.0.1' # Localhost


PORT = 3002 # Port number
BUFFER_SIZE = 1024 # Buffer size for packets

# creating UDP socket with a fixed host and port


client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = (HOST, PORT)

filename = "suyash.txt"
client_socket.sendto(filename.encode(), server_address)

# Open and send file data to the data


with open(filename, "rb") as file:
while (chunk := file.read(BUFFER_SIZE)):
client_socket.sendto(chunk, server_address)
ack, _ = client_socket.recvfrom(BUFFER_SIZE) # Waiting for
acknowledgment

# confirmation
client_socket.sendto(b"END", server_address)
print("File sent successfully.")
client_socket.close()

You might also like