Python Socket Flush

后端 未结 4 2202
逝去的感伤
逝去的感伤 2020-11-30 12:53

I am trying to make sure that every time I call the socket.send function my buffer is sent (flushed) to my server (which is in C using unix socket).

From my understa

4条回答
  •  心在旅途
    2020-11-30 13:37

    There is no way to ensure the size of the data chunks that are sent. If you want to make sure that all the data that you want to send is send, you can close the connection:

    self.sck.close()
    

    Note also, that n = socket.send() returns the number of actual sent bytes. If you definitely want to send all data, you should use

    self.sck.sendall()
    

    or loop over the data sending:

    while data:
        n = self.sck.send(data)    
        data = data[n:]
    

    (But that is roughly the same as sendall() ). If you want to receive the data in bigger chunks, you can increase the size of the buffer in recv(), but this only makes the possible chunk size bigger. There is no guaranty that the data arrives in these sizes.

提交回复
热议问题