Python socket flush

I try 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 a unix socket).

From my understanding (and from what I see on this board), just turning off the altar. should do this, but my server is still receiving my data in a piece of 4096 bytes (installed by default) ...

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...

Turning TCP_NODELAY on / off does not seem to have any effect ... Is this a bug or is something missing?

TIA

+3
source share
4 answers

TCP - "", . , , TCP , . 4096 , , , ( recv()).

TCP - , - . .

+4

, ( C), :

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

, my_writer_obj , -.

, .

+3

. , , , , :

self.sck.close()

, n = socket.send() . ,

self.sck.sendall()

:

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

( , sendall()). , recv(), . , .

+1

TCP. TCP . . , . , , .

In most cases, you do not need to worry about the Naggle algorithm. This algorithm is better described by the name TCP_NODELAY. If you turn it off, you can achieve lower latencies for smaller pieces, but at a lower speed for larger pieces at the same time.

0
source

Source: https://habr.com/ru/post/1779568/


All Articles