English 中文(简体)
为什么除了第一次循环迭代之外,recv()对循环迭代都返回0个字节?
原标题:Why recv() returns 0 bytes at all for-loop iterations except the first one?

我正在用C++编写一个小型网络程序。除此之外,它还必须下载twitter个人资料图片。我有一个URL列表(stl::vector)。我认为我的下一步是创建for循环,通过套接字发送GET消息,并将图片保存到不同的png文件中。问题是,当我发送第一条消息,接收答案片段并保存png数据时,一切似乎都很好。但就在下一次迭代中,通过相同的套接字发送的相同消息通过recv()函数产生0个接收字节。我通过在循环体中添加一个套接字创建代码来解决这个问题,但我对套接字的概念有点困惑。看起来,当我发送消息时,套接字应该关闭并重新创建,以便将下一条消息发送到同一服务器(以便获得下一个图像)。这是套接字网络编程的正确方式吗?还是可以通过同一个套接字接收多条HTTP响应消息?

提前谢谢。

UPD:这是我创建套接字的循环代码。

    // Get links from xml.
    ...
    // Load images in cycle.
    int i=0;
    for (i=0; i<imageLinks.size(); i++)
    {
        // New socket is returned from serverConnect. Why do we need to create new at each iteration?
        string srvAddr = "207.123.60.126";
        int socketImg = serverConnect(srvAddr);
        // Create a message.
        ...
        string message = "GET " + relativePart;
                message += " HTTP/1.1
";
        message += "Host: " + hostPart + "
";
        message += "
";
        // Send a message.
        BufferArray tempImgBuffer = sendMessage(sockImg, message, false);
        fstream pFile;
        string name;
        // Form the name.
        ...
        pFile.open(name.c_str(), ios::app | ios::out | ios::in | ios::binary);
        // Write the file contents.
        ...
        pFile.close();
        // Close the socket.
        close(sockImg);
    }
最佳回答

另一边正在关闭连接。这就是HTTP/1.0的工作原理。您可以:

  • Make a different connection for each HTTP GET
  • Use HTTP/1.0 with the unofficial Connection: Keep-Alive
  • Use HTTP/1.1. In HTTP 1.1 all connections are considered persistent unless declared otherwise.

强制性xkcd链接服务器注意范围

Wiki HTTP

The original version of HTTP (HTTP/1.0) was revised in HTTP/1.1. HTTP/1.0 uses a separate connection to the same server for every request-response transaction, while HTTP/1.1 can reuse a connection multiple times

问题回答

HTTP的原始形式(HTTP1.0)实际上是“每个连接一个请求”协议。一旦你得到回复,对方可能已经关闭了连接。一些实现中添加了非官方机制,以支持每个连接的多个请求,但这些机制并没有标准化。

HTTP 1.1扭转了这一局面。默认情况下,所有连接都是“持久的”。

要使用它,您需要在请求行的末尾添加“HTTP/1.1”。而不是获取http://someurl/,执行获取http://someurl/HTTP/1.1。执行此操作时,您还需要确保提供“Host:”标头。

但是,请注意,即使是一些其他兼容的HTTP服务器也可能不支持持久连接。还要注意的是,连接实际上可能会在很小的延迟、一定数量的请求或只是随机之后被丢弃。您必须为此做好准备,并准备好重新连接并在中断时继续发出请求。

另请参阅HTTP 1.1 RFC





相关问题
How to set response filename without forcing "save as" dialog

I am returning a stream in some response setting the appropriate content-type header. The behavior I m looking for is this: If the browser is able to render content of the given content type then it ...

Which Http redirects status code to use?

friendfeed.com uses 302. bit.ly uses 301. I had decided to use 303. Do they behave differently in terms of support by browsers ?

Does HttpWebRequest send 200 OK automatically?

Background: I am implementing Paypal IPN handler. This great article on Paypal states that I am required to send a 200 OK back to Paypal after I read the response. The processing of IPN request is ...

Java HTTPAUTH

我试图把桌面应用程序连接起来,我是同D.icio.us api @ Delicious Alan书写的,简单地向他们提供我的用户名和密码,并请他把书记上写给我......。

Finding out where curl was redirected

I m using curl to make php send an http request to some website somewhere and have set CURLOPT_FOLLOWLOCATION to 1 so that it follows redirects. How then, can I find out where it was eventually ...

热门标签