我使用boost asio實現TCP套接字。我已經按照聊天服務器的例子。我第一次運行套接字服務器,它工作正常。 Im做這樣的:增強asio套接字不能連接後停止
void ActiveSocketServer::run() throw (ActiveException){
initBoost();
server->startAccept();
t=new boost::thread(boost::bind(&boost::asio::io_service::run, &io_service));
}
void ActiveSocketServer::initBoost(){
if (isIpV6){
endpoint=new tcp::endpoint(tcp::v6(), port);
}else{
endpoint=new tcp::endpoint(tcp::v4(), port);
}
server=new ActiveSocketServerConnection(io_service,
*endpoint,
port,
id,
maxSizePacket,
this);
}
啓動它後,我呼籲停止方法,這樣寫的:
void ActiveSocketServer::stop(){
io_service.stop();
if (t){
t->join();
delete t;
t=NULL;
}
if (endpoint){
delete endpoint;
endpoint=NULL;
}
if (server){
delete server;
server=NULL;
}
}
並在此之後,該端口不可達(netstat的犯規顯示)。後來,我嘗試再次調用運行,並且拋出任何錯誤,端口已經啓動(我可以用netstat看到),但是任何連接都被接受(async_accept永遠不會喚醒)。
我認爲這可能是有關io_service,但如果在停止方法,使重置和錯誤125(操作取消)引發。
有什麼想法?
是的,我稱之爲startAccept我收到一個新的連接,但與錯誤125,這是正常的同一時刻?我在哪裏可以讀到關於它的一些信息
我startAccept:
void ActiveSocketServerConnection::startAccept(){
sessionId++;
newSession=new ActiveSocketServerSession( io_service_,
getPort(),
getId(),
maxSizePacket,
activeSocketServer,
sessionId);
acceptor_.async_accept(newSession->socket(),
boost::bind(&ActiveSocketServerConnection::handleAccept,
this, newSession,
boost::asio::placeholders::error));
}
和手柄接受:
void ActiveSocketServerConnection::handleAccept(ActiveSocketServerSession* newSession,
const boost::system::error_code& error){
if (!error){
newSession->start();
}else{
activeSocketServer.onException(error);
}
如果我這樣做,即時獲取錯誤代碼125操作取消)在句柄接受... – opernas
假設你的'StartAccept()'函數接受一個連接,然後立即嘗試接受另一個?然後,這是預期的,並且處理程序正在執行從您以前的調用'stop()'。 – Chad