服务器套接字从chrome发送时收到2个http请求,当我从firefox发送时收到一个http请求

编程入门 行业动态 更新时间:2024-10-25 10:24:58
本文介绍了服务器套接字从chrome发送时收到2个http请求,当我从firefox发送时收到一个http请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在Linux下用C语言编写了一个简单的服务器,它在localhost上侦听端口80。现在,当我从浏览器google chrome向程序发送请求时,它会收到2个请求,而当我从firefox发送时只收到一个请求。

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.

URL I在浏览器中键入: http://localhost/xyz.html

当我输入网址为CHROME时输出

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#

第二个请求不发送任何数据,所以我的代码在读取调用时等待,所以我必须终止它'^ C'。

the second request does not send any data so my code waits at the read call and so I have to terminate it '^C'.

当我输出时FIREFOX中的TYPE URL

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#

问题:当我只输入一次网址时,Chrome浏览器如何发送2个请求(一个为空)。如您所见,我检测到2个请求。我试图在从chrome发送URL的情况下执行netstat,我发现这两个请求都是由浏览器发送的。当我从firefox发送URL时,你可以看到上面只收到1个请求。

以下是我的net stat的输出从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

提前感谢:)

推荐答案

我的节点服务器遇到了类似的问题。这是由于Chrome中的以下错误造成的。总而言之,Chrome会在每次请求时发送 favicon 请求。因为,很可能,你没有发回一个favicon,它会在每个合法请求后请求一个。

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和大多数其他浏览器也会发出请求一个首选连接时的图标,但缓存结果,即如果第一次没有返回图标,他们就不会继续尝试 - 这就是为什么你只看到来自Firefox的单个请求。不幸的是,Chrome有点太持久了,因为它的favicon请求。

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发送时收到一个http请求

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

发布评论

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

>www.elefans.com

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