Files
CN-Educoder/UDP Ping程序实现/lab5.py
Yunjay Liu 809b1b1cd3 finish
2024-10-25 22:03:45 +08:00

27 lines
1.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from socket import *
import time
serverName = '127.0.0.1' # 服务器地址,本例中使用本机地址
serverPort = 12000 # 服务器指定的端口
clientSocket = socket(AF_INET, SOCK_DGRAM) # 创建UDP套接字使用IPv4协议
clientSocket.settimeout(1) # 设置套接字超时值1秒
for i in range(0, 9):
sendTime = time.time()
message = ('Ping %d %s' % (i+1, sendTime)).encode() # 生成数据报编码为bytes以便发送
try:
########## Begin ##########
# 将信息发送到服务器
clientSocket.sendto(message,(serverName,serverPort))
# 从服务器接收信息,同时也能得到服务器地址
modifiedMessage, serverAddress = clientSocket.recvfrom(1024)
########## End ##########
rtt = time.time() - sendTime # 计算往返时间
print('Sequence %d: Reply from %s RTT = %.3fs' % (i+1, serverName, rtt)) # 显示信息
except Exception as e:
print('Sequence %d: Request timed out.' % (i+1))
clientSocket.close() # 关闭套接字