-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
88 lines (72 loc) · 2.23 KB
/
client.py
File metadata and controls
88 lines (72 loc) · 2.23 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
from socket import *
from frame import *
import time
import pickle
import random
HOST = gethostbyname(gethostname()) # The server's hostname or IP address
PORT = 9999 # The port used by the server
# HOST = gethostbyname("www.google.com") # The server's hostname or IP address
# PORT = 80
#create client socket
clientSocket=socket(AF_INET, SOCK_DGRAM)
ADDRESS=(HOST,PORT)
# clientSocket.connect(ADDRESS)
# clientSocket=socket(AF_INET, SOCK_STREAM)
# clientSocket.connect((HOST,PORT))
# clientSocket=create_connection((HOST, PORT))
clientSocket.settimeout(0.01)
base=0
window=7
sendNext=0
timeout=1
lastackreceived= time.time()
packets=[] #window packets generated stored stored in this
last_ack=-1
def generatePacket(index, size=0):
data="Hello"
d=dataframe(size,index,data)
return d
def windowPacket(ind):
if ind<len(packets):
return packets[ind]
else:
return None
while True:
if(sendNext<base+window):
size=int(random.uniform(512,2048))
pkt=generatePacket(sendNext,size)
packets.append(pkt)
sendNext+=1
pickledpkt=pickle.dumps(pkt)
print "sending packet"
# clientSocket.send("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n")
clientSocket.sendto(pickledpkt,ADDRESS)
# print clientSocket.recv(1024)
print "yes"
#RECEIPT OF AN ACK
try:
pickledack,sss = clientSocket.recvfrom(1024)
# ack = []
ack = pickle.loads(pickledack)
print "Received ack for", ack.index
# slide window and reset timer
last_ack=ack.index
while ack.index>=base and packets:
lastackreceived = time.time()
for i in range(ack.index-base+1):
del packets[0]
base = ack.index + 1
#TIMEOUT
except:
print last_ack," in except"
if(time.time()-lastackreceived>timeout):
for i in packets:
print "resend"
clientSocket.sendto(pickle.dumps(i),ADDRESS)
# d1=dataframe(100,0,"Hello")
# print(d1.data)
# with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
# s.connect((HOST, PORT))
# s.sendall(b'Hello, world')
# data = s.recv(1024)
# print('Received', repr(data))