title: C program hanging while getting response from Flask server
body: <p>EDIT After some more reading, the broken pipe error just means that I terminated the connection. But the question remains, why is the <code>read</code> request hanging? </p>
  
  <p>I'm very new to this so please bear with me.</p>
  
  <p>I've got a tiny webapp in flask running on localhost which looks like this:</p>
  
  <pre><code>import stuff
  app = Flask(__name__)
  ...do some stuff
  @app.route("/")
  def foo():
  
      resp_OK = Response(response="", status=200)
      resp_NO = Response(response="", status=500)
  
      ...do some stuff
  
      if some stuff:
          return resp_NO
      else:
          return resp_OK
  
  if __name__== '__main__':
      app.run()
  </code></pre>
  
  <p>if I just type the <code>localhost:5000/?param1=therightparamts&amp;param2=tomakeitwork</code>, everything works fine, the command line response is either 200 or 500 accordingly.</p>
  
  <p>Now, I'm trying to get this response from inside C, the code for which I borrowed from Jeremy Jeremiah's response <a href="https://stackoverflow.com/questions/22077802/simple-c-example-of-doing-an-http-post-and-consuming-the-response">here</a>. As far as I can tell, it opens the socket and sends the request, and then it fails to read the response. I just want to know if the webapp returned 200 or 500. Here's the relevant bit of C code:</p>
  
  <pre><code>char response[4096];
  int total, received, bytes;
  ...
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  ...
  if (connect(sockfd,(struct sockaddr *)&amp;serv_addr,sizeof(serv_addr)) &lt; 0)
      error("ERROR connecting");
  ...
  do {
      bytes = write(sockfd,&amp;(message.c[sent]),total-sent);
      ...}while(...);
  ...
  memset(response,0,sizeof(response));
  total = sizeof(response)-1;
  received = 0;
  do {
      bytes = read(sockfd,response+received,total-received);
      printf("Read %i bytes.\n", received); fflush(stdout);
      if (bytes &lt; 0)
          error("ERROR reading response from socket");
      if (bytes == 0)
          break;
      received+=bytes;
  } while (received &lt; total);
  </code></pre>
  
  <p>It never gets to the printf after <code>read(sockfd...)</code>. If I terminate the C program while it's hanging, the console output is</p>
  
  <pre><code>127.0.0.1 - - [27/Dec/2015 14:25:33] "GET /?file=foo&amp;signature=46b4ec586117154dacd49d664e5d63fdc88efb51 HTTP/1.1" 500 -
  ----------------------------------------
  Exception happened during processing of request from ('127.0.0.1', 2363)
  Traceback (most recent call last):
    File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
      self.process_request(request, client_address)
    File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
      self.finish_request(request, client_address)
    File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
      self.RequestHandlerClass(request, client_address, self)
    File "/usr/lib/python2.7/SocketServer.py", line 657, in __init__
      self.finish()
    File "/usr/lib/python2.7/SocketServer.py", line 716, in finish
      self.wfile.close()
    File "/usr/lib/python2.7/socket.py", line 283, in close
      self.flush()
    File "/usr/lib/python2.7/socket.py", line 307, in flush
      self._sock.sendall(view[write_offset:write_offset+buffer_size])
  error: [Errno 32] Broken pipe
  ----------------------------------------
  </code></pre>
  
  <p>If I keyboard interrupt the webapp while the C program is hanging, the response is as follows:</p>
  
  <pre><code>Exception happened during processing of request from ('127.0.0.1', 3760)
  Traceback (most recent call last):
    File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
      self.process_request(request, client_address)
    File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
      self.finish_request(request, client_address)
    File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
      self.RequestHandlerClass(request, client_address, self)
    File "/usr/lib/python2.7/SocketServer.py", line 655, in __init__
      self.handle()
    File "/home/gmoss/.local/lib/python2.7/site-packages/werkzeug/serving.py", line 217, in handle
      rv = BaseHTTPRequestHandler.handle(self)
    File "/usr/lib/python2.7/BaseHTTPServer.py", line 340, in handle
      self.handle_one_request()
    File "/home/gmoss/.local/lib/python2.7/site-packages/werkzeug/serving.py", line 251, in handle_one_request
      elif self.parse_request():
    File "/usr/lib/python2.7/BaseHTTPServer.py", line 291, in parse_request
      self.headers = self.MessageClass(self.rfile, 0)
    File "/usr/lib/python2.7/mimetools.py", line 25, in __init__
      rfc822.Message.__init__(self, fp, seekable)
    File "/usr/lib/python2.7/rfc822.py", line 108, in __init__
      self.readheaders()
    File "/usr/lib/python2.7/rfc822.py", line 155, in readheaders
      line = self.fp.readline()
    File "/usr/lib/python2.7/socket.py", line 451, in readline
      data = self._sock.recv(self._rbufsize)
  KeyboardInterrupt
  ----------------------------------------
  </code></pre>
  
  <p>I looked at some of the other answers on SO but don't quite understand how to apply those answers to my situation (as I said, new to this). Would appreciate any pointers in the right direction.</p>