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

最新文章

  1. Postman - 功能强大的 API 接口请求调试和管理工具
  2. 在jquery中,全选/全不选的表示方法
  3. HDOJ1001-1005题解
  4. PHP中错误处理集合
  5. Effective C++ -----条款18:让接口容易被正确使用,不易被误用
  6. C#-创建自定义双击事件
  7. 新闻专栏~ART让Android更流畅
  8. threejs 组成的3d管道,寻最短路径问题
  9. php使用websocket示例详解
  10. python2 线程基础
  11. source insight 中文乱码解决方法
  12. linux nohup
  13. SEVERE: An incompatible version 1.1.27 of the APR based Apache Tomcat Native library is installed, while Tomcat requires version 1.1.32
  14. 步步为营-54-DOM
  15. adb monkey测试 命令
  16. asp.net开发中的问题总结
  17. sas基础系列(3)-表格标颜色示例
  18. November 16th 2016 Week 47th Wednesday
  19. jdbc之Statement和Preparement
  20. struts.xml 文件添加DTD文件

热门文章

  1. GPU和显卡是什么关系?GPU会取代CPU吗?
  2. [web] 前端一些细节
  3. python dlib学习(五):比对人脸
  4. Couldn&#39;t open file /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 解决办法(转)
  5. Proj.4 升级新版本5.x和6.x
  6. ES开启慢查询日志
  7. Q-Dir – 布局灵活的文件管理,强烈推荐
  8. aardio类的例子
  9. Python3基础 tuple 使用通配符*进行拆包 简单示例
  10. SQL存储实现将JSON自动转化成SQL数据列