未能bind()
套接字到地址不會使底層套接字無效。因此,connect()
操作將繼續使用未綁定的套接字,並推遲到內核綁定到本地端點。
下面是一個完整的例子demonstrating此行爲:
#include <boost/asio.hpp>
#include <boost/bind.hpp>
// This example is not interested in all handlers, so provide a noop function
// that will be passed to bind to meet the handler concept requirements.
void noop() {}
int main()
{
using boost::asio::ip::tcp;
// Create all I/O objects.
boost::asio::io_service io_service;
tcp::acceptor acceptor(io_service, {tcp::v4(), 0});
tcp::socket server(io_service, tcp::v4());
// Open socket1, binding to a random port.
tcp::socket socket1(io_service, {boost::asio::ip::address_v4::any(), 0});
tcp::socket socket2(io_service); // non-open
// Explicitly open client2, which will bind it to the any address.
boost::system::error_code error;
socket2.open(tcp::v4(), error);
assert(!error);
assert(socket2.local_endpoint().port() == 0);
// Attempt to bind socket2 to socket1's address will fail with
// an already in use error, leaving socket2 bound to the any endpoint.
// (e.g. a failed bind does not have side effects on the socket)
socket2.bind(socket1.local_endpoint(), error);
assert(error == boost::asio::error::address_in_use);
assert(socket2.local_endpoint().port() == 0);
// Connect will defer to the kernel to bind the socket.
acceptor.async_accept(server, boost::bind(&noop));
socket2.async_connect(acceptor.local_endpoint(),
[&error](const boost::system::error_code& ec) { error = ec; });
io_service.run();
io_service.reset();
assert(!error);
assert(socket2.local_endpoint().port() != 0);
}
你確實老工藝真的殺了你連接到舊的,而不是新的,其提供錯誤? –
如果這是在linux/os x/freebsd上,你可以試試netstat -na | grep your-port-number,看看這個過程是否還在。 – kometen
感謝您的意見。 @Joachim,我很確定這個過程已經死了。我已經徹底關閉了窗口,並確實打開關閉和關閉和套接字析構函數。如果操作系統以某種方式允許一個新進程控制舊的進程套接字,那將是相當驚人的。看起來很不安全。 kometen,我會給它一個肯定的嘗試 –