服务器套接字在我从 chrome 发送时收到 2 个 http 请求,在我从 firefox 发送时收到一个 [英] server socket receives 2 http requests when I send from chrome and receives one when I send from firefox
问题描述
我在 linux 下使用 C 语言编写了一个简单的服务器,它在本地主机上的端口 80 上侦听.现在,当我从浏览器 google chrome 向程序发送请求时,它会收到 2 个请求,而当我从 Firefox 发送时,它只收到一个请求.
我在浏览器中输入的 URL 是:http://localhost/xyz.html>
当我在 Chrome 中输入 URL 时的输出
root@anirudh-Aspire-5920:/home/anirudh/workspace/DCMTOL# ./DCMTOL_RUN内部 HTTP 服务器处理程序内部 HTTP 请求处理程序**检测到的请求:clientsocket_fd = 6 clientportnumber = 38027**获取/xyz.html HTTP/1.1主机:本地主机连接:保持连接缓存控制:max-age=0接受:application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5用户代理:Mozilla/5.0(X11;U;Linux i686;en-US)AppleWebKit/534.10(KHTML,像 Gecko)Chrome/8.0.552.224 Safari/534.10接受编码:gzip、deflate、sdch接受语言:en-US,en;q=0.8接受字符集:ISO-8859-1,utf-8;q=0.7,*;q=0.3内部 HTTP 请求处理程序**检测到的请求:clientsocket_fd = 7 clientportnumber = 38029**^Croot@anirudh-Aspire-5920:/home/anirudh/workspace/DCMTOL#
第二个请求不发送任何数据,所以我的代码等待读取调用,所以我必须终止它'^C'.
当我在 Firefox 中输入 URL 时的输出
root@anirudh-Aspire-5920:/home/anirudh/workspace/DCMTOL# ./DCMTOL_RUN内部 HTTP 服务器处理程序内部 HTTP 请求处理程序**检测到的请求:clientsocket_fd = 6 clientportnumber = 45567**获取/xyz.html HTTP/1.1主机:本地主机用户代理:Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13接受:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8接受语言:en-us,en;q=0.5接受编码:gzip,deflate接受字符集:ISO-8859-1,utf-8;q=0.7,*;q=0.7保持活力:115连接:保持连接^Croot@anirudh-Aspire-5920:/home/anirudh/workspace/DCMTOL#
问题:当我只输入一次 URL 时,chrome 浏览器如何发送 2 个请求(一个为空).正如您在上面看到的,我检测到 2 个请求.我尝试在从 chrome 发送 URL 的情况下执行 netstat,我发现这两个请求都仅由浏览器发送.正如你在上面看到的,当我从 Firefox 发送 URL 时,只收到 1 个请求.
这是我从 chrome 发送请求时 net stat 的输出
活动的 Internet 连接(无服务器)Proto Recv-Q Send-Q 本地地址 外地址 状态 PID/程序名称tcp 0 0 117.195.110.186:48701 74.125.77.102:80 TIME_WAIT -tcp 0 0 117.195.110.186:48700 74.125.77.102:80 已建立 5699/google-chrometcp 0 0 117.195.110.186:55815 209.85.175.138:80 已建立 5699/google-chrometcp 0 0 127.0.0.1:80 127.0.0.1:38029 已建立 -tcp 0 0 127.0.0.1:38029 127.0.0.1:80 已建立 5699/google-chrometcp 0 0 127.0.0.1:38027 127.0.0.1:80 已建立 5699/google-chrometcp 0 0 127.0.0.1:80 127.0.0.1:38027 已建立 -tcp 0 0 117.195.110.186:35402 74.125.153.125:5222 已建立 4430/pidgin
提前致谢:)
我的节点服务器遇到了类似的问题.这是由于 Chrome 中的以下错误所致.总之,Chrome 会针对每个请求发送一个网站图标 的请求.很可能,您不会发回网站图标,它会在每个合法请求之后请求一个.
Firefox 和大多数其他浏览器在第一次连接时也会发出对网站图标的请求,但会缓存结果,即如果第一次没有返回网站图标,它们就不会继续尝试 - 这就是您的原因' 只看到来自 Firefox 的一个请求.不幸的是,Chrome 对网站图标的要求似乎有点过于执着了.
I wrote a simple server using socket API in C under linux which listens at port 80 on localhost. Now when I send a request from the browser google chrome to the program it receives 2 requests while it receives only one when I send from firefox.
The URL I typed in the browser was: http://localhost/xyz.html
OUTPUT WHEN I TYPE URL IN CHROME
root@anirudh-Aspire-5920:/home/anirudh/workspace/DCMTOL# ./DCMTOL_RUN
Inside HTTP server Handler
Inside HTTP request Handler
**Detected request: clientsocket_fd = 6 clientportnumber = 38027**
GET /xyz.html HTTP/1.1
Host: localhost
Connection: keep-alive
Cache-Control: max-age=0
Accept:application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Inside HTTP request Handler
**Detected request: clientsocket_fd = 7 clientportnumber = 38029**
^C
root@anirudh-Aspire-5920:/home/anirudh/workspace/DCMTOL#
the second request does not send any data so my code waits at the read call and so I have to terminate it '^C'.
OUTPUT WHEN I TYPE URL IN FIREFOX
root@anirudh-Aspire-5920:/home/anirudh/workspace/DCMTOL# ./DCMTOL_RUN
Inside HTTP server Handler
Inside HTTP request Handler
**Detected request: clientsocket_fd = 6 clientportnumber = 45567**
GET /xyz.html HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
^C
root@anirudh-Aspire-5920:/home/anirudh/workspace/DCMTOL#
Question: How can chrome browser send 2 requests (one being empty) when I typed the URL only once. As you can see above I detected 2 requests. I tried to do netstat in the case of sending URL from chrome and I found that both of the request were sent by the browser only. and as u can see above when I send the URL from firefox only 1 request is received.
Here is the output of net stat when I send request from chrome
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 117.195.110.186:48701 74.125.77.102:80 TIME_WAIT -
tcp 0 0 117.195.110.186:48700 74.125.77.102:80 ESTABLISHED 5699/google-chrome
tcp 0 0 117.195.110.186:55815 209.85.175.138:80 ESTABLISHED 5699/google-chrome
tcp 0 0 127.0.0.1:80 127.0.0.1:38029 ESTABLISHED -
tcp 0 0 127.0.0.1:38029 127.0.0.1:80 ESTABLISHED 5699/google-chrome
tcp 0 0 127.0.0.1:38027 127.0.0.1:80 ESTABLISHED 5699/google-chrome
tcp 0 0 127.0.0.1:80 127.0.0.1:38027 ESTABLISHED -
tcp 0 0 117.195.110.186:35402 74.125.153.125:5222 ESTABLISHED 4430/pidgin
thanks in advance :)
I had a similar issue with my node server. It is due to the following bug in Chrome. In summary, Chrome is sending a request for a favicon on every request. As, is likely, you aren't sending a favicon back, it requests one after every legitimate request.
Firefox, and most other browsers, also send out a request for a favicon when they first connect, but cache the result i.e. if there isn't a favicon returned first time, they don't keep trying - which is why you're only seeing a single request from Firefox. It seems Chrome is unfortunately a little too persistent with its favicon requestiness.
这篇关于服务器套接字在我从 chrome 发送时收到 2 个 http 请求,在我从 firefox 发送时收到一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!