1
我已經實現了簡單的boost :: asio程序來啓動tcp連接。 它工作完美的Linux(Ubuntu的12.04,升壓1_48,海灣合作委員會4.6.4), 但不是在Win7(升壓1_55,vs2008express)。boost :: asio :: acceptor在win7掛斷
接受幾個(從3到20)連接後,它掛斷了,不再接受連接。 它是一些Windows保護問題嗎?我關閉防火牆和防病毒。
下面是參考代碼:
#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的問題。