title: Server displaying text instead of HTML
body: <p>I'm attempting to make a C server that will take inputs and be able to spit them back out to the user via html format where the server works as a user interface. <strong>My current issue I cant seem to figure out is why the C server spits out the HTML code as text at localhost:3838 instead of displaying it as a proper web page.</strong> </p>
  
  <p>How can I solve this and have it be able to work to send user commands back to the server and spit out the proper response? Do I do this by using <code>recieve</code> and then putting the message into a buffer, parsing that message, and then putting that response into a buffer to send back using <code>send</code>? </p>
  
  <p><strong>CSERVER.c</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;signal.h&gt;
  
  #define PORT "3838" //port being connected to 
  #define MAXLEN 800
  #define BACKLOG 10 //number of pending connections to be held in queue
  
  //format of html page 
  
  char header []= 
  "&lt;!DOCTYPE html&gt;\n"
  "&lt;html&gt;\n"
  "&lt;head&gt;\n"
  "&lt;title&gt;Web-Based Remote Command Server&lt;/title&gt;\n"
  "&lt;/head&gt;\n"
  "&lt;body&gt;\n\n";
  char input []= 
  "&lt;form action= \"/run\" method= \"get\"&gt; \n"
  "Command: &lt;input type=\"text\" size=\"100\" name=\"command\"&gt;\n"
  "&lt;input type=\"submit\" value=\"Run\"&gt;\n"
  "&lt;/form&gt;";
  char output []= "&lt;p&gt;Command that was run:&lt;/p&gt;\n"
  "&lt;pre&gt;Your server will include the command that was just run here.&lt;/pre&gt;\n\n"
  "&lt;p&gt;Standard Output:&lt;/p&gt;\n""&lt;pre&gt;Your server will include the stdout results here.&lt;/pre&gt;\n\n"
  "&lt;p&gt;Standard Error:&lt;/p&gt;\n"
  "&lt;pre&gt;Your server will include the stderr results here.&lt;/pre&gt;\n\n"
  "&lt;/body&gt;\n""&lt;/html&gt;";
  
  char *buff = header; 
  
  void sigchld_handler(int s)
  {
      (void)s; // quiet unused variable warning
  
      // waitpid() might overwrite errno, so we save and restore it:
      int saved_errno = errno;
  
      while(waitpid(-1, NULL, WNOHANG) &gt; 0);
  
      errno = saved_errno;
  }
  
  
  void *get_in_addr(struct sockaddr *sa)
  {
      if (sa-&gt;sa_family == AF_INET) {
          return &amp;(((struct sockaddr_in*)sa)-&gt;sin_addr);
      }
  
      return &amp;(((struct sockaddr_in6*)sa)-&gt;sin6_addr);
  }
  
  
  int main (void){
      int sockfd;
      int new_fd; 
      struct addrinfo hints;
      struct addrinfo *serverinfo; 
      struct addrinfo *p;
      struct sockaddr_storage client_addr;
      socklen_t addrsize;
      struct sigaction sa;
      int yes = 1;
      char s[INET6_ADDRSTRLEN];
      int status;
  
      memset(&amp;hints, 0, sizeof hints); //makes struct empty 
      hints.ai_family = AF_UNSPEC; //IPv4 or v6 
      hints.ai_socktype = SOCK_STREAM; //TCP type need 
      hints.ai_flags = AI_PASSIVE; //Fill in IP for us 
  
  
      //if can't get address info print error 
      if((status = getaddrinfo(NULL, PORT, &amp;hints, &amp;serverinfo)) != 0){
          fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
          return 1;
      }
  
  
      for(p = serverinfo; p != NULL; p = p-&gt;ai_next){
          if((sockfd = socket(p-&gt;ai_family, p-&gt;ai_socktype, p-&gt;ai_protocol)) == -1){
              perror("server: socket");
              continue;
          }
  
          if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &amp;yes, sizeof(int)) == -1){
              perror("setsockopt");
              exit(1);
          }
  
          if(bind(sockfd, p-&gt;ai_addr, p-&gt;ai_addrlen) == -1){
              close(sockfd);
              perror("server: bind");
              continue;
          }
  
          break;
      }
  
      freeaddrinfo(serverinfo);
  
      if(p == NULL){
          fprintf(stderr, "server: failed to bind\n");
          exit(1);
      }
  
      if(listen(sockfd, BACKLOG) == -1){
          perror("listen");
          exit(1);
      }
  
      sa.sa_handler = sigchld_handler; // reap all dead processes
      sigemptyset(&amp;sa.sa_mask);
      sa.sa_flags = SA_RESTART;
      if (sigaction(SIGCHLD, &amp;sa, NULL) == -1) {
          perror("sigaction");
          exit(1);
      }
  
      printf("server: waiting for connections....\n");
  
      while(1){
          addrsize = sizeof client_addr;
          new_fd = accept(sockfd, (struct sockaddr *)&amp;client_addr, &amp;addrsize);
          if(new_fd == -1){
              perror("Did not accept");
              continue;
          }
  
          inet_ntop(client_addr.ss_family, get_in_addr((struct sockaddr *)&amp;client_addr), s, sizeof s);
          printf("server: got connection from %s\n", s);
  
          if(!fork()){
              close(sockfd);
  int bufsize = 1024;
  char *buffer = malloc(bufsize);
  recv(new_fd, buffer, bufsize, 0);
  write(new_fd, "HTTP/1.1 200 OK\n", 16);
  write(new_fd, "Content-length: 46\n", 19);
  write(new_fd, "Content-type: text/html\n\n", 25);
  write(new_fd, "&lt;html&gt;&lt;head&gt;\n&lt;title&gt;The Web Page&lt;/title&gt;\n&lt;/head&gt;\n&lt;/html&gt;", 46);
              if(send(new_fd, header, MAXLEN, 0) == -1)
                  perror("send");
              close(new_fd);
              exit(0);
          }
          close(new_fd);
      }
  
      return 0;
  
  
  }
  </code></pre>