win :: asio :: acceptor在win7上挂起

时间:2014-03-19 12:16:05

标签: c++ cygwin boost-asio

我已经实现了启动tcp连接的简单boost :: asio程序。 它在linux上运行完美(ubuntu 12.04,boost 1_48,gcc 4.6.4), 但不是Win7(提升1_55,vs2008express)。

接受少量(从3到20)连接后,它会挂断,并且不再接受连接。 是一些窗户保护问题?我关闭了防火墙和防病毒软件。

以下是供参考的代码:

#include <iostream>

#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>

class Session : public boost::enable_shared_from_this<Session>
{
  public:
    Session(boost::asio::io_service &io_) : tcpSocket(io_)
    {
      std::cerr << "session ctor" << std::endl;
    }
    ~Session()
        {
      std::cerr << "session Dtor" << std::endl;
    }
    boost::asio::ip::tcp::socket& getTcpSocket() { return this->tcpSocket; }

private:
    boost::asio::ip::tcp::socket tcpSocket;
};

class Server
{
public:

    static const unsigned int defaultPort = 55550;
    Server();
    void start();

private:

    boost::asio::io_service io;
    boost::asio::ip::tcp::acceptor acceptor;

    void startAccept();
    void handleAccept(boost::shared_ptr<Session> s_,
          const boost::system::error_code& e_);
};

Server::Server()
    : acceptor(io, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), defaultPort))
{
}

void Server::start()
{
    this->startAccept();
    this->io.run();
}

void Server::startAccept()
{
    boost::shared_ptr<Session> s(new Session(io));

    acceptor.async_accept(s->getTcpSocket(), boost::bind(&Server::handleAccept,
                           this, s, boost::asio::placeholders::error));
}

void Server::handleAccept(boost::shared_ptr<Session> s_,
      const boost::system::error_code& e_)
{
    std::cerr << "handleAccept" << std::endl;

    if(e_)
        std::cerr << e_ << std::endl;

    this->startAccept();
}

int main(int, char**)
{
  Server server;
  server.start();
}

编辑:

问题解决了。这是Cygwin的问题。

1 个答案:

答案 0 :(得分:0)

可能是Win7接受者需要重用地址的选项:即SO_REUSEADDR 尝试将服务器构造函数更改为:

Server::Server()
: acceptor(io)
{
  boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), defaultPort);
  acceptor.open(endpoint.protocol());
  acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
  boost::system::error_code ec;
  acceptor.bind(endpoint, ec);

  if(ec)
    // raise an exception or log this error, etc.
}