11

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 understanding (and from what I see on this board) just disabling the naggle algo. should do it but my server still receive my data in chunk of 4096 bytes (default set)...

Im using the following code in Python v2.5.4:

 self.sck = socket( AF_INET, SOCK_STREAM )

 self.sck.setsockopt( IPPROTO_TCP, TCP_NODELAY, 1 ) # That doesn't seems to work...

 self.sck.connect( ( "127.0.0.1", "12345" ) )

 while( 1 ):
      self.sck.send( "test\n" )
      self.sck.send( "" ) # Still trying to flush...

Enabling/Disabling TCP_NODELAY seems that have no effect whatsoever... Is this a bug or I am missing something?

TIA

Bob McLaury
  • 409
  • 1
  • 5
  • 6

5 Answers5

9

TCP does not provide any kind of guaranteed "packet" sending to the other end. You are sending data as fast as you can, and TCP is helpfully batching up the data into as much as it can send at once. Your server is receiving data 4096 bytes at a time, probably because that's what it asked for (in a recv() call).

TCP is a stream protocol and therefore you will have to implement some kind of framing yourself. There are no built-in message boundaries.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
7

The only way I got a flush to work (back to a C client) was to use:

my_writer_obj = mysock.makefile(mode='w', ...)
my_writer_obj.write('my stuff')
my_writer_obj.flush()

The big advantage is that my_writer_obj defaults to text mode, so no more byte translation.

creating a reader object did not go as smoothly, but I did not need a flush on that side.

Stuart Deal
  • 75
  • 1
  • 4
  • This is a lifesaver. Cheers! – pbnelson Dec 13 '16 at 02:10
  • -1 This answer is wrong. TCP protocol by design has no way to send data in chunks. It can only send stream of data. The code in this answer is equivalent to the one posted in the question. – alyaxey Mar 09 '18 at 13:26
  • 2
    From the Python3 sockets page:Now there are two sets of verbs to use for communication. You can use send and recv, or you can transform your client socket into a file-like beast and use read and write. The latter is the way Java presents its sockets. I’m not going to talk about it here, except to warn you that you need to use flush on sockets. These are buffered “files”, and a common mistake is to write something, and then read for a reply. Without a flush in there, you may wait forever for the reply, because the request may still be in your output buffer. – Stuart Deal Apr 01 '18 at 06:00
6

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.

xubuntix
  • 2,333
  • 18
  • 19
  • 2
    I see, however in my case I need to have "real-time" interaction... So what are my options? I mean how does ie a telnet server do it, as soon as you push enter the command is directly send, same for an FTP client... I would like to reproduce something similar as in my case I need to send data at 30fps... having the buffer that is stalling until its full and then be sent or adding extra data just to make sure that the buffer is pushed doesn't seems like an option to me... So what can I do? – Bob McLaury Dec 10 '10 at 13:55
  • 1
    @Bob McLaury: "in my case I need to have "real-time" interaction." You're doing it wrong. (1) search for real-time network or something similar here. Then (2) search for UDP. You can't use TCP for this. You have to use UDP. – S.Lott Dec 10 '10 at 15:02
4

This is a common question about TCP protocol. TCP itself has no way to send data in specific chunks. It's designed only for sending stream of data. If you need such functionality, you should implement it yourself. For example, send your chunks in separate lines or first send chunk size and then the chunk itself.

In most cases, you don't need to care about the Naggle algorithm. This algorithm is better described by the name TCP_NODELAY. If you disable it, you may achieve smaller delays for small chunks but lower speed for large chunks at the same time.

alyaxey
  • 1,129
  • 12
  • 10
3

In Linux, this is possible with a call to ioctl:

SIOCOUTQ
Returns the amount of unsent data in the socket send queue. The > socket must not be in LISTEN state, otherwise an error (EINVAL) is returned. SIOCOUTQ is defined in <linux/sockios.h>. Alternatively, you can use > the synonymous TIOCOUTQ, defined in <sys/ioctl.h>.

Quick look to linux/sockios.h:

/* Linux-specific socket ioctls */
#define SIOCINQ         FIONREAD
#define SIOCOUTQ        TIOCOUTQ        /* output queue size (not sent + not acked) */

So, applying this is fairly easy:

import socket

def flush_socket(sock: socket.socket) -> bool:
    from ctypes import c_ulong
    from time import sleep
    from termios import TIOCOUTQ
    from fcntl import ioctl

    while sock.fileno() != -1: # if fd is -1, then it has been probably close()'d
        remaining = c_ulong.from_buffer_copy(
            ioctl(sock.fileno(), TIOCOUTQ, bytearray(8), False)).value

        if remaining == 0:
            # all data has been sent and ACKed
            return True

        # wait a bit before retrying,
        # sleep(0) was meant like yield current thread,
        # but will probably be close to busy-waiting,
        # feel free to change it to fit your needs
        sleep(0)

    # not all data has been sent
    return False
Zaffy
  • 16,801
  • 8
  • 50
  • 77