-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathudp_server2.py
More file actions
192 lines (172 loc) · 6.01 KB
/
udp_server2.py
File metadata and controls
192 lines (172 loc) · 6.01 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
__author__ = 'alex.stuart,Lauren.mills,Caleb Stevenson'
import socket # Import socket module
import thread
import struct
def checkchecksum(checksum, 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
print "sum %03d"%sum
return "%03d"%sum
# c is , addr is going to be the address of client
def clientconnection(c):
while True:
#receive 1024 bytes for the filename
#l = c.recv(1024)
print "Trying to connect"
fileAckMatch = False
while not fileAckMatch:
l, addr = c.recvfrom(1024)
print l
print l[-3:]
print l[:-3]
if not checkchecksum(l[-3:], l[:-3]):
print "checksum does not match"
continue
else:
print "checksum does match"
print "Sending ack for filename"
fileAck = "888"
fileAck += calcchecksum(fileAck)
print "set fileAckMatch to true"
fileAckMatch = True
c.sendto(fileAck, addr)
print 'Connected to', addr # Confirm correct client
if not l:
break
#decode the raw byte using UTF8
filename = l[:-3].decode("utf-8")
print "file:" + filename
awktidbits = [0] * 256
filemorsels = [0] * 256
c.settimeout(1.0)
#open the file request
try:
file = open(filename,'r')
#read the file from disk in 1024 byte chunks
l = file.read(1018)
ll = "000"
#print "awk num:" + ll
ll += l
#print "whole packet:" + ll
filemorsels.append(l)
except Exception as e:
print e
#something went wrong with the file need to tell the client
c.sendto("*****", addr)
l = False
file = False
#while bytes to send
i = 1
lastpacket = False
startwindow = 0
endwindow = 5
no_timeout = True
reset = False
# l is the data from the file
# ll is the ack number + the file data
# lll is the acknumber + filedata + checksum
while(ll):
no_timeout = True
if reset:
ll = False
break
checksum = calcchecksum(ll)
lll = ll + str(checksum)
while(i < endwindow):
c.sendto(lll, addr)
print "sending"
timeoutcount = 0
if(filemorsels[i]):
l = filemorsels[i]
else:
#read next 1024
l = file.read(1018)
if not l:
print "nothing left to read"
endwindow = i
lastpacket = i
#print type(l)"%02d"%a
ll = "%03d"%i
print "i is ", ll
#print "chr of i " + ll
ll += l
filemorsels[i] = l
print "i", i
i+=1
if(i == 256):
i = 0
checksum = calcchecksum(ll)
lll = ll + checksum
while(no_timeout):
print "Listening for Acks"
try:
data, addr = c.recvfrom(1024)
except Exception:
print "timeout!"
timeoutcount += 1
if timeoutcount == 5:
reset = True
no_timeout = False
print data
awkchecksum = checkchecksum(data[-3:], data[:-3])
print "awkchecksum = ", awkchecksum
if awkchecksum:
#print "if awkchecksum"
resp_num = int(data[:-3])
awktidbits[resp_num] = True
#print "after if awkchecksum"
newstartwindow = startwindow
for g in range(5):
#print "g is ", g
#print "startwindow is ", startwindow
#print "awktidbits[startwindow + g]"
#print "\t", awktidbits[startwindow + g]
if awktidbits[startwindow + g]:
newstartwindow = startwindow+g
else:
break
startwindow = newstartwindow
i = startwindow
if not lastpacket:
endwindow = startwindow + 5
else:
endwindow = lastpacket
print startwindow
print endwindow
print "i - 2 is ", i -2
# file sending should be complete, stop sending
# and reset
# do we have all of our ack's back
#if i == startwindow:
#if i - 2 == startwindow:
# print "i is ", i
# reset = True
# break
if file:
file.close()
print "finished sending file"
break
print 'Client ' + str(addr) + ' is no longer connected.'
serv_sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)# Create a socket object
this_machine_name = socket.gethostname() # Get local machine ip
#print this_machine_name
port = raw_input("Enter port number: ")
try:
port = int(port)
if(port < 0 or port > 65535):
raise Exception
except Exception:
print "not a valid port number"
quit() # Reserve the passed port for your service.
serv_sock.bind(("", port)) # Bind to the port
# Open a temp file to store the data
#serv_sock.listen(1) # Now wait for client connection.
clientconnection(serv_sock)
#c.close()