
Hi, it is possible obtain a Socket (boost::asio::ip::tcp::socket) from an acceptor object? This is my problem: I use the accept() method of acceptor for wait on a connection. But, in java, the method accept() of ServerSocket class return a socket object and I can pass this reference to a thread that handles it and I can continue to accept() in main program. It is possible obtain this behavior with asio? -- View this message in context: http://boost.2283326.n4.nabble.com/Boost-asio-question-tp3255518p3255518.htm... Sent from the Boost - Users mailing list archive at Nabble.com.

On Thu, Feb 3, 2011 at 3:39 AM, Claude
Hi, it is possible obtain a Socket (boost::asio::ip::tcp::socket) from an acceptor object?
This is my problem: I use the accept() method of acceptor for wait on a connection.
But, in java, the method accept() of ServerSocket class return a socket object and I can pass this reference to a thread that handles it and I can continue to accept() in main program.
It is possible obtain this behavior with asio?
In Asio, it's the reverse -- you pass it an appropriately constructed socket to the call to acceptor::accept(...). Look at the examples for more information on how to do it the Boost.Asio and C++ way. The documentation for Boost.Asio is here: http://www.boost.org/libs/asio PS. The Boost.Asio way is very different from the Java way. -- Dean Michael Berris about.me/deanberris

Thank Dean!
I am using this code:
void threadSocket(tcp::socket *socket)
{
string msg = "Hello From Thread!";
boost::system::error_code ignored_error;
boost::asio::write(*socket,
boost::asio::buffer(msg),
boost::asio::transfer_all(),
ignored_error);
std::this_thread::sleep_for(std::chrono::seconds(25));
}
int main()
{
try
{
while(1)
{
boost::asio::io_service io_service;
tcp::acceptor acceptor(io_service,tcp::endpoint(tcp::v4(),3211));
tcp::socket socket(io_service);
cout << "Wait for connection." << std::endl;
acceptor.accept(socket);
std::thread t(bind(threadSocket,&socket));
t.join();
}//endwhile
}
catch(std::exception &e)
{
cout << e.what() <

In my examples, the socket is really a new instace of tcp::socket? It is a different socket? -- View this message in context: http://boost.2283326.n4.nabble.com/Boost-asio-question-tp3255518p3261184.htm... Sent from the Boost - Users mailing list archive at Nabble.com.
participants (2)
-
Claude
-
Dean Michael Berris