title: Right way to do TCP connection between python and Qt?
body: <p>I want to connect two programs via TCP. My main program is written with Qt and needs to talk to another program written in Python. I think about using TCP sockets and Google's protobuf to exchange the messages. In Qt, I use a QTcpSocket that accepts the connection and reads from the stream, as soon as its readyRead-Signal is triggered. In python, I also use a tcp-socket and send messages. </p>
  
  <p>This works very well, as long as no side is killed. Currently, the python-side is sending messages to the C++ side. (<code>socket.send(str(id)+"\ņ")</code>) After every send, I check for exceptions (connection reset by peer, broken pipe, ...) to see if the message was received. 
  If I kill the C++ program, the next message send from the python client triggers no exception, but is obviously not received. The next message triggers the exception, but the last message is lost. </p>
  
  <p>After a bit of experimenting, I found that sending an empty message (<code>socket.send("\n")</code>) after each message solves the problem. I do now</p>
  
  <pre><code>try:
    s.send(str(id)+"\n");
    s.send("\n")
    sleep(0.5)
  except socket.error,v: 
     print "FAILed to send",id,v[0],v[1]
  </code></pre>
  
  <p>and receive the exception as soon as the C++-Peer is killed (calling <code>s.send(str(id)+"\n\n")</code> however does not help). </p>
  
  <p>Finally, my question is: Is this a reliable way to check if my message was received? 
  I don't want to switch to UDP as I don't want to implement my own ACK-messages for each message. </p>
  
  <p>This is my first time I use sockets with python and C++ and can't really explain why my approach works, so I'm a bit uncomfortable using it. </p>
  
  <p>Can someone tell me a a bit more? I guess that the python socket expects an ACK for the first <code>send(int(id)+"\n")</code> after sending the <code>send("\n")</code> and then realizes that the pipe is broken. Is this correct?</p>