From dd241b9e516e40fc92f1d837d04dc96bbb519078 Mon Sep 17 00:00:00 2001 From: Kshitij Date: Tue, 8 Oct 2024 23:38:38 +0530 Subject: [PATCH] Added code for B9 (sender+receiver) --- Codes/Code-B9 (Receiver).py | 28 ++++++++++++++++++++++++++++ Codes/Code-B9 (Sender).py | 30 ++++++++++++++++++++++++++++++ README.md | 2 ++ 3 files changed, 60 insertions(+) create mode 100644 Codes/Code-B9 (Receiver).py create mode 100644 Codes/Code-B9 (Sender).py diff --git a/Codes/Code-B9 (Receiver).py b/Codes/Code-B9 (Receiver).py new file mode 100644 index 0000000..9b339d4 --- /dev/null +++ b/Codes/Code-B9 (Receiver).py @@ -0,0 +1,28 @@ +import socket + +def receive_file(port): + # Create a UDP socket + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + sock.bind(('', port)) + print(f"Listening for incoming files on port {port}...") + + # Receive file information + data, addr = sock.recvfrom(1024) + filename, filesize = data.decode().split(":") + filesize = int(filesize) + + with open(filename, "wb") as f: + print(f"Receiving {filename}...") + bytes_received = 0 + + while bytes_received < filesize: + data, addr = sock.recvfrom(1024) # Receive in chunks + f.write(data) + bytes_received += len(data) + + print(f"File {filename} received successfully.") + sock.close() + +if __name__ == "__main__": + port = int(input("Enter the port to listen on: ")) + receive_file(port) diff --git a/Codes/Code-B9 (Sender).py b/Codes/Code-B9 (Sender).py new file mode 100644 index 0000000..1355bbd --- /dev/null +++ b/Codes/Code-B9 (Sender).py @@ -0,0 +1,30 @@ +import socket +import os + +def send_file(filename, host, port): + # Create a UDP socket + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + + # Get file size + filesize = os.path.getsize(filename) + sock.sendto(f"{filename}:{filesize}".encode(), (host, port)) + + with open(filename, "rb") as f: + print(f"Sending {filename}...") + bytes_sent = 0 + + while bytes_sent < filesize: + data = f.read(1024) # Read in chunks + sock.sendto(data, (host, port)) + bytes_sent += len(data) + + print(f"File {filename} sent successfully.") + sock.close() + +if __name__ == "__main__": + target_host = input("Enter the receiver's IP address: ") + target_port = int(input("Enter the receiver's port: ")) + + # Choose a file to send + filename = input("Enter the file path to send (Text, Audio, Video, or Script): ") + send_file(filename, target_host, target_port) diff --git a/README.md b/README.md index 7dc8635..2b84551 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,8 @@ This Git repository is a comprehensive resource for the Computer Networks and Se - [Output-B8](https://git.kska.io/sppu-te-comp-content/ComputerNetworksAndSecurity/src/branch/main/Printable%20outputs/Output-B8.pdf) ##### B9 - UDP Protocol +- [Code-B9 (Sender)](https://git.kska.io/sppu-te-comp-content/ComputerNetworksAndSecurity/src/branch/main/Codes/Code-B9%20%28Sender%29.py) +- [Code-B9 (Receiver)](https://git.kska.io/sppu-te-comp-content/ComputerNetworksAndSecurity/src/branch/main/Codes/Code-B8%20%28Receiver%29.py) - [Handout-B9](https://git.kska.io/sppu-te-comp-content/ComputerNetworksAndSecurity/src/branch/main/Handouts/Handout-B9.pdf) - [Write-up - B9](https://git.kska.io/sppu-te-comp-content/ComputerNetworksAndSecurity/src/branch/main/Write-ups/Write-up%20-%20B9.pdf) - [Output-B9](https://git.kska.io/sppu-te-comp-content/ComputerNetworksAndSecurity/src/branch/main/Printable%20outputs/Output-B9.pdf)