本文整理汇总了C++中tcp::socket::receive方法的典型用法代码示例。如果您正苦于以下问题:C++ socket::receive方法的具体用法?C++ socket::receive怎么用?C++ socket::receive使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tcp::socket
的用法示例。
在下文中一共展示了socket::receive方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: recv_data
std::size_t SampleTestClient::recv_data(tcp::socket& socket, int t_length)
{
ptime t1,t2;
double tdiff=0;
double last_status = 0;
std::size_t h_recvd=0;
std::size_t ret=0;
boost::array<char, DATASIZE> data;
boost::system::error_code error;
t1 = get_pts();
t2 = t1;
h_recvd = 0;
while ( get_diff(t1,t2) < t_length ) {
ret = socket.receive(boost::asio::buffer(data), 0, error);
if ( ret == 0 ) {
if ( error && error != boost::asio::error::eof) {
std::cout << error.message() << std::endl;
}
break;
}
h_recvd += ret;
t2 = get_pts();
tdiff = get_diff(t1, t2);
if ( last_status + 1 < tdiff ) {
status(tdiff, h_recvd);
last_status = tdiff;
}
}
status(tdiff, h_recvd);
return h_recvd;
}
示例2: parse_request
/**
* Parse filepath URL from incoming HTTP request
*/
std::string HttpServer::parse_request(tcp::socket &socket, boost::system::error_code &error_code)
{
std::regex url_path_regex("(\\/([^\\s]+)|\\/)");
std::smatch url_path_match;
std::vector<char> request_buffer(1024);
socket.receive(boost::asio::buffer(request_buffer), {}, error_code);
std::string request_str(request_buffer.data());
std::regex_search(request_str, url_path_match, url_path_regex);
return url_path_match[0];
}
示例3: recv_value
int SampleTestClient::recv_value(tcp::socket& socket, uint32_t *value) {
boost::system::error_code error;
uint32_t n_value=0;
int ret = 0;
ret = socket.receive(boost::asio::buffer(&n_value, sizeof(n_value)), 0, error);
if ( ret != sizeof(uint32_t) ) {
if ( error && error != boost::asio::error::eof) {
std::cout << error.message() << std::endl;
}
return -1;
}
*value = ntohl(n_value);
return OK;
}
示例4: sure_recv
int SampleTestClient::sure_recv(tcp::socket& socket, void *data, std::size_t size) {
boost::system::error_code error;
std::size_t ret = 0;
ret = socket.receive(boost::asio::buffer(data, size), 0, error);
if ( ret != size ) {
if ( error && error != boost::asio::error::eof) {
std::cout << error.message() << std::endl;
}
return -1;
}
return OK;
}
示例5: sizeof
boost::shared_ptr<NetPacket>
receiveMessage(tcp::socket &socket)
{
boost::shared_ptr<NetPacket> tmpPacket;
do {
// This is necessary, because we use TCP.
// Packets may be received in multiple chunks or
// several packets may be received at once.
if (recBufPos >= NET_HEADER_SIZE) {
// Read the size of the packet (first 4 bytes in network byte order).
uint32_t nativeVal;
memcpy(&nativeVal, recBuf.c_array(), sizeof(uint32_t));
size_t packetSize = ntohl(nativeVal);
if (packetSize > MAX_PACKET_SIZE) {
recBufPos = 0;
cout << "Packet too large" << endl;
return boost::shared_ptr<NetPacket>();
} else if (recBufPos >= packetSize + NET_HEADER_SIZE) {
try {
tmpPacket = NetPacket::Create(&recBuf.c_array()[NET_HEADER_SIZE], packetSize);
if (tmpPacket) {
recBufPos -= (packetSize + NET_HEADER_SIZE);
if (recBufPos) {
memmove(recBuf.c_array(), recBuf.c_array() + packetSize + NET_HEADER_SIZE, recBufPos);
}
}
} catch (const exception &) {
// Reset buffer on error.
recBufPos = 0;
cout << "Packet creation failed" << endl;
return boost::shared_ptr<NetPacket>();
}
}
}
if (!tmpPacket) {
recBufPos += socket.receive(boost::asio::buffer(recBuf.c_array() + recBufPos, BUF_SIZE - recBufPos));
if (recBufPos == 0) {
cout << "Receive failed" << endl;
return boost::shared_ptr<NetPacket>();
}
}
} while (!tmpPacket);
return tmpPacket;
}