Assignment 2: UDP Pinger[课后作业]
2024-09-07 11:20:32
Computer Networking : A Top-Down Approach 的课后作业.
要求: 基于UDP协议,实现一个Pinger工具. 服务端代码已经提供了,自己实现客户端的代码.
完整的题目链接: https://wenku.baidu.com/view/ed19e6cce2bd960591c677d2.html
服务端代码:
# UDPPingerServer.py
# 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(('127.0.0.1', 9600))
while True:
# Generate random number in the range of 0 to 10
print('Waiting for Client!')
rand = random.randint(0, 10)
# Receive the client packet along with the address it is coming from
message, address = serverSocket.recvfrom(1024)
print(message)
# Capitalize the message from the client
data = message.upper()# If rand is less is than 4, we consider the packet lost and do not respond
if rand < 4:
continue
# Otherwise, the server responds
serverSocket.sendto(data, address)
客户端代码
自己随手写的,还可以再完善下,也懒得改了,不过题目的基本要求已经达到了.
import time
from socket import *
server_address = ('127.0.0.1', 9600)
count = 0
message = b'python'
while count < 10:
clientSocket = socket(AF_INET, SOCK_DGRAM)
clientSocket.settimeout(1)
clientSocket.sendto(message, server_address)
start_time = time.clock()
try:
data, address = clientSocket.recvfrom(1024)
if data:
end_time = time.clock()
print('Ping:', (end_time - start_time)*1000)
print(data)
count += 1
except:
continue
最新文章
- Postman - 功能强大的 API 接口请求调试和管理工具
- 在jquery中,全选/全不选的表示方法
- HDOJ1001-1005题解
- PHP中错误处理集合
- Effective C++ -----条款18:让接口容易被正确使用,不易被误用
- C#-创建自定义双击事件
- 新闻专栏~ART让Android更流畅
- threejs 组成的3d管道,寻最短路径问题
- php使用websocket示例详解
- python2 线程基础
- source insight 中文乱码解决方法
- linux nohup
- SEVERE: An incompatible version 1.1.27 of the APR based Apache Tomcat Native library is installed, while Tomcat requires version 1.1.32
- 步步为营-54-DOM
- adb monkey测试 命令
- asp.net开发中的问题总结
- sas基础系列(3)-表格标颜色示例
- November 16th 2016 Week 47th Wednesday
- jdbc之Statement和Preparement
- struts.xml 文件添加DTD文件
热门文章
- GPU和显卡是什么关系?GPU会取代CPU吗?
- [web] 前端一些细节
- python dlib学习(五):比对人脸
- Couldn&#39;t open file /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 解决办法(转)
- Proj.4 升级新版本5.x和6.x
- ES开启慢查询日志
- Q-Dir – 布局灵活的文件管理,强烈推荐
- aardio类的例子
- Python3基础 tuple 使用通配符*进行拆包 简单示例
- SQL存储实现将JSON自动转化成SQL数据列