Python 套接字刷新 [英] Python Socket Flush

查看:36
本文介绍了Python 套接字刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图确保每次调用 socket.send 函数时,我的缓冲区都被发送(刷新)到我的服务器(在 C 中使用 unix 套接字).

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

根据我的理解(以及我在这个板上看到的)只是禁用了 naggle 算法.应该这样做,但我的服务器仍然以 4096 字节的块(默认设置)接收我的数据...

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

我在 Python v2.5.4 中使用以下代码:

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
" )
      self.sck.send( "" ) # Still trying to flush...

启用/禁用 TCP_NODELAY 似乎没有任何影响......这是一个错误还是我遗漏了什么?

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

TIA

推荐答案

TCP 不提供任何类型的有保证的数据包"发送到另一端.您正在以最快的速度发送数据,TCP 有助于将数据批量处理成一次可以发送的数量.您的服务器一次接收 4096 个字节的数据,这可能是因为它要求(在 recv() 调用中).

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 是一种流协议,因此您必须自己实现某种框架.没有内置的消息边界.

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

这篇关于Python 套接字刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆