周围路过的boost ::支持ASIO ::知识产权:: TCP ::插座 [英] Passing around boost::asio::ip::tcp::socket

查看:185
本文介绍了周围路过的boost ::支持ASIO ::知识产权:: TCP ::插座的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这是我目前的code片断:

Alright, this is my current code snippet:

namespace bai = boost::asio::ip;
bai::tcp::socket tcp_connect(std::string hostname, std::string port) {
    try {
        boost::asio::io_service io_service;
        bai::tcp::resolver resolver(io_service);

        // we now try to get a list of endpoints to the server 
        bai::tcp::resolver::query query(hostname, port);
        bai::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
        bai::tcp::resolver::iterator end;

        // looking for a successful endpoint connection
        bai::tcp::socket socket(io_service);
        boost::system::error_code error = boost::asio::error::host_not_found;
        while (error && endpoint_iterator != end) {
            socket.close();
            socket.connect(*endpoint_iterator++ , error);
        }

        if (error) throw boost::system::system_error(error);

        return socket;
    } catch (std::exception &ex) {
        std::cout << "Exception: " << ex.what() << "\n";
    }
}

这应该返回一个的boost ::支持ASIO ::知识产权:: TCP ::插座连接到主机名端口。但是我得到INCOM prehensible 的shitload psented $ P $的boost ::不可复制错误。但我的问题是,我应该怎么绕过这些插座呢?这有什么错呢?

Which should return a boost::asio::ip::tcp::socket connected to hostname on port. However I get presented with a shitload of incomprehensible boost::noncopyable errors. But my question is, how should I pass around these sockets then? What's wrong with this?

推荐答案

插座无法复制。使用的boost :: shared_ptr的&LT;李白:: TCP ::插座&GT; 来代替。如果你的可能的复制插座你有各种各样的,如果你结束了与两个有趣的问题插座实例重新presenting相同底层操作系统插座 - 所以它是有道理的复制(因此由返回值,按值传递)是不允许的。

socket can't be copied. Use a boost::shared_ptr<bai::tcp::socket> instead. If you could copy a socket you'd have all sorts of funny issues if you ended up with two socket instances representing the same underlying OS socket - so it makes sense that copying (and therefore return by value, pass by value) is not allowed.

这篇关于周围路过的boost ::支持ASIO ::知识产权:: TCP ::插座的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆