嗯,标题说明了一切......
我有一个简单的服务器:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ("", 5453)
sock.bind(server_address)
sock.listen(1)
while True:
connection, client_address = sock.accept()
while True:
data = connection.recv(16)
if data:
print(data.decode())
connection.close()
break
一个简单的客户:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ("localhost", 7171)
sock.connect(server_address)
while True:
given_text= input("Enter your text: ")
sock.sendall(str.encode(given_text))
data = sock.recv(16)
print(data.decode())
事情是,如果客户端在键入文本之前关闭连接,则服务器崩溃并显示错误:
data = connection.recv(16)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
我该如何解决?
答案 0 :(得分:1)
客户端没有关闭连接。它退出而没有关闭连接,这导致Windows 重置连接。解决方案:正确关闭它。但是服务器总是必须能够应对连接重置。