2

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).

b) The transmission ends, and the socket is closed:

 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.

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.

In both situations there are not pending processes in the attached io_service.

The questions are:

Is that reconnection admissible?

Is that the supposed correct process?

Thanks in advance.

P.D: I tried to open the socket before the reconnection, but the result remains the same. 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; Windows 32 and 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!

That was the solution that I do in my application with good results, athought with some extra code.

HTH to some else.

Community
  • 1
  • 1
Old newbie
  • 811
  • 2
  • 11
  • 21

1 Answers1

0

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. Ex:

socket_.open();
socket_.async_connect( ... );
Sam Miller
  • 23,808
  • 4
  • 67
  • 87
  • Thank very much for your answer. To reconnect I use some lilke: `socket_.async_connect (endpoint, boost::bind(&hndlConnect, this, boost::asio::placeholders::error, ++endpoint_iterator));` That works every first time, and by the way, the doc say: "The socket is automatically opened if it is not already open". Any way, I will try your advice of open before the reconnection and see what happens. – Old newbie Mar 04 '11 at 21:29
  • @Old you are correct, async_connect will implicitly open the socket if it's not already opened. It is not clear to me what problem you are having. Can you update your question to clarify it? For example, what error code to you get in your connect handler? – Sam Miller Mar 06 '11 at 21:23