套接字Python 3 UDP连接重置错误:[WinError 10054]远程主机强制关闭了现有连接

vlju58qv  于 2022-12-20  发布在  Python
关注(0)|答案(2)|浏览(421)

我有插座问题

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

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

krugob8w

krugob8w1#

您没有在herk-PC:12000(UDP)上运行服务器,或者服务器与服务器之间存在防火墙。请在本地计算机上运行服务器,并让客户端连接到localhost:12000,以确保一切正常。
如果你仍然有同样的问题,你有没有在你的服务器上使用bind(('localhost',12000))

kuuvgm7e

kuuvgm7e2#

#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_服务器

相关问题