How do I clear the buffer upon start/exit in ZMQ socket? (to prevent server from connecting with dead clients)

别来无恙 提交于 2019-12-05 15:04:20

Answering my own question-

The problem is that the first client sends a message which the server accepts when it starts running, regardless of the status of the client.

To prevent this, 2 things have to be done. The most important thing is to use socket.close() to close the client connection. Secondly, the LINGER parameter can be set to a low value or zero. This clears the buffer after the timeout value from the time the socket is closed.

class client:
    def clientInit(self):
...
            self.socket.setsockopt(zmq.LINGER, 100)   
...

def clientQuit(self):
        try:
            self.socket.send('bye')
            self.socket.recv() 
        except:
            print "Error disconnecting."    
        self.socket.close() 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!