我有一个小的服务器端程序,它接受来自客户端的连接并写入套接字 . 我想在客户端断开连接后立即从服务器端进行检测 . 我看到客户端在断开连接时发送了一个TCP Fin,服务器只在有一些数据发送到客户端时检测到它,我看到Broken pipe异常 . 相反,我希望服务器能够检测到客户端已发送完成并立即关闭会话 . 有人可以通过提供一些意见来帮助我 .
>>> Error sending data: [Errno 32] Broken pipe
[DEBUG] (Keepalive-Thread) Exiting
服务器程序接受连接:
def enable_pce(ip):
#Step 1: Create a TCP/IP socket to listen on. This listens for IPv4 AFI
srv = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#Step 2: Prevent from "address in use" upon server restart
srv.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
#Step 3: Bind the socket to you server ip address
srv_address = (ip,55555)
print 'starting server on %s port %s' % srv_address
srv.bind(srv_address)
#Step 4: Listen for connections
srv.listen(1)
#Step 5: Wait for incoming connections
connection, peer_address = peer.accept()
print 'connection from', connection.getpeername()
return connection