Python 套接字刷新

编程入门 行业动态 更新时间:2024-10-27 15:28:30
本文介绍了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 套接字刷新

本文发布于:2023-08-07 03:51:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1316814.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!