Im using boost asio for implementing TCP sockets. I've followed the chat server example. The first time that I run the socket server it works ok. Im doing it like this:
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);
}
After start it, I call stop method, written like 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;
}
}
And after this, the port is not up (netstat doesnt show). Later, I try to call run again and any error is thrown, port is up (I can see with netstat) but any connections are accepted (async_accept is never waking up).
I think that could be something about the io_service, but if make reset in stop method, and error 125 (operation cancelled) is thrown.
Any idea?
Yes, at the same moment that I call startAccept I'm receiving a new connection but with error 125. It is normal? where can I read something about it?
My 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));
}
And the handle accept:
void ActiveSocketServerConnection::handleAccept(ActiveSocketServerSession* newSession,
const boost::system::error_code& error){
if (!error){
newSession->start();
}else{
activeSocketServer.onException(error);
}