如何计算读取/发送到boost :: asio :: ip :: tcp :: iostream的字节数

时间:2019-04-16 13:27:41

标签: c++ boost tcp iostream

我有C ++ TCP客户端服务器应用程序。服务器循环如下所示:

void Server::start() {
    using boost::asio::ip::tcp;
    boost::asio::io_service IOservice;
    tcp::endpoint serverEndpoint(tcp::v4(), 12345);
    tcp::acceptor acceptor(IOservice, serverEndpoint);
    std::cout << "Server started on port 12345 and accepting connections...\n";

    for (;;) {
        tcp::iostream clientStream;
        boost::system::error_code err;
        acceptor.accept(*clientStream.rdbuf(), err);
        if (!err) {
            std::cout << "client connected!" << std::endl;
            std::thread t(&Server::handleNewClient, this, std::move(clientStream));
            t.detach();
        } else {
            std::cerr << "Error " << err << std::endl;
        }
    }
}

void Server::handleNewClient(boost::asio::ip::tcp::iostream &&clientStream) {
// read from/write to clientStream
}

我能够毫无问题地读取和写入clientStream(例如,使用>>和<<操作符),但是我想跟踪通过iostream发送了多少字节?在这种情况下,测量写入/读取数据的正确方法是什么?我似乎找不到一种方法来返回该方向或任何可以帮助我计算大小的方法。我真的很想测量传输数据的整体大小(包括TCP +基础协议标头+数据),或者仅仅是tcp数据的字节。

编辑:为了澄清,我可以使用Wireshark,netstat,tcptop等工具查看流量数据。我要求在C ++中测量此错误的正确方法。

0 个答案:

没有答案