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

使用 C++ Socket 只接收必要的數(shù)據(jù)

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

問題描述

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

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

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.

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

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...
    }
}

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

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

相關(guān)文檔推薦

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++ 中旋轉(zhuǎn)圖像而不使用 OpenCV 函數(shù))
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 無法設(shè)置 SVM 參數(shù))
Convert a single color with cvtColor(使用 cvtColor 轉(zhuǎn)換單一顏色)
主站蜘蛛池模板: 久在线视频播放免费视频 | 91激情电影| 成人教育av | 日日射影院 | 日本一区二区高清不卡 | 天天草天天干天天 | 亚洲欧洲日韩 | 99视频免费看 | 国产精品视频免费观看 | 亚洲一二三区在线观看 | 中文字幕亚洲国产 | 日韩一区二区在线观看视频 | 一区二区av在线 | 日本黄色影片在线观看 | 嫩草视频在线免费观看 | 国产精品一区二区视频 | 韩日一区二区 | 日韩中文字幕一区二区 | 久久高清 | 免费v片| 亚洲欧美综合精品久久成人 | 交专区videossex农村 | 久久久久久久久久久久久久久久久久久久 | 永久免费av| 精品久久久久久久人人人人传媒 | 国产精品久久久久av | 久草日韩| 日韩精品视频在线观看一区二区三区 | 韩日一区二区 | 亚洲美女一区二区三区 | 欧美精品一区在线 | www.国产一区 | 97av| 你懂的在线视频播放 | 天天操天天射天天 | 精品一二区 | 人碰人操| 2020亚洲天堂 | 中文字幕在线观看一区 | www.中文字幕| 久久精品国产一区二区电影 |