title: java network programming coordination of messaging
body: <p>I have 2 processes running in different machines which are communicating via TCP sockets.<br>
  Both processes have code that acts both as a server and as a client.<br>
  I.e. <strong>ProcessA</strong> has opened a server socket that binds at portX and <strong>ProcessB</strong> has opened a server socket bind at portY.<br>
  ProcessA opens a client socket to connect with ProcessB and start sending messages as a client
  and receiving responses (over the same tcp connection of course).<br>
  ProcessB once it receives a message and processes it, it sends the response, but also could send a message over the second tcp connection, i.e. where ProcessB has opened a client socket to portX of ProcessA.<br>
  So the flow of messages are over 2 different tcp connections.<br>
  My problem is the following: <strong>Taking as granted that this "architecture" can not change and must stay as is:</strong><br>
  I have the problem that intermittently, the messages send from ProcessB to ProcessA over the tcp connection that ProcessB has opened the client socket, arrive at processA before the messages send as responses from ProcessB to ProcessA over the tcp connection that ProcessA has connected as a client socket.<br>
  I.e. Both flows occur</p>
  
  <pre><code>(1)  
  ProcessA ----&gt;(msg)----&gt; ProcessB(PortY)  (TCP1)
  ProcessB does processing   
  ProcessB(portY)---&gt;(response)-----&gt;ProcessA (TCP1)  
  ProcessB---&gt;(msg)-----&gt;ProcessA(portX)  (TCP2)
  
  (2)  
  ProcessA ----&gt;(msg)----&gt; ProcessB(PortY)  (TCP1)
  ProcessB does processing   
  ProcessB---&gt;(msg)-----&gt;ProcessA(portX)  (TCP2)
  ProcessB(portY)---&gt;(response)-----&gt;ProcessA  (TCP1)
  </code></pre>
  
  <p><strong>EDIT (after ejp request)</strong>
  How can I enforce/make sure that ProcessB does not send a msg over the connection that ProcessB has a client socket open to server portX of ProcessA, before the message send as a reply from server portY of ProcessB arrives at processA? I.e. to have only flow (1) of the above.<br>
  Note that processB is multithreaded and the processing is non-trivial.  </p>
  
  <p><strong>UPDATE:</strong> 
  May be it is my misconception, but when a process sends data over socket, and control is returned to application, this does not mean that the receiving side has received the data.
  So if a process sends data over 2 sockets, is there a race condition by OS?  </p>
  
  <p><strong>UPDATE2</strong><br>
  After answer I got from Vijay Mathew:<br>
  If I did a locking as suggested, is there a guarantee that OS (i.e. IP layer) will send the data in order? I.e. finish one transmission, then send the next? Or I would they be multiplexed and have same issue?</p>
  
  <p>Thanks</p>