目前,我正在使用Boost Asio通過TCP連接到服務器。如何避免程序退出後連接拒絕與升壓Asio和C/C++
我使用一個條件案例來決定應用程序是否必須啓動或不與服務器建立連接;它的偉大工程,但問題是,如果我嘗試連接到服務器時,服務器關閉,然後應用程序崩潰給這個錯誤:
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >'
what(): Connection refused
這是我使用的代碼:
case CONNECTION:
// Connect to the server
using boost::asio::ip::tcp;
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query(tcp::v4(), server, boost::lexical_cast<string>(porta));
tcp::resolver::iterator iterator = resolver.resolve(query);
tcp::socket s(io_service);
s.connect(*iterator);
我想保持活動我的應用程序的正常行爲,只提示有關連接失敗的警告。
我該如何處理這個異常?
+1提到兩種方式來處理錯誤。 – 2011-06-06 13:05:27