使用的资料

计算机网络自顶向下方法 第7版

参考前人第六版资料

操作系统:Windows 7

我的笔记

实验步骤

Client Code You need to implement the following client program.
The client should send 10 pings to the server. Because UDP is an unreliable protocol, a packet sent from the client to the server may be lost in the network, or vice versa. For this reason, the client cannot wait indefinitely for a reply to a ping message. You should get the client wait up to one second for a reply; if no reply is received within one second, your client program should assume that the packet was lost during transmission across the network. You will need to look up the Python documentation to find out how to set the timeout value on a datagram socket. Specifically, your client program should (1) send the ping message using UDP (Note: Unlike TCP, you do not need to establish a connection first, since UDP is a connectionless protocol.) (2) print the response message from server, if any (3) calculate and print the round trip time (RTT), in seconds, of each packet, if server responses (4) otherwise, print “Request timed out” During development, you should run the UDPPingerServer.py on your machine, and test your client by sending packets to localhost (or, 127.0.0.1). After you have fully debugged your code, you should see how your application communicates across the network with the ping server and ping client running on different machines. Message Format The ping messages in this lab are formatted in a simple way. The client message is one line, consisting of ASCII characters in the following format: Ping sequence_number time where sequence_number starts at 1 and progresses to 10 for each successive ping message sent by the client, and time is the time when the client sends the message.

自己在写的时候,对Python语法比较生疏,并且使用了try-except,但是并没有打印错误信息,而是输出Request timed out,以为是设计逻辑出了问题,debug很久。

之后才注意到是print()语法格式写错了,但是没有打印报错信息。

  1. #UDPPingerClient.py
  2. from socket import *
  3. from time import time
  4. serverName = 'localhost'
  5. serverPort = 12000
  6. clientSocket = socket(AF_INET, SOCK_DGRAM)
  7. clientSocket.settimeout(2.0);
  8. for i in range(1, 11):
  9. sentTime = time();
  10. message = "Ping " + str(i) + " " + str(sentTime)
  11. try:
  12. clientSocket.sendto(message.encode(), (serverName, serverPort))
  13. retMessage, serverAddress = clientSocket.recvfrom(2048)
  14. RTT = time() - sentTime
  15. print("{} RTT = {:.5f}".format(retMessage.decode(), RTT))
  16. except Exception as e:
  17. # print(e)
  18. print("Request timed out")
  19. clientSocket.close()

测试结果

image-20210118144218010.png

关于扩展练习

  1. Currently, the program calculates the round-trip time for each packet and prints it out individually.
    Modify this to correspond to the way the standard ping program works. You will need to report the minimum, maximum, and average RTTs at the end of all pings from the client. In addition, calculate the packet loss rate (in percentage).
  2. Another similar application to the UDP Ping would be the UDP Heartbeat. The Heartbeat can be used to check if an application is up and running and to report one-way packet loss. The client sends a sequence number and current timestamp in the UDP packet to the server, which is listening for the Heartbeat (i.e., the UDP packets) of the client. Upon receiving the packets, the server calculates the time difference and reports any lost packets. If the Heartbeat packets are missing for some specified period of time, we can assume that the client application has stopped.
    Implement the UDP Heartbeat (both client and server). You will need to modify the given UDPPingerServer.py, and your UDP ping client.

扩展练习都暂且保留。

总结

本次实验是是基于UDPClient.py的改写,一些不太熟悉的地方需要经常查文档,比如Python字符串的格式化,很容易遗忘。