Anonymous
Anonymous

Reputation: 1560

Boost ASIO Network Timing Issue

I am using boost::asio to implement network programming and running into timing issues. The issue is currently most with the client.

The protocol initially begins by the server returning a date time string to the user, and the client reads it. Up to that part it works fine. But What I also want is to be able to write commands to the server which then processes them. To accomplish this I use the io_service.post() function as shown below.

io_service.post(boost::bind()); // bounded function calls async_write() method. 

For some reason the write tries happens before the initial client/server communication, when the socket has not been created yet. And I get bad socket descriptor error.

Now the io_service's run method is indeed called in another thread.

When I place a sleep(2) command before post method, it work fine.

Is there way to synchronize this, so that the socket is created before any posted calls are executed.

Upvotes: 1

Views: 161

Answers (2)

Mickaël Le Baillif
Mickaël Le Baillif

Reputation: 2157

When creating the socket and establishing the connection using boost::asio, you can define a method to be called when these operations have either completed or failed. So, you should trigger your "posted call" in the success callback.

Relevant methods and classes are :

boost::asio::ip::tcp::resolver::async_resolve(...)
boost::asio::ip::tcp::socket::async_connect(...)

Upvotes: 1

titer1_on_the_way
titer1_on_the_way

Reputation: 9

I think the links below

will give u some help

http://www.boost.org/doc/libs/1_42_0/doc/html/boost_asio/reference/io_service.html

Upvotes: 0

Related Questions