finish
This commit is contained in:
10
UDP Ping程序实现/lab1.py
Normal file
10
UDP Ping程序实现/lab1.py
Normal file
@@ -0,0 +1,10 @@
|
||||
# UDPPingerServer.py
|
||||
from socket import *
|
||||
|
||||
########## Begin ##########
|
||||
|
||||
serverSocket = socket(AF_INET, SOCK_DGRAM)
|
||||
serverSocket.bind(("0.0.0.0",12000))
|
||||
########## End ##########
|
||||
|
||||
print( serverSocket)
|
||||
16
UDP Ping程序实现/lab2.py
Normal file
16
UDP Ping程序实现/lab2.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from socket import *
|
||||
|
||||
# 创建UDP套接字
|
||||
serverSocket = socket(AF_INET, SOCK_DGRAM)
|
||||
# 绑定本机IP地址和端口号
|
||||
serverSocket.bind(('', 12000))
|
||||
|
||||
########## Begin ##########
|
||||
# 接收客户端消息
|
||||
message, address = serverSocket.recvfrom(1024)
|
||||
# 将数据包消息转换为大写
|
||||
message = message.upper()
|
||||
#将消息传回给客户端
|
||||
serverSocket.sendto(message,address)
|
||||
########## End ##########
|
||||
|
||||
28
UDP Ping程序实现/lab3.py
Normal file
28
UDP Ping程序实现/lab3.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from socket import *
|
||||
import random
|
||||
|
||||
# 创建UDP套接字
|
||||
serverSocket = socket(AF_INET, SOCK_DGRAM)
|
||||
# 绑定本机IP地址和端口号
|
||||
serverSocket.bind(('', 12000))
|
||||
|
||||
num=0
|
||||
while True:
|
||||
|
||||
|
||||
# 接收客户端消息
|
||||
message, address = serverSocket.recvfrom(1024)
|
||||
# 将数据包消息转换为大写
|
||||
message = message.upper()
|
||||
|
||||
num=num+1
|
||||
if num>=8:
|
||||
break
|
||||
|
||||
########## Begin ##########
|
||||
if num % 3 == 1:
|
||||
continue
|
||||
########## End ##########
|
||||
|
||||
#将消息传回给客户端
|
||||
serverSocket.sendto(message, address)
|
||||
11
UDP Ping程序实现/lab4.py
Normal file
11
UDP Ping程序实现/lab4.py
Normal file
@@ -0,0 +1,11 @@
|
||||
|
||||
from socket import *
|
||||
|
||||
########## Begin ##########
|
||||
clientSocket = socket(AF_INET, SOCK_DGRAM) # 创建UDP套接字,使用IPv4协议
|
||||
# 设置套接字超时值1秒
|
||||
clientSocket.settimeout(1)
|
||||
########## End ##########
|
||||
|
||||
print(clientSocket)
|
||||
print(clientSocket.gettimeout())
|
||||
27
UDP Ping程序实现/lab5.py
Normal file
27
UDP Ping程序实现/lab5.py
Normal file
@@ -0,0 +1,27 @@
|
||||
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() # 关闭套接字
|
||||
Reference in New Issue
Block a user