-
Notifications
You must be signed in to change notification settings - Fork 0
/
9_stopwait_server.py
44 lines (35 loc) · 1.05 KB
/
9_stopwait_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
# Server side stop and wait protocol
#Take two test cases - ack sent and ack not sent
#In the first case, the ack is sent and the server waits for the next packet
#In the second case, the ack is not sent and the server waits for the same packet again
import socket
import time
# Create a TCP/IP socket
server = socket.socket()
server.bind(('localhost', 12345))
server.listen(5)
# Accept the connection
conn, addr = server.accept()
tt = 0
# Receive the data, send the ack
while True:
# Test case 1: ack sent
data = conn.recv(1024).decode()
if data == 'exit':
break
print('Received: ', data)
# Send the ack
# Test case 2: ack not sent
if data == 'test' and tt==0:
print('Not sending the ack')
# Wait for the same packet again
data = conn.recv(1024).decode()
print('Received: ', data)
# Send the ack
conn.send('test'.encode())
print('Sent: ack')
tt = 1
else:
conn.send(str(data).encode())
print('Sent Acknowledgement: ', data)
conn.close()