插座的read()挂了一段时间,当没有数据读取 [英] Socket read() hangs for a while when there is no data to read

查看:126
本文介绍了插座的read()挂了一段时间,当没有数据读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我正在写一个简单的HTTP端口转发器。我读了80端口的数据,并将数据传递给我的lighttpd服务器,端口8080。

Hi' I'm writing a simple http port forwarder. I read data from port 80, and pass the data to my lighttpd server, on port 8080.

只要我写的()端口8080(转发请求)没有任何问题,但是当我读()数据从插座(转发响应)插座上的数据,在过去的read()挂了很多(约1或2秒)之前实现没有更多的数据并返回0。

As long as I write() data on the socket on port 8080 (forwarding the request) there's no problem, but when I read() data from that socket (forwarding the response), the last read() hangs a lot (about 1 or 2 seconds) before realizing there's no more data and returning 0.

我试图设置套接字非阻塞,但这不起作用,因为有时它返回EWOULDBLOCKING即使有一些数据左(lighttpd的+ CGI可能很慢)。
我试图设定选择()超时,但是,如上所述,一个缓慢的CGI可以超时插座时,有一些实际的数据传输。

I tried to set the socket to non-blocking, but this doesn't work, as sometimes it returns EWOULDBLOCKING even if there's some data left (lighttpd + cgi can be quite slow). I tried to set a timeout with select(), but, as above, a slow cgi could timeout the socket when there's actually some data to transmit.

更新:解决。这是毕竟存活。我在lighttpd的配置文件中禁用后,整个事情完美运行。

Update: SOLVED. It was the keepalive after all. After I disabled it in my lighttpd configuration file, the whole thing runs flawlessly.

推荐答案

好了,完成的缘故,而按我的评论:

Well, for the sake of completion, and as per my comment:

这是可能的HTTP服务器本身(的lighttpd你的情况)是维持到您的代理持久连接,因为您的代理转发含有&ldquo的标题; <​​code>连接:保持活动&rdquo ;.当客户端希望使通过相同的连接多个请求这头爱滋病。所以,因为收到的lighttpd这个头,它认为它要接受进一步的要求,并保持插座开放,引起在代理阻止。

It is likely that the HTTP server itself (lighttpd in your case) is maintaining a persistent connection to your proxy because your proxy relayed a header containing “Connection: keep-alive”. This header aids when the client wants to make multiple requests over the same connection. So, because lighttpd received this header, it assumed it was going to receive further requests and kept the socket open, causing read to block in your proxy.

禁用保活在你的lighttpd的配置是解决它的一种方式,而且你也可以剥去&ldquo; 连接:保持活动&ldquo;从标题,你将其转发到Web服务器之前。

Disabling keep-alive in your lighttpd configuration is one way to fix it, but also you could also strip the “Connection: keep-alive“ from the header before you relay it to your web server.

这篇关于插座的read()挂了一段时间,当没有数据读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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