如何查明通过 Boost ASIO TCP 套接字连接发送了多少数据?

发布于 2024-11-30 02:43:00 字数 310 浏览 1 评论 0原文

我有一些指向 boost::asio::io_serviceboost::asio::ip::tcp::endpointboost::asio: 的共享指针:ip::tcp::acceptorboost::asio::ip::tcp::socket。我接受用户连接并将套接字的shared_ptr传递给我的某个类。它发挥作用。

现在我想要的很简单——统计我的流量。我想获取有关该连接期间发送和接收的数据量的信息。如何从接受的 Boost ASIO TCP 套接字获取此类信息?

I have some shared pointers to boost::asio::io_service, boost::asio::ip::tcp::endpoint, boost::asio::ip::tcp::acceptor and boost::asio::ip::tcp::socket. I accept users connection and pass shared_ptr of the socket to some class of mine. It does its work.

Now what I want is simple - count my traffic. I want to get information of how much data was sent and received during that connection. How to get such information from accepted Boost ASIO TCP socket?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

思念满溢 2024-12-07 02:43:00

假设您使用异步方法,为 async_read 提供的完成处理程序将指示接收到的字节数。类似地,提供给async_write的完成处理程序将指示写入的字节数。将正在运行的计数器维护为类的成员是很简单的,在该类中将方法绑定为前面描述的完成处理程序。

#include <boost/asio.hpp>

#include <iostream>

class Socket
{
public:
    Socket(
            boost::asio::io_service& io_service
          ) :
        _socket( io_service ),
        _counter( 0 )
    {

    }

    void readHandler(
            const boost::system::error_code& error,
            std::size_t bytes_transferred
            )
    {
        _counter += bytes_transferred;
    }

    void writeHHandler(
            const boost::system::error_code& error,
            std::size_t bytes_transferred
            )
    {
        _counter += bytes_transferred;
    }

private:
    boost::asio::ip::tcp::socket _socket;
    std::size_t _counter;
};

int
main()
{
    boost::asio::io_service io_service;
    Socket foo( io_service );
}

Assuming you use asynchronous methods, the completion handler given to async_read will indicate the number of bytes received. Similarly, the completion handler given to async_write will indicate the number of bytes written. It would be trivial to maintain a running counter as a member of a class where you bind methods as the previously described completion handlers.

#include <boost/asio.hpp>

#include <iostream>

class Socket
{
public:
    Socket(
            boost::asio::io_service& io_service
          ) :
        _socket( io_service ),
        _counter( 0 )
    {

    }

    void readHandler(
            const boost::system::error_code& error,
            std::size_t bytes_transferred
            )
    {
        _counter += bytes_transferred;
    }

    void writeHHandler(
            const boost::system::error_code& error,
            std::size_t bytes_transferred
            )
    {
        _counter += bytes_transferred;
    }

private:
    boost::asio::ip::tcp::socket _socket;
    std::size_t _counter;
};

int
main()
{
    boost::asio::io_service io_service;
    Socket foo( io_service );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文