如何解释以下QTcpSocket延迟? [英] How can the following QTcpSocket delay be explained?
问题描述
我有两个进程通过TCP套接字相互通信。进程A向进程B发送请求并等待响应。进程B处理请求,并发送响应。对于一些请求,不必发送对进程B的响应。
假设进程A首先发送不需要响应的请求X,并立即之后发送请求Y,需要响应,当A在其套接字上等待数据时(〜0.04s),观察到显着的小延迟。这是控制流:
A发送X
A发送Y
B处理X
B处理Y并写响应
(在〜0.04s的A中waitForReadyRead()的小延迟)
A接收对Y
当我为X引入响应时,让A等待响应,延迟消失,控制流程如下:
A发送X
B处理X并写入响应
A接收对X
的响应A发送Y
B处理Y并写入响应
A接收对Y
的响应
某种同步问题,但我不能解释。任何人都可以解释在情况B不发送X的响应时引发的小延迟
http://en.wikipedia.org/ wiki / Nagle_algorithm
您的数据已排队,不会发送。您需要设置套接字选项(TCP_NODELAY)以防止这种情况,或使用一些其他传输协议,如UDP。您可以在man tcp中获取更多信息。
I have two processes communicating with each other over TCP sockets. Process A sends a request to process B and waits for a response. Process B handles to request, and sends a response. For some of the requests, it is not necessary to send a response for process B.
Suppose process A first sends a request X that does not require a response, and immediately after that sends a request Y that does require a response, a noticeable small delay is observed when A is waiting for data on its socket (~0.04s). This is the flow of control:
A sends X
A sends Y
B handles X
B handles Y and writes response
(small delay in waitForReadyRead() in A of ~0.04s)
A receives response for Y
When I introduce a response for X as well, and let A wait for that response, the delay is gone, and the flow of control looks as follows:
A sends X
B handles X and writes response
A receives response for X
A sends Y
B handles Y and writes response
A receives response for Y
This is obviously some kind of synchronisation issue, but I cannot explain it. Can anyone explain the small delay that is induced in case B does not send a response for X?
This is the answer:
http://en.wikipedia.org/wiki/Nagle_algorithm
Your data is queued and not sent out. You need to set a socket option (TCP_NODELAY) to prevent this, or use some other transport protocol, like UDP. You can get more info in 'man tcp'.
这篇关于如何解释以下QTcpSocket延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!