简体   繁体   English

如何检测客户端何时断开与UDS的连接(Unix域套接字)

[英]How to detect when a client disconnects from a UDS (Unix Domain Socket)

When a client connects to the pipe, and sends data I can receive this fine and I can keep receiving the data. 当客户端连接到管道,并发送数据,我可以收到这个罚款,我可以继续接收数据。 Trouble comes when the client disconnects and the while loop is still active, connection.recv() doesn't block and therefore keeps looping frantically! 当客户端断开连接并且while循环仍处于活动状态时,遇到麻烦,connection.recv()不会阻塞,因此会疯狂地循环! So I need a way to detect if a client is still connected. 所以我需要一种方法来检测客户端是否仍然连接。

I have the following code: 我有以下代码:

    pipe = './pipes/uds_defzone-lrecv'

    try:
        os.unlink(pipe)
    except OSError:
        if os.path.exists(pipe):
            raise
    self.logger.debug('Created UDS pipe: ' + pipe)

    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    sock.bind(pipe)
    sock.listen(1)

    self.logger.debug('Waiting for connection: ' + pipe)
    connection, client_address = sock.accept()
    self.logger.debug('Connection from: ' + client_address)

    while True:

        self.logger.debug('Waiting for data')
        data = connection.recv(4096)
        self.logger.debug('Received: ' + str(data))

For reference, the sender.py code: 作为参考,sender.py代码:

# Create a UDS socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

# Connect the socket to the port where the server is listening
pipe = './pipes/uds_defzone-lrecv'

logger.debug('connecting to: ' + pipe)

try:
    sock.connect(pipe)
except socket.error, msg:
    logger.debug(msg)
    sys.exit(1)

try:
    message = 'THIS IS A TEST'
    logger.debug('sending: ' + message)
    sock.sendall(message)
    time.sleep(2)

finally:
    logger.debug('closing socket')
    sock.close()

TIA! TIA!

UPDATE UPDATE

I can slow it down with the following code I suppose, but not exactly what I want. 我可以用我想的下面的代码来减慢速度,但不完全是我想要的。

    while True:

        try:
            self.logger.debug('Waiting for data')
            data_present = select.select([sock], [], [], 30)
            if data_present[0]:
                data = connection.recv(4096)
                self.logger.debug('Received: ' + data)
        except select.timeout:
            pass

UPDATE 2 更新2

For reference this is the code I came up with: 作为参考,这是我提出的代码:

        while True:

            logger.debug('Waiting for data')
            data = connection.recv(4096)
            if not data == '':
                logger.debug('Received: ' + data)
            else:
                logger.debug('Nothing received')
                break

A hack I came up with in the process... Might be usable where it is legitimate that a client might send empty data, for signalling perhaps? 我在这个过程中想出了一个黑客...可能在客户端发送空数据合法的地方可用,或许可能发出信号?

        while True:

            try:
                logger.debug('Waiting for data')
                data = connection.recv(4096)
                # *** This throws an exception when client has disconnected
                x = connection.getpeername()
                logger.debug('Received: ' + data)
            except:
                logger.debug('Client disconnected')
                break

connection.recv() doesn't block and therefore keeps looping frantically! connection.recv()不会阻塞,因此会疯狂地循环! So I need a way to detect if a client is still connected. 所以我需要一种方法来检测客户端是否仍然连接。

If the peer disconnects recv data will return empty data ( '' ). 如果对等方断开连接,则recv数据将返回空数据( '' )。 You need to check this and exit the loop. 您需要检查并退出循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM