ConnectionResetError:[WinError 10054]远程主机强行关闭现有连接

时间:2014-03-10 12:44:17

标签: python sockets

我是使用Socket进行Python编程的初学者。 现在,我遇到了这个错误的问题

data, address = s.recvfrom(MAX)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

以下是我的代码:

import socket, sys
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

MAX = 65535
PORT = 1060

a = raw_input()

if a == "server":
    s.bind(("127.0.0.1",PORT))
    print("Listening at ", s.getsockname())
    while True:
        data, address = s.recvfrom(MAX)
        print("The client at " + str(address) + " says " + repr(data))
elif a == "client":
    s.sendto("This is my message".encode(), ("127.0.0.1", PORT))

    print("Address after sending: ", s.getsockname())
    data, address = s.recvfrom(MAX)
    print("The server " + address + " says " + repr(data))
else:
    print(sys.stderr)

运行程序并输入“client”作为输入后,会导致错误。 谢谢您的回答。 ;)

1 个答案:

答案 0 :(得分:1)

同时运行服务器和客户端:)