title: Program structure for bi-directional TCP communication using Boost::Asio
body: <p>First off, I hope my question makes sense and is even possible! From what I've read about TCP sockets and Boost::ASIO, I think it should be. </p>
  
  <p>What I'm trying to do is to set up two machines and have a working bi-directional read/write link over TCP between them. Either party should be able to send some data to be used by the other party. </p>
  
  <p>The first confusing part about TCP(/IP?) is that it requires this client/server model. However, reading shows that either side is capable of writing or reading, so I'm not yet completely discouraged. I don't mind establishing an arbitrary party as the client and the other as the server. In my application, that can be negotiated ahead of time and is not of concern to me.</p>
  
  <p>Unfortunately, all of the examples I come across seem to focus on a client connecting to a server, and the server immediately sending some bit of data back. But I want the client to be able to write to the server also.</p>
  
  <p>I envision some kind of loop wherein I call <code>io_service.poll()</code>. If the polling shows that the other party is waiting to send some data, it will call <code>read()</code> and accept that data. If there's nothing waiting in the queue, and it has data to send, then it will call <code>write()</code>. With both sides doing this, they should be able to both read and write to each other. </p>
  
  <p>My concern is how to avoid situations in which both enter into some synchronous write() operation at the same time. They both have data to send, and then sit there waiting to send it on both sides. Does that problem just imply that I should only do asynchronous <code>write()</code> and <code>read()</code>? In that case, will things blow up if both sides of a connection try to write asynchronously at the same time?</p>
  
  <p>I'm hoping somebody can ideally:</p>
  
  <p>1) Provide a very high-level structure or <em>best practice</em> approach which could accomplish this task from both client and server perspectives</p>
  
  <p>or, somewhat less ideally,</p>
  
  <p>2) Say that what I'm trying to do is impossible and perhaps suggest a workaround of some kind.</p>