title: Send image through C socket
body: <pre><code>void callback (struct Request req) {
  
      char buffer[8196];
      int file;
      bzero(buffer, sizeof(buffer));
  
      if(!strncmp(req.method, "GET", 3)){
  
          if (!strcmp(req.path, "/")){
  
              memcpy(buffer, "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n", 44);
              response(buffer);
  
              int file = open("./static/index.html", O_RDONLY);
              long ret;
  
              while ((ret = read(file, buffer, 8196)) &gt; 0) {
                  response(buffer);   
              }
  
          }else if (!strcmp(req.path, "/login")){
  
              memcpy(buffer, "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n", 44);
              response(buffer);
  
              int file = open("./static/login.html", O_RDONLY);
              long ret;
  
              while ((ret = read(file, buffer, 8196)) &gt; 0) {
                  response(buffer);   
              }
  
          }else{
  
              char path[80];
              char header[240];
  
              sprintf(path, "./static%s", req.path);
              int ext = readFile(path, buffer);
              char type[15];
              struct stat st;
  
              if(!ext){
                  strcpy(type, "text/html\0");
              }else if(ext == 1){
                  strcpy(type, "text/javascript\0");
              }else if(ext == 2){
                  strcpy(type, "text/css\0");
              }else if(ext == 3){
                  strcpy(type, "image/jpeg\0");
              }
  
              stat(path, &amp;st);
              sprintf(header, "HTTP/1.0 200 OK\r\nContent-Type: %s\r\nContent-Length: %lld\r\n\r\n", type, st.st_size);
              response(header);
  
              int file = open(path, O_RDONLY);
              long ret;
  
              while ((ret = read(file, buffer, 8196)) &gt; 0) {
                  buffer[8196-1] = 0;
                  response(buffer);   
              }
  
          }
  
      }else if(!strncmp(req.method, "POST", 4)){
  
      }
  
      bzero(buffer, sizeof(buffer));
  
  };
  
  void response (char message[]) {
      write(clientsocket, message, strlen(message));
  };
  </code></pre>
  
  <p>This is the code I have for my HTTP server coded in C. The callback function is executed everytime the socket receives a request. The struct Request has 2 <code>char []</code>, method that can be <code>POST</code> or <code>GET</code>, and path, which contains the request's path. The server sends properly the static files such html, js and css files. It serves the bootstrap statics properly, although if <code>buffer[8196-1] = 0;</code> is not when I send the file, the file contains wrong characters.</p>
  
  <p>I try to send a <code>.jpg</code> image, and the headers are properly sended, but the file is not. When I access to <code>127.0.0.1:8080/image.jpg</code>, Firefox displays an error that the image contains errors and can't be displayed, or just display a bunch of caracters and the server stops serving the rest of js and css files.</p>
  
  <p>I read all the questions releated to this and I didn't file a solution jet. The image size is 1'1Mb, maybe is that the problem? I think not because I send the data in 'pieces', but I'm not sure at all. The program doesn't show me an error about the file, seems to be opened and read correctly.</p>
  
  <p>It's a header problem maybe? The rest of the js and css files are sended in the same pice of code that the image (the <code>else</code> statement), ad they don't have any trouble, and the FF console show me the headers of every file, and they're correct so I think that's not the problem.</p>
  
  <p>Thank's advanced!</p>