1

我收到以下错误消息:

Traceback (most recent call last):
  File "UDPPingerClient2.py", line 20, in <module>
    data, server = sock.recvfrom(1024)
ConnectionResetError: [WinError 10054] An existing connection was forcible closed by the remote host

我真的不知道问题是什么,因为我班上的其他人使用了基本相同的代码并让它正常运行。请帮忙!

这是我在记事本++中的代码,我正在通过命令提示符运行它:

import socket
import time

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_addr = ('localhost', 12000)
sock.settimeout(1)


try:
 for i in range(1, 11):
  start = time.time()
  message = 'Ping #' + str(i) + " " + time.ctime(start)
  try:
   sent = sock.sendto(message.encode(), server_addr)
   print("Sent " + message)
   data, server = sock.recvfrom(1024)
   print("Received " + data.decode())
   end = time.time();
   elapsed = end - start
   print("RTT: " + str(elapsed) + " seconds\n")

  except socket.timeout:
   print("#" + str(i) + " Requested Timed Out\n")

finally:
 print("closing socket")
 sock.close()
4

0 回答 0