使用shared_ptr c ++来增强tcp套接字

时间:2013-02-28 02:12:19

标签: c++ boost tcp boost-asio shared-ptr

我正在尝试使用c ++中的新类包装boost TCP。当我直接调用boost函数时,事情就像一个魅力。但是,当关闭包装在类函数中时,我无法调用套接字关闭。请帮助看看以下代码。

课程定义:

typedef boost::shared_ptr<tcp::socket> Socket;
class TCPConnector{
public :
    bool isConnected;
    Socket sock;
    string ip;
    int port;
    TCPConnector(string ip, int port);

    void Close();
    bool Connect();
};

功能

TCPConnector::TCPConnector(string ip,int port):ip(ip),port(port)
{

}

void TCPConnector::Close() {
    boost::system::error_code error;
    if (!isConnected)
        return;
    isConnected = false;

    try {

        sock->shutdown(boost::asio::ip::tcp::socket::shutdown_both, error);
        cout << "ShutDown" << endl;
        if (error)
            throw boost::system::system_error(error);
        sock->close(error);
        if (error)
            throw boost::system::system_error(error);
    } catch (exception& e) {
        cout << "#TCPConnector::Close()#" << e.what() << endl;
    }
}

主要功能:

int main(int argc, char* argv[]) {
    try {
        TCPConnector* conn = new TCPConnector("127.0.0.1",8088);

        for (int i = 0; i < 2; i++) {
            boost::asio::io_service io_service;
            tcp::resolver resolver(io_service);
            tcp::resolver::query query(tcp::v4(), "127.0.0.1", "8088");
            tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
            conn->sock.reset(new tcp::socket(io_service));
            conn->sock->connect(*endpoint_iterator);
            cout << "Connected" << endl;
            boost::thread acceptorThread(boost::bind(receive,conn));
            sleep(1);
            unsigned char msg[8] = { 0, 6, 55, 56, 55, 56, 55, 0 };
            boost::system::error_code error;
            try {

                boost::asio::write(*conn->sock, boost::asio::buffer(msg, 8),
                        boost::asio::transfer_all(), error);
                cout << "Sent" << endl;
                if (error)
                    throw boost::system::system_error(error);
                conn->sock->shutdown(boost::asio::ip::tcp::socket::shutdown_both,
                        error);
                if (error)
                    throw boost::system::system_error(error);




                conn->sock->close(error);//close socket directly , the result is ok
                //conn->Close();// close by calling functions, it causes problems.





                cout << "Closed" << endl;
                if (error)
                    throw boost::system::system_error(error);
                io_service.stop();
            } catch (std::exception& e) {
                std::cerr << "Exception in thread: " << e.what() << "\n";
            }
            cout << "Sleep" << endl;
            sleep(2);
            cout << "Wake up" << endl;
        }
    } catch (std::exception& e) {
        std::cerr << e.what() << std::endl;
    }
    return 0;
}

这两行给出了不同的行为。我不知道为什么第二个会引起问题。

conn->sock->close(error);//close socket directly , the result is ok
conn->Close();// close by calling functions, it causes problems.

互斥锁:

上打印了无效参数
sock->close(error);
        if (error)
            throw boost::system::system_error(error);

问题是否与shared_ptr有关?或者我错过了关闭插座的重要事项?

感谢您的任何建议。

1 个答案:

答案 0 :(得分:3)

问题是io_service应该比socket更长。

在除for循环的第一次迭代之外的所有循环中,语句conn->sock.reset(new tcp::socket(io_service));调用前一次迭代的套接字的析构函数。这个析构函数访问前一个迭代的io_service(特别是它的互斥锁)的元素,到那时它们本身就被破坏了。

要解决此问题,您可以将io_service移到for循环之外,也可以在conn->sock.reset();循环结束时调用for以调用<{1}}仍然有效时socket的析构函数。