This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUDP_Server.py
160 lines (132 loc) · 4.03 KB
/
UDP_Server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import binascii
import socket
import struct
import sys
import hashlib
import select
import codecs
import threading
import time
from hurry.filesize import size
fileUpdate = threading.Event()
endLoop = threading.Event()
repeatRecv = threading.Event()
UDP_IP = "0.0.0.0"
UDP_PORT = 5005
UDP_PORT2 = 5002
Client_IP = "127.0.0.1"
bytesCount = 1024 * 63
maxThreads = 1
fileName = "out_CS3543_100MB"
if len(sys.argv) == 2:
Client_IP = sys.argv[1]
if len(sys.argv) >= 3:
fileName = sys.argv[2]
endMessage = b"complete"
out_file = open(fileName, "wb")
unpacker = struct.Struct("I I " + str(bytesCount) + "s 32s")
packer = struct.Struct("I I " + str(bytesCount) + "s")
# Create the socket and listen
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # ACK
sock2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Data Packets
sock.bind((UDP_IP, UDP_PORT))
end = 0
x = 0
writeCount = 0
Timeout = 0.03
# Queue to store pending packets
class Queue:
def __init__(self):
self.queue = list()
def addtoq(self, dataval):
self.queue.insert(0, dataval)
repeatRecv.set()
return True
def size(self):
return len(self.queue)
def removefromq(self):
if len(self.queue) == 0:
return (0, 0)
return self.queue.pop()
dataQueue = Queue()
def writeData(count, data):
global writeCount
if writeCount > count:
return
while count != writeCount:
fileUpdate.wait()
fileUpdate.clear()
out_file.write(data)
writeCount = writeCount + 1
# print("Wrote: ",size(count*bytesCount))
fileUpdate.set()
return
def closeFile(count):
if writeCount > count:
return
while count != writeCount:
fileUpdate.wait()
endLoop.set()
repeatRecv.set()
out_file.close()
print("File closed")
def receive_data():
while True:
newData = dataQueue.removefromq()
if newData[0] != 0:
UDP_Packet = unpacker.unpack(newData[0])
# print("received from:", addr)
# print("received message:", UDP_Packet)
# Create the Checksum for comparison
values = (UDP_Packet[0], UDP_Packet[1], UDP_Packet[2])
packed_data = packer.pack(*values)
chksum = bytes(hashlib.md5(packed_data).hexdigest(), encoding="UTF-8")
# Compare Checksums to test for corrupt data
if UDP_Packet[3] == chksum and UDP_Packet[1] == UDP_Packet[0] % 2:
# Send Packet through
sock2.sendto(newData[0], (Client_IP, UDP_PORT2))
offset = removeNullBytes(UDP_Packet[2])
if UDP_Packet[2][:offset] == endMessage:
closeFile(UDP_Packet[0])
else:
writeData(UDP_Packet[0], UDP_Packet[2][:offset])
else:
print(
"CheckSums do not Match or the Sequence Number is incorrect, Packet is not ok"
)
sock2.sendto(newData[0], (Client_IP, UDP_PORT2))
else:
if endLoop.is_set() == True:
return
else:
repeatRecv.wait()
def removeNullBytes(input):
offset = bytesCount
i = offset - 1
while i > -1:
if input[i] == 0:
offset = offset - 1
i = i - 1
else:
break
return offset
repeatRecv.clear()
while maxThreads > 0 and endLoop.is_set() == False:
print("Packet Number: " + str(x+1))
timer = select.select([sock], [], [], Timeout)
if timer[0]:
maxThreads = maxThreads - 1
data, addr = sock.recvfrom(bytesCount + 1024)
dataQueue.addtoq((data, addr))
repeatRecv.clear()
thread = threading.Thread(target=receive_data)
thread.start()
x = x + 1
while endLoop.is_set() == False:
print("Packet Number: " + str(x + 1))
timer = select.select([sock], [], [], Timeout)
if timer[0]:
data, addr = sock.recvfrom(bytesCount + 1024)
dataQueue.addtoq((data, addr))
repeatRecv.clear()
x = x + 1