Compare commits

...

2 Commits

Author SHA1 Message Date
dd241b9e51
Added code for B9 (sender+receiver) 2024-10-08 23:38:38 +05:30
f984a16c01
Added code+output for B9. 2024-10-08 23:36:57 +05:30
4 changed files with 61 additions and 0 deletions

View File

@ -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)

30
Codes/Code-B9 (Sender).py Normal file
View File

@ -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)

Binary file not shown.

View File

@ -62,8 +62,11 @@ 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) - [Output-B8](https://git.kska.io/sppu-te-comp-content/ComputerNetworksAndSecurity/src/branch/main/Printable%20outputs/Output-B8.pdf)
##### B9 - UDP Protocol ##### 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) - [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) - [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)
##### C10 - DNS ##### C10 - DNS
- [Handout-C10](https://git.kska.io/sppu-te-comp-content/ComputerNetworksAndSecurity/src/branch/main/Handouts/Handout-C10.pdf) - [Handout-C10](https://git.kska.io/sppu-te-comp-content/ComputerNetworksAndSecurity/src/branch/main/Handouts/Handout-C10.pdf)