Compare commits
2 Commits
0ffed44958
...
dd241b9e51
Author | SHA1 | Date | |
---|---|---|---|
dd241b9e51 | |||
f984a16c01 |
28
Codes/Code-B9 (Receiver).py
Normal file
28
Codes/Code-B9 (Receiver).py
Normal 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
30
Codes/Code-B9 (Sender).py
Normal 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)
|
BIN
Printable outputs/Output-B9.pdf
Normal file
BIN
Printable outputs/Output-B9.pdf
Normal file
Binary file not shown.
@ -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)
|
||||
|
||||
##### 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)
|
||||
|
||||
##### C10 - DNS
|
||||
- [Handout-C10](https://git.kska.io/sppu-te-comp-content/ComputerNetworksAndSecurity/src/branch/main/Handouts/Handout-C10.pdf)
|
||||
|
Loading…
Reference in New Issue
Block a user