This is my python code. It is a pinger client.It would send a messeng to the server and expect to receive the reply which capitalize the letters in 1 seconds.
from socket import *
import threading
import time
def ping_thread(num, clientSocket):
try:
clientSocket.settimeout(1)
t1 = time.perf_counter()
message = "Ping " + str(num) + " " + str(time.time())
clientSocket.sendto(message.encode(), (serverName, serverPort))
modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
t2 = time.perf_counter()
lag = t2 - t1
print(f"{modifiedMessage.decode()}: {lag:.8f}s")
except TimeoutError:
print(f"ping {num} time out!")
if __name__ == "__main__":
threads = []
clientSocket = socket(AF_INET, SOCK_DGRAM)
serverName = "localhost"
serverPort = 12000
for i in range(1, 11):
t = threading.Thread(target=ping_thread, args=(i, clientSocket))
threads.append(t)
t.start()
for t in threads:
t.join()
clientSocket.close()
And this is the result of running the code.
PING 2 1710845145.1677558: 0.00191080s
PING 5 1710845145.169752: 0.00126800sPING 6 1710845145.169752: 0.00098610s
PING 1 1710845145.1658478: 0.00588400s
PING 3 1710845145.1677558: 0.00067630s
PING 8 1710845145.1719148: 0.00477900s
PING 10 1710845145.172912: 0.00083350s
ping 10 time out!
ping 4 time out!
ping 2 time out!
I wonder why some threading (like Ping 2 above) receive the reply but still throw the timeout exception. Thanks a lot!
240320 This is the server code. It is from the lab2 in the Computer Networking: A Top-Down Approach
# We will need the following module to generate randomized lost packets
import random
from socket import *
# 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(('', 12000))
while True:
# Generate random number 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)
# Capitalize the message from the client
message = message.upper()
# If rand is less than 4, we consider the packet lost and do not respond
if rand < 4:
continue
# Otherwise, the server responds
serverSocket.sendto(message, address)
Your server code shows you randomly ignore about 40% of the messages. Because the same socket is used for all 10 messages, the ~6 messages that are returned could be processed by any of the 10 threads.
If you change the server code to add a print statement:
if rand < 4:
print('ignored', message)
continue
then you will find that ~4 messages are ignored, but ~6 threads will get the responses in mixed order. Then ~4 threads will timeout from not receiving messages.
To make sure the correct threads get the correct responses, use a separate socket for each thread:
def ping_thread(num): # don't pass common socket
try:
clientSocket = socket(AF_INET, SOCK_DGRAM) # unique socket for thread
clientSocket.settimeout(1)
t1 = time.perf_counter()
message = "Ping " + str(num) + " " + str(time.time())
clientSocket.sendto(message.encode(), (serverName, serverPort))
modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
t2 = time.perf_counter()
lag = t2 - t1
print(f"{modifiedMessage.decode()}: {lag:.8f}s")
except TimeoutError:
print(f"ping {num} time out!")
with socket(AF_INET, SOCK_DGRAM) as clientSocket:
serverName = "localhost"
serverPort = 12000
# Create threads passing only the thread number.
threads = [threading.Thread(target=ping_thread, args=(i,)) for i in range(10)]
for t in threads:
t.start()
for t in threads:
t.join()
Output. In this case 3 responses were ignored (2,3,8) and those are the ones that time out.
ignored b'PING 3 1710957710.7031364'
PING 0 1710957710.7031364: 0.00558820s
PING 6 1710957710.7031364: 0.00411860s
PING 4 1710957710.7031364: 0.00456480s
PING 1 1710957710.7031364: 0.00579830s
PING 5 1710957710.7031364: 0.00471390s
ignored b'PING 8 1710957710.7031364'
PING 7 1710957710.7031364: 0.00405850s
ignored b'PING 2 1710957710.7031364'
PING 9 1710957710.7031364: 0.00469280s
ping 3 time out!
ping 8 time out!
ping 2 time out!