-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathudp_client2.py
More file actions
143 lines (133 loc) · 4.06 KB
/
udp_client2.py
File metadata and controls
143 lines (133 loc) · 4.06 KB
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
__author__ = 'alex.stuart,lauren.mills,Caleb.Stevenson'
import socket # Import socket module
import select
import struct
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
def checkchecksum(checksum, filedata):
print "checksum = ", checksum
print "calc sum = ", calcchecksum(filedata)
if checksum != str(calcchecksum(filedata)):
return False
else:
return True
def calcchecksum(filedata):
sum = 0
for i in filedata:
sum += ord(i)
sum = sum % 1000
return "%03d"%sum
try:
host = raw_input("Enter IP address: ")
#host = socket.gethostname()
#print host
except Exception:
print "not an IP address"
quit() # Reserve a port for your service.
#host = socket.gethostname() # Get local machine name
port = raw_input("Enter port number: ")
try:
port = int(port)
# throw Exception if port < 0 or > 352983752 whatever
if(port < 0 or port > 65535):
raise Exception;
except Exception:
print "Not a port number"
quit() # Reserve a port for your service.
try:
s.connect((host, port))
except Exception:
print "problem connecting to that IP address"
quit()
while True:
filename = raw_input("Enter the file's name or press ENTER to exit: ")
b=bytes(filename)
if not b:
print "Exiting"
quit()
print 'Sending...'
fileAckMatch = False
b += calcchecksum(b)
s.settimeout(1.0)
while not fileAckMatch:
s.send(b)
try:
l,a = s.recvfrom(1024)
except Exception as e:
continue
fileAck = l[0:3]
if not checkchecksum(l[-3:], l[:-3]):
#do stuff
print "Checksum's don't match!"
else:
print "checksum's match"
#print l
print fileAck
if fileAck == "888":
print "file ack match"
fileAckMatch = True
else:
print "file ack does not match"
l,a = s.recvfrom(1024)
#print l
if l=="*****":
print("file not available")
quit()
s.settimeout(1.0)
#print "rec 1st"
newfile = open(filename, 'ab')
skip_write = False
done = False
skip_count = 0
filemorsels = [0] * 256
lastwrite_awk = 0
while(not done):
print skip_write
if not skip_write:
if not checkchecksum(l[-3:], l[:-3]):
#do stuff
print "Checksum's don't match!"
else:
print "checksum's match"
print len(l[3:])
awk_num = l[0:3]
print " awk:" + awk_num
filemorsels[int(awk_num)] = l[3:-3]
print "lastwrite_awk = ", lastwrite_awk
if filemorsels[lastwrite_awk]:
i = lastwrite_awk
while filemorsels[i]:
print "writing all of the file morsels"
#print " ", (if filemorsels[i])
newfile.write(filemorsels[i])
i+= 1
lastwrite_awk = i
#we need to wait to write all the data
#newfile.write(l[3:-3])
# need to include a checksum with the ack packet
awk_num += calcchecksum(awk_num)
print "sending awk_num = ", awk_num
s.send(awk_num)
print "sent ack "
#print "wrote a chunk"
#print l
try:
if skip_write:
skip_count += 1
skip_write = False
l = False
l,a = s.recvfrom(1024)
if l:
skip_count = 0
except Exception as e:
skip_write = True
print "skip_count is ", skip_count
if skip_count > 5:
l = False
done = True
# newfile.write(l)
# print "inexpection"
# print l
#print len(newfile)
#f.close()
print "Done Recieving"
#print s.recv(1024)