简体   繁体   English

在Windows中使用Boost asio重新连接套接字

[英]Reconnect a socket with Boost asio in Windows

I'm having trouble when connecting a socket to an endpoint after being connected to another. 将套接字连接到另一个端点之后将套接字连接到端点时遇到麻烦。

This is the situation: 这种情况:

a) The boost::asio::ip::tcp::socket is connected to a remote host (say pop.remote1.com ). a) boost::asio::ip::tcp::socket连接到远程主机(例如pop.remote1.com )。

b) The transmission ends, and the socket is closed: b)传输结束,插座关闭:

 socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, error);
 socket_.close(error);

Then, when trying to connect to another host (say pop.remote2.com ) using the same process that in a) , the proccess returns without error, but the socket remains closed. 然后,当尝试使用与a)中相同的过程连接到另一台主机(例如pop.remote2.com )时 ,该过程将无错误返回,但是套接字保持关闭状态。

Note that when using pop.remote2.com as the first connection, the things run Ok, and the same problem arises if try to connect to pop.remote1.com after closing. 请注意,当使用pop.remote2.com作为第一个连接时,事情运行正常,如果在关闭后尝试连接到pop.remote1.com也会出现相同的问题。

In both situations there are not pending processes in the attached io_service . 在这两种情况下,附加的io_service中都没有待处理的进程。

The questions are: 问题是:

Is that reconnection admissible? 可以重新连接吗?

Is that the supposed correct process? 那是正确的过程吗?

Thanks in advance. 提前致谢。

PD: I tried to open the socket before the reconnection, but the result remains the same. PD:我尝试在重新连接之前打开套接字,但是结果保持不变。 That is, the result is the same if after closing the previous connection with. 也就是说,如果在关闭之前的连接之后,结果是相同的。

 socket_.shutdown(...);
 socket_.close(...);

is used 用来

 socket_.open(...);
 socket_.async_connect( ... );

or just 要不就

 socket_.async_connect( ... );

A final thought: 最后的想法:

After spent some time on the problem, and do some debug with MS Visual Studio, I think that simply that is not possible, at least in Asio v. 1.45.0; 在花了一些时间解决问题并使用MS Visual Studio进行调试之后,我认为至少在Asio v.1.45.0中根本不可能做到这一点。 Windows 32 and VC++. Windows 32和VC ++。

Perhaps the question is that here -at Boost librarys- all people think in and use objects, and if sometime need reconnect, simply delete the apropriate object, and do a new connection... creating a new object! 也许问题在于,在Boost库中,所有人都在思考和使用对象,如果某个时候需要重新连接,只需删除适当的对象,然后进行新的连接……就可以创建一个新的对象!

That was the solution that I do in my application with good results, athought with some extra code. 那是我在应用程序中执行的解决方案,并通过一些额外的代码来取得了很好的效果。

HTH to some else. HTH到其他。

Is that reconnection admissible? 可以重新连接吗?

yes

Is that the supposed correct process? 那是正确的过程吗?

yes and no. 是的,没有。 If you aren't opening the socket for subsequent connections after you close it for the previous one, you'll need to do that. 如果在close前一个插座后没有打开该插座以进行后续连接,则需要这样做。 Ex: 例如:

socket_.open();
socket_.async_connect( ... );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM