2

我正在尝试使用 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有关吗?或者我错过了关闭套接字的重要内容?

感谢您的任何建议。

4

1 回答 1

3

问题是io_service应该比socket.

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

要解决此问题,您可以移动循环io_service外部for,或者您可以conn->sock.reset();在循环结束时for调用,以便在仍然有效socket时调用 的析构函数。io_service

于 2013-02-28T09:06:48.190 回答