久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 C++ Socket 只接收必要的數據

Receiving only necessary data with C++ Socket(使用 C++ Socket 只接收必要的數據)
本文介紹了使用 C++ Socket 只接收必要的數據的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我只是想獲取帶有標題的頁面的內容...但是對于通過的最后一個信息包來說,我的大小為 1024 的緩沖區似乎太大或太小...我不知道不想得到太多或太少,如果這是有道理的.這是我的代碼.它可以很好地打印出包含所有信息的頁面,但我想確保它是正確的.

I'm just trying to get the contents of a page with their headers...but it seems that my buffer of size 1024 is either too large or too small for the last packet of information coming through...I don't want to get too much or too little, if that makes sense. Here's my code. It's printing out the page just fine with all the information, but I want to ensure that it's correct.

//Build HTTP Get Request
std::stringstream ss;
ss << "GET " << url << " HTTP/1.0
Host: " << strHostName << "

";
std::string req = ss.str();

// Send Request
send(hSocket, req.c_str(), strlen(req.c_str()), 0);

// Read from socket into buffer.
do
{
     nReadAmount = read(hSocket, pBuffer, sizeof pBuffer);
     printf("%s", pBuffer);

}
while(nReadAmount != 0);

推薦答案

讀取 HTTP 回復的正確方法是讀取直到收到完整的 LF 分隔行(某些服務器使用 bare LF 即使官方規范說使用 CRLF),其中包含響應代碼和版本,然后繼續閱讀 LF 分隔的行,即標題,直到遇到一個長度為 0 的行,指示標頭的結尾,然后您必須分析標頭以找出剩余數據的編碼方式,以便您知道讀取它的正確方法并知道它是如何終止的.有幾種不同的可能性,請參閱 RFC 2616 第 4.4 節實際規則.

The correct way to read an HTTP reply is to read until you have received a full LF-delimited line (some servers use bare LF even though the official spec says to use CRLF), which contains the response code and version, then keep reading LF-delimited lines, which are the headers, until you encounter a 0-length line, indicating the end of the headers, then you have to analyze the headers to figure out how the remaining data is encoded so you know the proper way to read it and know how it is terminated. There are several different possibilities, refer to RFC 2616 Section 4.4 for the actual rules.

換句話說,你的代碼需要改用這種結構(偽代碼):

In other words, your code needs to use this kind of structure instead (pseudo code):

// Send Request
send(hSocket, req.c_str(), req.length(), 0);

// Read Response
std::string line = ReadALineFromSocket(hSocket);
int rescode = ExtractResponseCode(line);
std::vector<std::string> headers;
do
{
     line = ReadALineFromSocket(hSocket);
     if (line.length() == 0) break;
     headers.push_back(line);
}
while (true);

if (
    ((rescode / 100) != 1) &&
    (rescode != 204) &&
    (rescode != 304) &&
    (request is not "HEAD")
)
{
    if ((headers has "Transfer-Encoding") && (Transfer-Encoding != "identity"))
    {
        // read chunks until a 0-length chunk is encountered.
        // refer to RFC 2616 Section 3.6 for the format of the chunks...
    }
    else if (headers has "Content-Length")
    {
       // read how many bytes the Content-Length header says...
    }
    else if ((headers has "Content-Type") && (Content-Type == "multipart/byteranges"))
    {
        // read until the terminating MIME boundary specified by Content-Type is encountered...
    }
    else
    {
        // read until the socket is disconnected...
    }
}

這篇關于使用 C++ Socket 只接收必要的數據的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Assertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(斷言失敗(size.width0 amp;amp; size.height0))
Rotate an image in C++ without using OpenCV functions(在 C++ 中旋轉圖像而不使用 OpenCV 函數)
OpenCV: process every frame(OpenCV:處理每一幀)
Why can#39;t I open avi video in openCV?(為什么我不能在 openCV 中打開 avi 視頻?)
OpenCV unable to set up SVM Parameters(OpenCV 無法設置 SVM 參數)
Convert a single color with cvtColor(使用 cvtColor 轉換單一顏色)
主站蜘蛛池模板: 国产精品99久久久久久宅男 | 国产午夜精品视频 | 色视频网站 | 一区二区三区在线播放视频 | 精品一区二区三区在线观看 | 日日射夜夜骑 | 国产91亚洲精品一区二区三区 | 国产一区二区三区四区在线观看 | 黄片毛片免费观看 | 97人澡人人添人人爽欧美 | 一区二区在线免费观看 | 日本精品一区二区三区在线观看 | 黄视频免费 | 九九免费视频 | 少妇精品久久久久久久久久 | 免费国产视频 | 精品综合视频 | 四虎影视免费观看 | 99精品国自产在线观看 | 天天射天天干 | 久久久www成人免费精品 | 国精品一区 | 国产精品久久久久久婷婷天堂 | 久久久精 | 国产91视频一区二区 | 精品国产乱码久久久久久蜜退臀 | 亚洲欧美一区二区三区在线 | 国产精品亚洲综合 | 欧美日韩一本 | 国产激情视频网址 | 高清成人av | 五月婷婷丁香 | 男人的天堂久久 | 精品一区二区三区在线观看 | 国产精品国产三级国产aⅴ中文 | 国产一区二区三区在线免费 | 成人国产免费视频 | 精品久久久久久久久久久久 | 欧美亚洲一区二区三区 | 国产一级免费在线观看 | 日本又色又爽又黄的大片 |