-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.py
More file actions
28 lines (26 loc) · 1012 Bytes
/
Copy pathServer.py
File metadata and controls
28 lines (26 loc) · 1012 Bytes
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
# Server.py
# We will need the following module to generate randomized lost packets
import random
from socket import socket, AF_INET, SOCK_DGRAM
# Create a UDP socket
# Notice the use of SOCK_DGRAM for UDP packets
serverSocket = socket(AF_INET, SOCK_DGRAM)
# Assign IP address and port number to socket
serverSocket.bind(('10.0.0.1', 12000)) # 10.0.0.1
print("Waiting for Client....")
i = 1
while True:
# Generate random number5 in the range of 0 to 10
rand = random.randint(0, 10)
# Receive the client packet along with the address it is coming from
message, address = serverSocket.recvfrom(1024)
# If rand is less is than 4, we consider the packet lost and do not respond
modifiedMessage = message.decode().upper()
if rand < 4:
continue
print('\nPING {} Received'.format(i))
print('Mesg rcvd:' + message.decode())
print('Mesg sent: ' + modifiedMessage)
# Otherwise, the server responds
serverSocket.sendto(modifiedMessage.encode(), address)
i += 1