当前位置: 首页>>代码示例>>C++>>正文


C++ socket::read_some方法代码示例

本文整理汇总了C++中tcp::socket::read_some方法的典型用法代码示例。如果您正苦于以下问题:C++ socket::read_some方法的具体用法?C++ socket::read_some怎么用?C++ socket::read_some使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tcp::socket的用法示例。


在下文中一共展示了socket::read_some方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: delivery

	void delivery()
	{
		std::vector<char>	buf(BUFF_SIZE);
		while(src_sock.available()){
			src_sock.read_some(boost::asio::buffer(buf));
			dest_sock.write_some(boost::asio::buffer(buf));
		}
		while(dest_sock.available()){
			dest_sock.read_some(boost::asio::buffer(buf));
			src_sock.write_some(boost::asio::buffer(buf));
		}
		return;
	}
开发者ID:TheFool0415,项目名称:simple-proxy,代码行数:13,代码来源:proxy.hpp

示例2: recvFile

bool recvFile(tcp::socket &socket, std::string filepath) {
    std::fstream file;
    file.open(filepath, std::ios::out | std::ios::binary);
    if(!file.is_open()) return false;
    size_t file_size = 0;
    boost::asio::read(socket, boost::asio::buffer(&file_size, sizeof(file_size)));
    size_t file_count = 0;
    while(file_count < file_size) {
        
        char buffer[1024];
        memset(buffer, 0, sizeof(buffer));
        boost::system::error_code ecode;
        size_t buffer_size = socket.read_some(boost::asio::buffer(buffer, 1024), ecode);
        if(ecode == boost::asio::error::eof)
            break;
        else if(ecode) throw boost::system::system_error(ecode);
        
        file.write(buffer,buffer_size);
        file_count += buffer_size;
        std::cout << ".";
        
        /*
        boost::array<char, 128> ar;
        boost::system::error_code ecode;
        size_t len = socket.read_some(boost::asio::buffer(ar), ecode);
        if(ecode == boost::asio::error::eof) break;
        else if(ecode) throw boost::system::system_error(ecode);
        file.write(ar.data(), len);
        */
    }
    file.close();
    return true;
}
开发者ID:lostjared,项目名称:cplusplus11.Examples,代码行数:33,代码来源:filesend.cpp

示例3: getResponse

TextRconPacket getResponse(tcp::socket & socket)
{
	boost::array<char, 16384> buf;
	uint8_t responseHeaderBuf[BinaryRconPacketHeader::Size];
	
	size_t len = socket.read_some(boost::asio::buffer(buf));

	for(uint8_t pos = 0; pos < BinaryRconPacketHeader::Size; pos++)
		responseHeaderBuf[pos] = buf[pos];

	BinaryRconPacketHeader binaryRconPacketHeader(responseHeaderBuf);

	if(!binaryRconPacketHeader.isValid())
		throw string("Invalid Binary Packet Header");
	
	uint32_t binaryRconResponsePacketSize = binaryRconPacketHeader.getPacketSize();
	uint32_t binaryRconResponseBodySize = binaryRconResponsePacketSize - BinaryRconPacketHeader::Size;

	uint8_t* responseBodyBuf = new uint8_t[binaryRconResponseBodySize];

	for(uint32_t pos = 0; pos < binaryRconResponseBodySize; pos++)
		responseBodyBuf[pos] = buf[pos + BinaryRconPacketHeader::Size];

	BinaryRconPacket binaryResponse(binaryRconPacketHeader, responseBodyBuf);

	if(!binaryResponse.isValid())
		throw string("Invalid Binary Packet");

	return TextRconPacket(binaryResponse);
}
开发者ID:DeadHunter,项目名称:TuxNet,代码行数:30,代码来源:main.cpp

示例4: receiveMessages

void TcpBase::receiveMessages(tcp::socket& s) {
    mutex::scoped_lock lock(mutex_);
    boost::system::error_code error;
    char data[MAX_LEN];
    size_t length = s.read_some(asio::buffer(data), error);
    if (length == 0 && error == asio::error::would_block) {
        boost::this_thread::sleep(boost::posix_time::milliseconds(1));
        return;
    }
    if (error) {
        throw boost::system::system_error(error);
    }
    data[length] = '\0';
    logTrace(name_ + " TcpServer new message");
    std::string messagesStr(data);
    std::vector<std::string> messages;
    split(messages, messagesStr, is_any_of("\r\n"));
    for_each(messages.begin(), messages.end(), [this](std::string& message) {
        logTrace("RX: " + message);
        inQueue_.push(message);
    });
}
开发者ID:alepez,项目名称:threescanner,代码行数:22,代码来源:TcpBase.cpp

示例5: session

void session(tcp::socket sock)
{
  try
  {
    for (;;)
    {
      char data[max_length];

      asio::error_code error;
      size_t length = sock.read_some(asio::buffer(data), error);
      if (error == asio::error::eof)
        break; // Connection closed cleanly by peer.
      else if (error)
        throw asio::system_error(error); // Some other error.

      asio::write(sock, asio::buffer(data, length));
    }
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception in thread: " << e.what() << "\n";
  }
}
开发者ID:Dagarman,项目名称:mame,代码行数:23,代码来源:blocking_tcp_echo_server.cpp

示例6: session_thread

void session_thread(tcp::socket sock) {
    std::stringstream threadName;
    threadName << sock.remote_endpoint();
    loguru::set_thread_name(threadName.str().c_str());
    auto db = Storage::Mongo::MongoBackend{
        "mongodb://localhost",
        "directory",
        "rootdn",
        "dc=mongodb,dc=com"
    };

    bool noAuthentication = false;
    // Put this into its own scope so the YAML nodes get cleaned up.
    {
        auto check = config["noAuthentication"];
        if (check && check.as<bool>() == true)
            noAuthentication = true;
    }

    bool userBound = false;
    std::string userBoundDN;

    for (;;)
    {
        std::vector<uint8_t> header(2);;
        size_t length;
        asio::error_code error;

        length = sock.read_some(asio::buffer(header), error);
        if (error) {
            if (error == asio::error::eof)
                break;
            else
                throw asio::system_error(error);
        }
        if (length != header.size()) {
            LOG_F(ERROR, "Client sent malformed BER header");
            break;
        }

        std::vector<uint8_t> reqBuffer;
        reqBuffer.reserve(1024);
        if ((header[1] & 128) != 0) {
            header[1] -= 128;
            reqBuffer.resize(header[1]);
            length = sock.read_some(asio::buffer(reqBuffer), error);
            if (length != reqBuffer.size()) {
                LOG_F(ERROR, "Client sent mal-formed BER size header");
                break;
            }
            auto decodedReqSize = Ber::decodeInteger(reqBuffer.cbegin(), reqBuffer.cend());
            reqBuffer.resize(decodedReqSize, 0);
        } else {
            reqBuffer.resize(header[1], 0);
        }
        length = sock.read_some(asio::buffer(reqBuffer), error);
        if (length != reqBuffer.size()) {
            LOG_F(ERROR, "Client sent fewer bytes than expected");
            break;
        }

        auto ber = Ber::Packet::decode(header[0], reqBuffer);
        auto messageId = static_cast<uint64_t>(ber.children[0]);
        auto messageType = Ldap::MessageTag { static_cast<Ldap::MessageTag>(ber.children[1].tag) };

        Ldap::MessageTag errorResponseType;
        switch (messageType) {
        case Ldap::MessageTag::SearchRequest:
            errorResponseType = Ldap::MessageTag::SearchResDone;
            break;
        default:
            errorResponseType = static_cast<Ldap::MessageTag>(static_cast<uint8_t>(messageType) + 1);
            break;
        }
        try {
            if (messageType == Ldap::MessageTag::BindRequest) {
                Ldap::Bind::Request bindReq(ber.children[1]);
                if (bindReq.type == Ldap::Bind::Request::Type::Sasl) {
                // TODO SUPPORT SASL BINDS!
                    throw Ldap::Exception(Ldap::ErrorCode::authMethodNotSupported);
                }

                bool passOkay = false;
                if (noAuthentication) {
                    LOG_S(INFO)
                        << "Authentication is disabled, sending bogus bind for "
                        << bindReq.dn;
                    passOkay = true;
                } else {
                    LOG_S(INFO) << "Authenticating " << bindReq.dn;
                    try {
                        auto entry = db.findEntry(bindReq.dn);
                        for (const auto& pass: entry->attributes.at("userPassword")) {
                            passOkay = Password::checkPassword(bindReq.simple, pass);
                            if (passOkay)
                                break;
                        }

                    } catch (Ldap::Exception e) {
                        LOG_S(ERROR) << "Error during authentication " << e.what();
//.........这里部分代码省略.........
开发者ID:jbreams,项目名称:nfldap,代码行数:101,代码来源:main.cpp


注:本文中的tcp::socket::read_some方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。