Well my problem is this:
We are starting to see the topic of sockets and I have these examples:
CLIENT:
import socket
host = "localhost"
port = 9999
socket1 = socket.socket()
socket1.connect((host, port))
try:
while(True):
cadena = input("Mensaje para enviar al servidor: ")
socket1.send(cadena.encode(encoding='utf-8'))
print("se ha enviado la cadena: ", cadena)
CadenaRecServidor = socket1.recv(1024).decode('utf-8')
print("El servidor responde: ", CadenaRecServidor)
except Exception as e:
print("error", e)
socket1.close()
SERVER:
import socketserver
class MiTcpHandler(socketserver.BaseRequestHandler):
def handle(self):
try:
self.cadena = self.request.recv(1024).decode('utf-8')
print("Cliente:", self.cadena)
self.CadenaSendServer = input("Sevidor: ")
self.request.send(self.CadenaSendServer.encode(
encoding='utf-8', errors='strict'))
except Exception as e:
print("error", e)
host = "localhost"
port = 9999
server1 = socketserver.TCPServer((host, port), MiTcpHandler)
print("Servidor Corriendo")
server1.serve_forever()
My problem is that when I run them they connect without problems. First I send a message from client to server. I then reply from the server to the client with another message. The problem is that when trying to send a third message from the client again, the program (client only, server if it remains active) quits unexpectedly and displays the following message:
error [VinError 10053] A connection established by software on your host machine has been aborted*
Can someone please help me with this error or who has had experience in these cases. Or at least explain to me why it happens, if I can understand what is happening then I will solve it myself.
Thanks in advance, I'll be here as long as I need.
Error 10053 occurs when you try to write from one side of the communication to a socket that has been closed from the other side.
So this indicates that the server has closed the socket after sending the first message to the client. Why has he done that?
This is related to how the module works
socketserver
. In this module, when you invokeserve_forever()
the following happens:MiTcpHandler
, in this case). At that moment, another (data) socket is created that is assigned to the fieldrequest
of that object..handle()
of that object, and wait for it to.handle()
return..finish()
of that object. In your case you have not programmed that method, so it will use the one you inherit fromBaseRequestHandler
, which is limited to closing the data socket.MiTcpHandler
to handle it, etc.)So here we have the answer. The framework
socketserver
expects all network traffic to be handled in the.handle()
. As soon as that method returns, it is understood that the communication has finished and therefore the socket is closed.The solution therefore is to do, within
.handle()
a loop that repeats several times the reading and writing of the socket. If you want to make the server stay in that loop until the client has disconnected, you must use the receipt of an empty string as the exit condition of the loop. That is, if when you doself.request.recv(1024).decode('utf-8')
you receive""
this it means that the other end (the client) has closed the socket. In that case the server can terminate.handle()
so that its socket is closed as well.