title: Server Socket errno 57 - Socket is not connected
body: <p>For some reason i am getting error an 57 - socket is not connected.</p>
  
  <p>Why?</p>
  
  <p><strong>Console output:</strong></p>
  
  <pre><code>GET / HTTP/1.1
  Host: localhost:8080
  Connection: keep-alive
  Cache-Control: max-age=0
  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
  User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36
  Accept-Encoding: gzip,deflate,sdch
  Accept-Language: en-US,en;q=0.8,it;q=0.6,lt;q=0.4,nl;q=0.2,ru;q=0.2
  
  Can't write to socket 57
  </code></pre>
  
  <p><strong>The code:</strong></p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;unistd.h&gt;
  
  #include &lt;errno.h&gt;
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  #define PORT 8080
  #define PROTOCOL 0
  #define BACKLOG 10
  #define BUFLEN 1500
  
  void str_server(int sock) 
  { 
    char buf[1025]; 
    const char* filename = "index.html"; 
    FILE *file = fopen(filename, "rb"); 
    if (!file)
      {
        printf("Can't open file for reading"); 
        return;
      }
    while (!feof(file)) 
      { 
        int rval = fread(buf, 1, sizeof(buf), file); 
        if (rval &lt; 1)
          {
        printf("Can't read from file");
        fclose(file);
        return;
          }
  
        int off = 0;
          do
        {
              int sent = send(sock, &amp;buf[off], rval - off, 0);
              if (sent &lt; 1)
            {
                  printf("Can't write to socket %d", errno);
                  fclose(file);
                  return;
            }
  
              off += sent;
        }
          while (off &lt; rval);
      } 
  
    fclose(file);
  } 
  
  int main()
  {
    int fd;
    int connfd;
  
    // For bind()
    struct sockaddr_in addr; 
  
    // For accept()
    struct sockaddr_in cliaddr; 
    socklen_t cliaddrlen = sizeof(cliaddr);
  
    // For reading and writing
    ssize_t i;
    ssize_t rcount;
    char buffer[BUFLEN];
  
    // Open a socket
    fd = socket(AF_INET, SOCK_STREAM, PROTOCOL);
    if (fd == -1) {
      printf("Error: unable to open a socket\n");
      printf("Error code: %d\n", errno);
      exit(1);
    }
  
    // Create an address
    //memset(&amp;addr, 0, sizeof addr);
    addr.sin_addr.s_addr = INADDR_ANY;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
  
    int yes = 1;
    if ( setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &amp;yes, sizeof(int)) == -1 )
    {
        perror("setsockopt");
    }
  
    if ((bind(fd, (struct sockaddr *)&amp;addr, sizeof(addr))) == -1) {
      printf("Error: unable to bind\n");
      printf("Error code: %d\n", errno);
      exit(1);
    }
  
    // List for connections
    if ((listen(fd, BACKLOG)) == -1) {
      printf("Error: unable to listen for connections\n");
      printf("Error code: %d\n", errno);
      exit(1);
    }
  
    // Accept connections
    connfd = accept(fd, (struct sockaddr *) &amp;cliaddr, &amp;cliaddrlen);
    if (connfd == -1) {
      printf("Error: unable to accept connections\n");
      printf("Error code: %d\n", errno);
      exit(1);
    }
  
    // Read data
    rcount = read(connfd, buffer, BUFLEN);
    if (rcount == -1) {    
      printf("Error: unable to accept connections\n");
      printf("Error code: %d\n", errno);
      exit(1);
    }
  
    for (i = 0; i &lt; rcount; i++) {
      printf("%c", buffer[i]);
    }
  
    //write(connfd, buffer, rcount);
    str_server(fd);
  
  
    // Bye
    //close(connfd);
    return 0;
  }
  </code></pre>