boost asio服务器在调用时挂起来关闭boost :: socket

时间:2013-01-11 07:47:35

标签: c++ sockets boost boost-asio

下面是我使用boost asio的套接字服务器示例代码。

此服务器将在端口10001上等待任何客户端连接。当任何客户端连接时,它将启动线程从该客户端读取并等待另一个客户端。但是当我的客户端断开服务器套接字时,会发生什么情况在my_socket->close()调用中挂起。

如果新客户端尝试连接服务器崩溃。

我正在使用 g ++(Ubuntu 4.4.3-4ubuntu5.1)4.4.3

#include <ctime>
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <sys/socket.h>
#include <unistd.h>
#include <string>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>

using namespace std;
using boost::asio::ip::tcp;

void run(boost::shared_ptr<tcp::socket> my_socket)
{
    while (1)
    {
        char buf[128];
        boost::system::error_code error;

        size_t len = my_socket->read_some(boost::asio::buffer(buf, 128), error);
        std::cout << "len : " << len << std::endl;

        if (error == boost::asio::error::eof)
        {
            cout << "\t(boost::asio::error::eof)" << endl;
            if (my_socket->is_open())
            {
                boost::system::error_code ec;
                cout << "\tSocket closing" << endl;
                my_socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
                cout << "\tShutdown " << ec.message() << endl;
//                cout << "normal close : " << ::close(my_socket->native_handle()) << endl;
                my_socket->close(ec);
                cout << "\tSocket closed" << endl;
            }
            break; // Connection closed cleanly by peer.
        }
        else if (error)
        {
            std::cout << "Exception : " << error.message() << std::endl;
            break;
        }
        else
        {
            for (unsigned int i = 0; i < len; i++)
                printf("%02x ", buf[i] & 0xFF);
            printf("\n");
        }
    }
}

int main()
{
    const int S = 1000;
    vector<boost::shared_ptr<boost::thread> > arr_thr(S);

    try
    {
        for (uint32_t i = 0;; i++)
        {
            boost::asio::io_service io_service;

            tcp::endpoint endpoint(tcp::v6(), 10001);

            boost::shared_ptr<tcp::socket> my_socket(new tcp::socket(io_service));
            tcp::endpoint end_type;

            tcp::acceptor acceptor(io_service, endpoint);

            std::cout << "before accept" << endl;
            acceptor.accept(*my_socket, end_type);

            std::cout << "connected... hdl : " << my_socket->native_handle() << std::endl;

            boost::asio::ip::address addr = end_type.address();
            std::string sClientIp = addr.to_string();

            std::cout << "\tclient IP : " << sClientIp << std::endl;
            arr_thr[i] = boost::shared_ptr<boost::thread>(new boost::thread(&run, my_socket));
        }
    } catch (std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }

    return 0;
}

1 个答案:

答案 0 :(得分:10)

启动run线程后,main中的for循环再次启动,销毁并重新初始化本地io_service变量,套接字上的下一个事件仍会假设旧的io_service对象,但是你的崩溃。

您应该只使用io_service的一个实例。

另外,你应该看一下boost :: asio提供的异步函数,比如async_acceptasync_read,例如参见这个例子:http://www.boost.org/doc/libs/1_52_0/doc/html/boost_asio/example/chat/chat_server.cpp