0

I'm writing simple http server, firstly let's take a look at this part of code:

static void *connection_handler(void *connection) {
   /* cast the connection */
   Connection *c = (Connection*)connection;
   HttpRequest req;

   char buffer[1024];
   size_t bytes_recv = recv(c->s, buffer, 1024, 0);

   parse_http_request(&req, buffer, bytes_recv);

   printf("Received connection!");
   //printf("%s", buffer);

   /* cleanup */
   krystal_close_socket(c->s);
   free(connection);
   return NULL;
}

this is my connection handler, every connection I push it to the vector of threads and I got strange bug. When printf with received data is commented, all "Received connection" messages show off after the server loop stops (I've set to stop server after 5 connections) but.. when I'm printing received data every connection it works, buffer is printed on every connection. WTF?!

Kacper Czyż
  • 21
  • 2
  • 6
  • 1
    Possible duplicate of [Why does printf not flush after the call unless a newline is in the format string?](https://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin) – DYZ Jan 15 '18 at 01:53

1 Answers1

0

Okay, when I added "\n" and the of the printing message it starts to working, next I've try to fflush(stdout) and still work. So my question now, is printing '\n\ character doing same thing as fflush(stdout)?

Kacper Czyż
  • 21
  • 2
  • 6