問題描述
我的客戶端應用程序使用 boost::asio::ip::tcp::socket
連接到遠程服務器.如果應用失去與該服務器的連接(例如,由于服務器崩潰或關閉),我希望它定期嘗試重新連接,直到成功.
My client application uses a boost::asio::ip::tcp::socket
to connect to a remote server.
If the app loses connection to this server (e.g. due to the server crashing or being shutdown) I would like it to attempt a re-connect at regular intervals until it succeeds.
我需要在客戶端做什么才能干凈地處理斷開連接、整理然后反復嘗試重新連接?
目前我的代碼中有趣的部分看起來像這樣.
Currently the interesting bits of my code look something like this.
我connect
是這樣的:
bool MyClient::myconnect()
{
bool isConnected = false;
// Attempt connection
socket.connect(server_endpoint, errorcode);
if (errorcode)
{
cerr << "Connection failed: " << errorcode.message() << endl;
mydisconnect();
}
else
{
isConnected = true;
// Connected so setup async read for an incoming message.
startReadMessage();
// And start the io_service_thread
io_service_thread = new boost::thread(
boost::bind(&MyClient::runIOService, this, boost::ref(io_service)));
}
return (isConnected)
}
runIOServer()
方法只是:
void MyClient::runIOService(boost::asio::io_service& io_service)
{
size_t executedCount = io_service.run();
cout << "io_service: " << executedCount << " handlers executed." << endl;
io_service.reset();
}
如果任何異步讀取處理程序返回錯誤,則它們只需調用此 disconnect
方法:
And if any of the async read handlers return an error then they just call this disconnect
method:
void MyClient::mydisconnect(void)
{
boost::system::error_code errorcode;
if (socket.is_open())
{
// Boost documentation recommends calling shutdown first
// for "graceful" closing of socket.
socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, errorcode);
if (errorcode)
{
cerr << "socket.shutdown error: " << errorcode.message() << endl;
}
socket.close(errorcode);
if (errorcode)
{
cerr << "socket.close error: " << errorcode.message() << endl;
}
// Notify the observer we have disconnected
myObserver->disconnected();
}
..它嘗試正常斷開連接,然后通知觀察者,觀察者將開始每隔五秒調用 connect()
直到重新連接.
..which attempts to gracefully disconnect and then notifies an observer, which will start calling connect()
at five second intervals until it gets reconnected.
還有什么我需要做的嗎?
目前這似乎有效.如果我殺死它所連接的服務器,我會在讀取處理程序中收到預期的 "End of file"
錯誤,并且 mydisconnect()
被調用而沒有任何問題.
Currently this does seem to work. If I kill the server that it is connected to I get the expected "End of file"
error at my read handlers and mydisconnect()
is called without any issues.
但是當它嘗試重新連接并失敗時,我看到它報告socket.shutdown error: Invalid argument"
.這僅僅是因為我試圖關閉一個沒有掛起讀/寫的套接字嗎?或者還有什么?
But when it then attempts to re-connect and fails I see it report "socket.shutdown error: Invalid argument"
. Is this just because I am attempting to shutdown a socket that has no read/writes pending on it? Or is it something more?
推薦答案
每次重新連接時都需要創(chuàng)建一個新的 boost::asio::ip::tcp::socket
.最簡單的方法可能是使用 boost::shared_ptr
在堆上分配套接字(如果你的套接字完全封裝在一個類中).例如:
You need to create a new boost::asio::ip::tcp::socket
each time you reconnect. The easiest way to do this is probably to just allocate the socket on the heap using a boost::shared_ptr
(you could probably also get away with scoped_ptr
if your socket is entirely encapsulated within a class). E.g.:
bool MyClient::myconnect()
{
bool isConnected = false;
// Attempt connection
// socket is of type boost::shared_ptr<boost::asio::ip::tcp::socket>
socket.reset(new boost::asio::ip::tcp::socket(...));
socket->connect(server_endpoint, errorcode);
// ...
}
然后,當 mydisconnect
被調用時,你可以釋放套接字:
Then, when mydisconnect
is called, you could deallocate the socket:
void MyClient::mydisconnect(void)
{
// ...
// deallocate socket. will close any open descriptors
socket.reset();
}
您看到的錯誤可能是操作系統(tǒng)在您調用 close
后清理文件描述符的結果.當您調用 close
然后嘗試在同一個套接字上 connect
時,您可能正在嘗試連接無效的文件描述符.此時,根據(jù)您的邏輯,您應該會看到一條以Connection failed: ..."開頭的錯誤消息,但您隨后調用了 mydisconnect
,這可能是在嘗試調用 shutdown
> 在無效的文件描述符上.惡性循環(huán)!
The error you're seeing is probably a result of the OS cleaning up the file descriptor after you've called close
. When you call close
and then try to connect
on the same socket, you're probably trying to connect an invalid file descriptor. At this point you should see an error message starting with "Connection failed: ..." based on your logic, but you then call mydisconnect
which is probably then attempting to call shutdown
on an invalid file descriptor. Vicious cycle!
這篇關于如何在斷開連接后干凈地重新連接 boost::socket?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!