本文整理汇总了C++中CSocket::Read方法的典型用法代码示例。如果您正苦于以下问题:C++ CSocket::Read方法的具体用法?C++ CSocket::Read怎么用?C++ CSocket::Read使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CSocket
的用法示例。
在下文中一共展示了CSocket::Read方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: s_ReadSocket
static void s_ReadSocket(CSocket& sock, void* buffer,
size_t buffer_size, size_t* bytes_read)
{
EIO_Status status;
while ((status = sock.Read(buffer,
buffer_size, bytes_read)) == eIO_Interrupt)
/* no-op */;
if (status != eIO_Success) {
// eIO_Timeout, eIO_Closed, eIO_InvalidArg,
// eIO_NotSupported, eIO_Unknown
NCBI_THROW_FMT(CNetStorageException, eIOError,
"I/O error while reading from NetStorage server " <<
sock.GetPeerAddress() << ". "
"Socket status: " << IO_StatusStr(status) << '.');
}
}
示例2: ExecuteHTTPGet
CString ExecuteHTTPGet (const CString &sInput)
{
// Parse the input
char *pPos = sInput.GetParsePointer() + STR_HTTP_GET_PREFIX.GetLength();
// Parse the URL
CString sHost;
CString sPath;
if (!urlParse(pPos, NULL, &sHost, &sPath))
return CString("Invalid URL.");
// If no host, then local host
if (sHost.IsEmpty())
sHost = CString("localhost");
// Connect
CSocket theSocket;
if (!theSocket.Connect(sHost, 80))
return strPattern("Unable to connect to: %s.", sHost);
// Compose a request
CHTTPMessage Request;
Request.InitRequest(CString("GET"), sPath);
Request.AddHeader(HEADER_HOST, sHost);
Request.AddHeader(HEADER_CONNECTION, CString("keep-alive"));
#ifdef DEBUG_REQUEST_FRAGMENT_X
Request.AddHeader(HEADER_USER_AGENT, CString("AI1/1.0 (This is a test of the header parsing system in Hexarc. There is probably a bug in which splitting the header across packets will cause failure of the HTTP parsing engine.)"));
#else
Request.AddHeader(HEADER_USER_AGENT, CString("AI1/1.0"));
#endif
// Send the request
CBuffer Buffer(4096);
Request.WriteToBuffer(Buffer);
#ifdef DEBUG_REQUEST_FRAGMENT
int iTotalLen = Buffer.GetLength();
int iSplit = 105;
if (iSplit < iTotalLen)
{
printf("Split at %d bytes\n", iSplit);
CString sPart(Buffer.GetPointer(), iSplit);
printf("%s\n", (LPSTR)sPart);
theSocket.Write(Buffer.GetPointer(), iSplit);
::Sleep(10);
theSocket.Write(Buffer.GetPointer() + iSplit, iTotalLen - iSplit);
}
else
theSocket.Write(Buffer.GetPointer(), Buffer.GetLength());
#else
theSocket.Write(Buffer.GetPointer(), Buffer.GetLength());
#endif
// Now read the response. We build up a buffer to hold it.
CHTTPMessage Response;
CBuffer ResponseBuff;
// Keep reading until we've got enough (or until the connection drops)
while (!Response.IsMessageComplete())
{
CBuffer TempBuffer(8192);
// Read
int iBytesRead = theSocket.Read(TempBuffer.GetPointer(), 8192);
TempBuffer.SetLength(iBytesRead);
// If we're no making progress, then we're done
if (iBytesRead == 0)
return strPattern("Unable to read entire message.");
// Add to entire buffer
ResponseBuff.Write(TempBuffer.GetPointer(), iBytesRead);
// Parse to see if we're done
if (!Response.InitFromPartialBuffer(TempBuffer))
return strPattern("Unable to parse HTTP message.");
}
// Done
theSocket.Disconnect();
return CString(ResponseBuff.GetPointer(), ResponseBuff.GetLength());
}