title: 'Bad file descriptor' error on an accepted socket
body: <p>I'm trying to get into network programming and have been following a guide on sockets. I wrote a simple Windows TCP server and client in C, but cannot seem to get them working. The second the client connects, the server throws a 'Bad file descriptor' error and closes. I don't understand how the file descriptor that was just created by <code>accept()</code> can be bad. I have done research on this and have only found people who had closed the file before reading it, which I did not.</p>
  
  <p>Here is the code :</p>
  
  <p>SERVER</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;winsock2.h&gt;
  
  int main(int argc, char *argv[])
  {
      WSADATA WSAData;
      WSAStartup(MAKEWORD(2,0), &amp;WSAData);
  
      SOCKET sock, newsock;
      int portno, clilen;
      char buffer[256];
      SOCKADDR_IN serv_addr, cli_addr;
      int n;
  
      sock = socket(AF_INET, SOCK_STREAM, 0);
  
      if (sock &lt; 0)
      {
          perror("Error opening socket");
          exit(1);
      }
  
      memset((char *) &amp;serv_addr, 0, sizeof(serv_addr));
      portno = 5001;
  
      serv_addr.sin_family = AF_INET;
      serv_addr.sin_addr.s_addr = INADDR_ANY;
      serv_addr.sin_port = htons(portno);
  
      if (bind(sock, (SOCKADDR *) &amp;serv_addr, sizeof(serv_addr)) &lt; 0)
      {
          perror("Eror on binding");
          exit(1);
      }
  
      listen(sock, 5);
      clilen = sizeof(cli_addr);
  
      newsock = accept(sock, (SOCKADDR *) &amp;cli_addr, &amp;clilen);
      if (newsock &lt; 0)
      {
          perror("Error on accept");
          exit(1);
      }
  
      memset(buffer, 0, 256);
      n = read(newsock, buffer, 255);
  
      if (n &lt; 0)
      {
          perror("Error reading from socket");
          exit(1);
      }
  
      printf("Here is the message : %s\n", buffer);
  
      n = write(newsock, "I got your message", 18);
      if (n &lt; 0) 
      {
          perror("Error writing to socket");
          exit(1);
      }
  
      WSACleanup();
      return 0;
  }
  </code></pre>
  
  <p>CLIENT</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;winsock2.h&gt;
  
  int main(int argc, char *argv[])
  {
      WSADATA WSAData;
      WSAStartup(MAKEWORD(2,0), &amp;WSAData);
  
      SOCKET sock;
      int portno, n;
      SOCKADDR_IN serv_addr;
      HOSTENT *server;
      char buffer[256];
  
      if (argc &lt; 3)
      {
          fprintf(stderr, "Usage : %s hostname port\n", argv[0]);
          exit(0);
      }
  
      portno = atoi(argv[2]);
      sock = socket(AF_INET, SOCK_STREAM, 0);
      if (sock &lt; 0)
      {
          perror("Error opening socket\n");
          exit(1);
      }
  
      server = gethostbyname(argv[1]);
  
      if (server == NULL)
      {
          perror("Error, no such host");
          exit(1);
      }
  
      memset((char *) &amp;serv_addr, 0, sizeof(serv_addr));
      serv_addr.sin_family = AF_INET;
      memcpy((char *) &amp;serv_addr.sin_addr.s_addr, (char *) server-&gt;h_addr, server-&gt;h_length);
      serv_addr.sin_port = htons(portno);
  
      if (connect(sock, (SOCKADDR *) &amp;serv_addr, sizeof(serv_addr)) &lt; 0)
      {
          perror("Error connecting to server");
          exit(1);
      }
  
      printf("Please enter your message : ");
      memset(buffer, 0, 256);
      fgets(buffer, 255, stdin);
  
      n = write(sock, buffer, strlen(buffer));
      if (n &lt; 0)
      {
          perror("Error writing to socket");
          exit(1);
      }
  
      memset(buffer, 0, 256);
      n = read(sock, buffer, 255);
  
      if (n &lt; 0)
      {
          perror("Error reading from socket");
          exit(1);
      }
  
      printf("%s\n", buffer);
  
      WSACleanup();
      return 0;
  }
  </code></pre>
  
  <p>...And here's the output just in case :</p>
  
  <pre><code>C:\...myfolder&gt;server
  *here it does nothing until I start the client with "client 127.0.0.1 5001" in another window*
  Error reading from socket: Bad file descriptor
  </code></pre>
  
  <p>Do you see what is wrong with my code? Thanks in advance for your help!</p>