I am trying to wrap the boost TCP using a new class in c++. Things work like a charm while I call the boost function directly. However I fail to call socket close while the close is wrap in a class function. Please help have a look on the following codes.
class defination:
typedef boost::shared_ptr<tcp::socket> Socket;
class TCPConnector{
public :
bool isConnected;
Socket sock;
string ip;
int port;
TCPConnector(string ip, int port);
void Close();
bool Connect();
};
functions:
TCPConnector::TCPConnector(string ip,int port):ip(ip),port(port)
{
}
void TCPConnector::Close() {
boost::system::error_code error;
if (!isConnected)
return;
isConnected = false;
try {
sock->shutdown(boost::asio::ip::tcp::socket::shutdown_both, error);
cout << "ShutDown" << endl;
if (error)
throw boost::system::system_error(error);
sock->close(error);
if (error)
throw boost::system::system_error(error);
} catch (exception& e) {
cout << "#TCPConnector::Close()#" << e.what() << endl;
}
}
Main Function:
int main(int argc, char* argv[]) {
try {
TCPConnector* conn = new TCPConnector("127.0.0.1",8088);
for (int i = 0; i < 2; i++) {
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query(tcp::v4(), "127.0.0.1", "8088");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
conn->sock.reset(new tcp::socket(io_service));
conn->sock->connect(*endpoint_iterator);
cout << "Connected" << endl;
boost::thread acceptorThread(boost::bind(receive,conn));
sleep(1);
unsigned char msg[8] = { 0, 6, 55, 56, 55, 56, 55, 0 };
boost::system::error_code error;
try {
boost::asio::write(*conn->sock, boost::asio::buffer(msg, 8),
boost::asio::transfer_all(), error);
cout << "Sent" << endl;
if (error)
throw boost::system::system_error(error);
conn->sock->shutdown(boost::asio::ip::tcp::socket::shutdown_both,
error);
if (error)
throw boost::system::system_error(error);
conn->sock->close(error);//close socket directly , the result is ok
//conn->Close();// close by calling functions, it causes problems.
cout << "Closed" << endl;
if (error)
throw boost::system::system_error(error);
io_service.stop();
} catch (std::exception& e) {
std::cerr << "Exception in thread: " << e.what() << "\n";
}
cout << "Sleep" << endl;
sleep(2);
cout << "Wake up" << endl;
}
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
}
return 0;
}
These 2 lines give the different behaviours. I don't know why the second one will cause problem.
conn->sock->close(error);//close socket directly , the result is ok
conn->Close();// close by calling functions, it causes problems.
mutex: Invalid argument was printed on
sock->close(error);
if (error)
throw boost::system::system_error(error);
Is the problem related to shared_ptr? or I missed something important to close the socket?
Thanks for any suggestion.
The problem is that the io_service
should outlive the socket
.
On all but the first iteration of the for
loop, the statement conn->sock.reset(new tcp::socket(io_service));
calls the destructor of the previous iteration's socket. This destructor accesses elements of the previous iteration's io_service
(specifically its mutex) which by that point have themselves been destroyed.
To fix this, you can move the io_service
outside the for
loop, or you can call conn->sock.reset();
at the end of the for
loop in order to invoke the socket
's destructor while the io_service
is still valid.