title: How do you send a full response to a web browser using C in a simple http server?
body: <p>Please help, I am attempting to write a basic http server in C. I have had success in producing a result but the result is not what I was going for. The browser is only rendering one character and I can't figure out why. Below is my server code as of right now. Anything you can do to help would be most appreciated. You're the best!</p>
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;string.h&gt;
  #define PORT 8080
  
  int clients[1000];
  
  int handle(int socket, int fd, struct sockaddr *pSockaddr, socklen_t *pInt, char *output);
  
  int main(int argc, char const *argv[])
  {
      int server_fd, new_socket, valread;
      struct sockaddr_in address;
      int opt = 1;
      int addrlen = sizeof(address);
      char buffer[1024] = {0};
      char *hello = &quot;Hello from server&quot;;
  
      if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
      {
          perror(&quot;socket failed&quot;);
          exit(EXIT_FAILURE);
      }
  
      if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR,
                     &amp;opt, sizeof(opt)))
      {
          perror(&quot;setsockopt&quot;);
          exit(EXIT_FAILURE);
      }
      address.sin_family = AF_INET;
      address.sin_addr.s_addr = INADDR_ANY;
      address.sin_port = htons( PORT );
  
      if (bind(server_fd, (struct sockaddr *)&amp;address,
              sizeof(address))&lt;0)
      {
          perror(&quot;bind failed&quot;);
          exit(EXIT_FAILURE);
      }
  
      char* httpVersion = &quot;HTTP/1.1 200 OK\r\n&quot;;
      char* contentLength = &quot;Content-Length: 1\r\n&quot;;
      char* server = &quot;Server: n\r\n&quot;;
      char output[30000];
      char* contentType = &quot;Content-Type: text/html&quot;;
      char* newLine = &quot;\r\n&quot;;
      strncat(output, httpVersion, strlen(httpVersion));
      strncat(output, contentLength, strlen(contentLength));
      strncat(output, server, strlen(server));
      strncat(output, contentType, strlen(contentType));
      strncat(output, newLine, strlen(newLine));
      strncat(output, newLine, strlen(newLine));
      strncat(output, &quot;abc&quot;, strlen(&quot;abc&quot;));
      strncat(output, newLine, strlen(newLine));
      strncat(output, newLine, strlen(newLine));
  
      while(1){
  
          if (listen(server_fd, 3) &lt; 0)
          {
              perror(&quot;listen&quot;);
              exit(EXIT_FAILURE);
          }
  
          struct sockaddr *sa = (struct sockaddr *)&amp;address;
          socklen_t *sl = (socklen_t*)&amp;addrlen;
          handle(new_socket, server_fd, sa, sl, output);
      }
  }
  
  
  int handle(int new_socket, int server_fd, struct sockaddr *sa, socklen_t *sl, char *output) {
  
      if ((new_socket = accept(server_fd, sa, sl))&lt;0)
      {
          perror(&quot;accept&quot;);
          exit(EXIT_FAILURE);
      }
  
      write(new_socket, output, strlen(output));
  
      return 0;
  }
  
  </code></pre>