Sliding Window Protocol (Go-back-N algorithm)
unknown
python
3 years ago
4.4 kB
19
Indexable
#Client.py
import socket
import select
def client_program():
n = 4
win_start = 0
win_end = win_start + n - 1
host = socket.gethostname()
port = 12366
sender = []
flag = 0
client_socket = socket.socket()
client_socket.connect((host, port))
print('Window Size is ', n)
print('******** Enter "bye" to close connection ***************')
message = input("Hit any key to start sending frames -> ")
while message.lower().strip() != 'bye':
print("Sending frames...")
if flag == 0:
for i in range(n):
sender.append(win_start + i)
for i in sender:
print("Frame -> ", i)
else:
print("Frame -> ", win_start)
msg = str(win_start)
client_socket.send(msg.encode())
ready = select.select([client_socket], [], [], 1)
if ready[0]:
data = client_socket.recv(1024).decode()
ack = int(data)
if ack not in sender:
win_start = ack
win_end = win_start + n - 1
flag = 0
for i in range(n):
sender.pop()
else:
win_start = ack
flag = 1
else:
print("Timeout occurred, resending frames...")
continue
print("************************************")
print('Received ACK server: ' + data)
message = input("Hit any key to start sending frames -> ")
client_socket.close()
if __name__ == '__main__':
client_program()
#---------------------------------------------------------------------------#
#Server.py
import socket
import random
def server_program():
# get the hostname
host = socket.gethostname()
port = 12366 # initiate port no above 1024
exp = 0
n = 4
new = 1
win_start = 0
win_end = win_start + n - 1
receiver = []
server_socket = socket.socket() # get instance
# look closely. The bind() function takes tuple as argument
server_socket.bind((host, port)) # bind host address and port together
# configure how many client the server can listen simultaneously
server_socket.listen(2)
conn, address = server_socket.accept() # accept new connection
print("Connection from: ", str(address))
while True:
try:
# receive data stream. it won't accept data packet greater than 1024 bytes
data = conn.recv(1024).decode()
if not data:
# if data is not received break
break
except:
print("Packet Loss\n")
continue
rec = int(data)
lim = rec + n - 1
count = 0
flag = 0
ack = rec
# Simulating packet loss by randomly dropping packets
if random.random() <= 0.3:
print("Packet Loss: Frame ->", rec)
continue
randy = random.randint(1, 4)
if new == 1 : # you received a new frame of a new window
while(count != randy):
temp = random.randint(rec, lim)
if temp not in receiver:
print("Received Frame -> ", temp)
count+=1
flag = 1 # At least one new frame added in the receiver buffer
receiver.append(temp)
else :
print("Received Frame -> ", rec) # you received a new frame of an old window
receiver.append(rec)
flag = 1
if(flag == 1):
for i in range(rec,lim+1):
if i not in receiver:
ack = i
break
ack = i+1
print( "Sending ACK -> ", ack) # next expected frame
print('***************************************************')
data = str(ack)
conn.send(data.encode()) # send data to the client
if ack > win_end :
win_start = ack
win_end = win_start + n - 1
new = 1 # now receive a new frame of a new window
else :
new = 0 # now received a new frame of an old window
conn.close() # close the connection
if __name__ == '__main__':
server_program()
Editor is loading...