Reputation: 37
i have a TCP Client on a Raspberry Pi connecting to a network Printer:
sock = socket.socket(sockwt.AF_INET, socket.SOCK_STREAM)
sock.settimeout(5)
sock.connect((ip,port))
My Problem ist now that the socket stays open when I unplug the network cable or turn off the printer.
And sending data does not fail -
sock.send(data)
always returns the correct number of bytes
So i tried to add a keep alive
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 1)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 3)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 2)
but that changes nothing.
So why does the sock.send(data) not fail, when the server has died? or: how can i make sock.send fail?
When I send data and the server has died, there should be no ACK. Why do I not get a information about that?
many thanks!
Upvotes: 0
Views: 132
Reputation: 149185
I am afraid that this is a TCP thing. You have a stream socket, and you asked for sending some bytes. A correct answer only means that the bytes could be sent but makes no guarantees for the peer receiving them. It is necessary because you could have a complex network with a number of switches and routers on the path.
Under normal circumstances, you should get an error at a time but possibly after sending some packets.
What could be done?
Some printer protocols have provision for commands close to Are You There? or Who Are You? that allow the client to ask the server for a message. That would be a robust way to make sure that the printer is ready.
Upvotes: 1