asio异步服务器不接受连接

时间:2016-10-19 23:12:13

标签: c++ c++11 asynchronous boost-asio

我创建了一个基于Daytime Async TCP Server example的异步服务器,但使用的是C ++ 11而不是boost。这是服务器类:

 class TcpServer {
  public:
    static std::shared_ptr<TcpServer> make(int port);
    ~TcpServer();
    void start();
  protected:
      TcpServer(int port);
      void init(int port);
      void createSession();
      void onConnect(std::vector<std::shared_ptr<TcpSession>>::iterator session_iter, const asio::error_code& error);
      asio::io_service mService;
      std::thread mServiceThread;
      asio::ip::tcp::acceptor mAcceptor;
      asio::ip::tcp::socket mSocket;
      asio::ip::tcp::endpoint mLocalEndpoint;
      std::vector < std::shared_ptr<TcpSession>> mSessions;
  };

std::shared_ptr<TcpServer> TcpServer::make(int port) {
    std::shared_ptr<TcpServer> Server(new TcpServer(port));
    return Server;
}

TcpServer::TcpServer(int port) : mSocket(mService), mAcceptor(mService) {
    init(port);
}

TcpServer::~TcpServer() {
    mSocket.cancel();
    mService.stop();
    if (mServiceThread.joinable()) {
        mServiceThread.join();
    }
}

void TcpServer::start() {
    createSession();
}

void TcpServer::init(int port) {
    mLocalEndpoint = asio::ip::tcp::endpoint(asio::ip::tcp::v4(), port);

    std::printf("TcpServer -- Creating endpoint %s:%d ...\n", mLocalEndpoint.address().to_string().c_str(), mLocalEndpoint.port());

    mAcceptor.open(mLocalEndpoint.protocol());
    mAcceptor.set_option(asio::ip::tcp::acceptor::reuse_address(true));
    mAcceptor.bind(mLocalEndpoint);
    mAcceptor.listen();

    mServiceThread = std::thread([&] {
        mService.run();
    });

}

void TcpServer::createSession() {
    std::printf("TcpServer -- Creating new session...\n");
    std::shared_ptr<TcpSession> newSession = TcpSession::make(mService);
    std::vector<std::shared_ptr<TcpSession>>::iterator iterator = mSessions.insert(mSessions.end(), newSession);

    mAcceptor.async_accept(newSession->getSocket(), 
      [this, iterator](const asio::error_code &error) {
      std::printf("Async Accept\n");
      onConnect(iterator, error);
    });
}

void TcpServer::onConnect(std::vector<std::shared_ptr<TcpSession>>::iterator session_iter, const asio::error_code& error) {
    if (!error) {
        std::printf("TcpServer -- Connection from %s received!\n", (*session_iter)->getSocket().remote_endpoint().address().to_string().c_str());

    // Write a response
    //auto buff = std::make_shared<std::string>("Hello World!\r\n\r\n");

    //asio::async_write((*session_iter)->getSocket(), asio::buffer(*buff), 
    //  [this, session_iter](const asio::error_code &error, std::size_t bytesReceived) {
    //  onWrite(session_iter, error, bytesReceived);
    //});

    // begin listening
    //receive(session_iter);

}
else {
    std::printf("TcpServer::onConnect -- Error receiving data. %s\n", error.message().c_str());
    mSessions.erase(session_iter);
}
}

然后是小TcpSession类:

class TcpSession {
    public:
        ~TcpSession();
        static std::shared_ptr<TcpSession> make(asio::io_service& service){
        asio::ip::tcp::socket& getSocket() {
            return mSocket;
        }
    protected:
        TcpSession(asio::io_service& service);
        asio::ip::tcp::socket mSocket;
    };

我创建了一个服务器实例并按如下方式运行:

void main() {
    mServer = TcpServer::make(8060);
    mServer->start();
}

然后我尝试使用TCP / IP Builder(适用于Windows)等轻量级TCP / IP应用程序连接到它;但是,我从来没有看到来自onConnect的任何消息表明我已经建立了联系。

有没有人对可能出错的地方有任何建议?

谢谢!

2 个答案:

答案 0 :(得分:2)

你的问题是asio :: io_service :: run()由于缺乏工作而立即返回。要使io_service保持运行,您应该创建一个io_service::work对象。

答案 1 :(得分:1)

您的防火墙是否有可能阻止进程/端口?