请参阅以下Python代码:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #udp Socket
sock.bind(('', 5505)) # bind port 5505
time.sleep(60) # waiting 60 seconds
countMessages = 0 # set counter to 0
while True:
try:
resp = sock.recv(2048).decode('utf-8') # read data from socket
countMessages += 1 # and count messages
except socket.timeout:
return countMessages
当应用程序处于睡眠状态时,我正在向绑定端口N udp
发送(5505)
消息,然后我会读取和计算消息。
如何在从套接字读取消息之前知道消息数量?