boostread_some函數(shù)歷程

read_some一旦有遇到數(shù)據(jù)發(fā)送過來,就會立刻返回,但是怎么知道數(shù)據(jù)是否已經(jīng)發(fā)送結(jié)束

成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),隰縣企業(yè)網(wǎng)站建設(shè),隰縣品牌網(wǎng)站建設(shè),網(wǎng)站定制,隰縣網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,隰縣網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。

目前的情況下,是繼續(xù)等待接收,直到遇到協(xié)商的結(jié)束符號.如果read_some返回數(shù)據(jù)是0,代表對端已經(jīng)關(guān)閉了

void ReadSomeFunc()

{

boost::system::error_code ec;

do

{

char szRecvBuf[10240] = { 0 };

int nReadLen = m_socket.read_some(boost::asio::buffer(szRecvBuf), ec);

if (0 == nReadLen)

{

char szRecvBufLeft[10240] = { 0 };

nReadLen = m_socket.read_some(boost::asio::buffer(szRecvBufLeft), ec);

if (0 == nReadLen) return;


m_strMatch = m_strMatch + szRecvBufLeft;

}

if (ec) return;


m_strMatch = szRecvBuf;

int nIndexOfContentLength = m_strMatch.find("Content-Length:", 0);

int indexOfEnd = m_strMatch.find("\r\n\r\n", 0);

if (nIndexOfContentLength == -1) break;


if (-1 == indexOfEnd) break;


std::string strContextLen = m_strMatch.substr(nIndexOfContentLength + 15, indexOfEnd - nIndexOfContentLength - 15);

unsigned int nContextLen = atoi(strContextLen.c_str());

if (nContextLen > m_strMatch.length())

{

char szRecvBufLeft[40960] = { 0 };

m_socket.read_some(boost::asio::buffer(szRecvBufLeft), ec);

m_strMatch = m_strMatch + szRecvBufLeft;

}


boost::asio::streambuf request;

std::ostream request_stream(&request);

request_stream << "HTTP/1.1 200 OK\r\n";

request_stream << "\r\n";

boost::asio::write(m_socket, request, ec);

} while (0);

}

1)boost asio 接收數(shù)據(jù)異常 $/x1

說明

    在發(fā)送PLAY指令之后,接收到的數(shù)據(jù)是$/x1,實際上通過調(diào)試服務(wù)器端,發(fā)現(xiàn)服務(wù)器端實際上已經(jīng)了200 OK過來,因此猜測是接收超時,但是在前面的指令收發(fā)都沒有問題,嘗試在PLAY指令發(fā)送之后,接收之前調(diào)用Sleep函數(shù)睡眠500ms,沒有任何的效果,查看如何設(shè)置socket超時,也沒有相關(guān)資料,使用的都是同步的收發(fā)

測試代碼

#include <iostream>

#include <fstream>

#include <string>

#include <sstream>

#include <boost/asio.hpp>

using namespace std;

using namespace boost::asio;

const char pszRtspServerIP[32] = "192.168.1.140";

short sRtspServerPort = 554;

std::string strFileName = "/smoke.264";

std::string strSessionId;

void WriteFile(char* buf)

{

ofstream ofs;

ofs.open("rtspoption.txt");

ofs << buf << endl;

ofs.close();

}

int ExtractSessionId(const char* pBuffer, int nStartSearchPos = 0)

{

std::string strContext = pBuffer;

const char* pszSession = "Session: ";

int nSessionStringLen = strlen(pszSession);

int nIndexSession = strContext.find(pszSession, nStartSearchPos);

if (-1 == nIndexSession) return -1;

int nIndexSemicolonAfterSession = strContext.find(";", nIndexSession);

if (-1 == nIndexSemicolonAfterSession) return -1;

strSessionId = strContext.substr(nIndexSession + nSessionStringLen, nIndexSemicolonAfterSession - nIndexSession - nSessionStringLen);

return nIndexSemicolonAfterSession;

}

int HandleOptionCommand(ip::tcp::socket &sock)

{

boost::system::error_code ec;

boost::asio::streambuf request;

std::ostream request_stream(&request);

request_stream << "OPTIONS " << "rtsp://" << pszRtspServerIP << " RTSP/1.0\r\n";

request_stream << "CSeq: " << "2\r\n";

request_stream << "User-Agent: " << "LibVLC/2.1.5 (Live555 Streaming Media v2014.0)\r\n\r\n";

boost::asio::write(sock, request);

char buf[1024] = { 0 };

size_t len = sock.read_some(buffer(buf), ec);

return 0;

}

int HanleDescribeCommand(ip::tcp::socket &sock)

{

boost::system::error_code ec;

boost::asio::streambuf request;

std::ostream request_stream(&request);

request_stream << "DESCRIBE " << "rtsp://" << pszRtspServerIP << strFileName << " RTSP/1.0\r\n";

request_stream << "CSeq: " << "3\r\n";

request_stream << "Accept: " << "application/sdp\r\n";

request_stream << "User-Agent: " << "LibVLC/2.1.5 (Live555 Streaming Media v2014.0)\r\n\r\n";

boost::asio::write(sock, request);

char buf[1024] = { 0 };

size_t len = sock.read_some(buffer(buf), ec);

return 0;

}

int HandleSetupCommand(ip::tcp::socket &sock)

{

boost::system::error_code ec;

boost::asio::streambuf request;

std::ostream request_stream(&request);

request_stream << "SETUP " << "rtsp://" << pszRtspServerIP << strFileName << " RTSP/1.0\r\n";

request_stream << "CSeq: " << "4\r\n";

request_stream << "Transport: " << "RTP/AVP/TCP;unicast;interleaved=0-1\r\n";

request_stream << "User-Agent: " << "LibVLC/2.1.5 (Live555 Streaming Media v2014.0)\r\n\r\n";

boost::asio::write(sock, request);

char buf[1024] = { 0 };

int id = 0;

size_t len = sock.read_some(buffer(buf), ec);

ExtractSessionId(buf);

return 0;

}

int HanlePlayCommand(ip::tcp::socket &sock)

{

boost::system::error_code ec;

boost::asio::streambuf request;

std::ostream request_stream(&request);

request_stream << "PLAY " << "rtsp://" << pszRtspServerIP << strFileName << " RTSP/1.0\r\n";

request_stream << "CSeq: " << "5\r\n";

request_stream << "Session: " << strSessionId << "\r\n";

request_stream << "Range: " << "npt=0.000-\r\n";

request_stream << "User-Agent: " << "LibVLC/2.1.5 (Live555 Streaming Media v2014.0)\r\n\r\n";

boost::asio::write(sock, request);

char buf[1024] = { 0 };

int id = 0;

::Sleep(500);

size_t len = sock.read_some(buffer(buf), ec);

return 0;

}

int main(int argc, char* argv[])

{

io_service iosev;

ip::tcp::socket socket(iosev);

ip::tcp::endpoint ep(ip::address_v4::from_string(pszRtspServerIP), sRtspServerPort);

boost::system::error_code ec;

socket.connect(ep, ec);

if (ec) return -1;

HandleOptionCommand(socket);

HanleDescribeCommand(socket);

HandleSetupCommand(socket);

HanlePlayCommand(socket);

return 0;

}

標(biāo)題名稱:boostread_some函數(shù)歷程
網(wǎng)頁URL:http://bm7419.com/article44/jdesee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、電子商務(wù)網(wǎng)站營銷、靜態(tài)網(wǎng)站、網(wǎng)站內(nèi)鏈、標(biāo)簽優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管