【问题标题】:Socket Python 3 UDP ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote hostSocket Python 3 UDP ConnectionResetError: [WinError 10054] 现有连接被远程主机强行关闭
【发布时间】:2015-05-24 19:23:12
【问题描述】:

我有套接字问题

import socket

serverName = "herk-PC"
serverPort = 12000

clientSocket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

message = input('input lowercase sentence:')

clientSocket.sendto(message.encode('utf-8'),(serverName, serverPort))

modifiedMessage, serverAddress = clientSocket.recvfrom(2048)


print (modifiedMessage.decode('utf-8'))

clientSocket.close()

这段代码给我错误

Traceback (most recent call last):
  File "J:\Sistem Jaringan\Task I\client.py", line 12, in <module>
    modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

我的错误有什么解决办法吗?

【问题讨论】:

    标签: python sockets python-3.x udp


    【解决方案1】:

    要么您没有在 herk-PC:12000 (UDP) 上运行的服务器,要么两者之间有防火墙。在您的本地计算机上运行服务器并让客户端连接到localhost:12000,以确保一切正常。

    如果您仍然遇到同样的问题,您是否在服务器上使用过bind(('localhost',12000))

    【讨论】:

      【解决方案2】:
      #udp_client.py
      import socket
      
      target_host = '127.0.0.1'
      target_port = 7210
      
      client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
      nBytes = client.sendto('ABCDEF'.encode('utf-8'), (target_host, target_port))
      print(nBytes, 'Bytes', 'Send OK')`
      

      udp 客户端

      #udp_server.py
      import socket
      
      bind_host = '127.0.0.1'
      bind_port = 7210
      
      sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
      sock.bind((bind_host, bind_port))
      
      data, addr = sock.recvfrom(4096)
      print(data.decode('utf-8'), addr[0], ':', addr[1])
      

      udp_server

      【讨论】:

        猜你喜欢
        • 2015-02-28
        • 2018-01-02
        • 2020-04-20
        • 1970-01-01
        • 2016-10-20
        • 2020-10-09
        • 2020-07-24
        • 2023-03-03
        • 2021-03-31
        相关资源
        最近更新 更多