title: Linux Socket Bad File Descriptor
body: <p>I couldn’t find a duplicate, so I decided to post. We're getting into Sockets (beginner-level) now, and was given the code below to make the client send a simple message to the server by using <code>send()</code> and <code>recv()</code>. However, everything I have tried doesn’t seem to get rid of the <code>error: Bad File Descriptor</code> and <code>newsockfd</code> always returns the value <code>-1</code>. I'm confused and have no idea why it doesn’t work.</p>
  
  <p><strong>defs.h</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  
  #define SERV_TCP_PORT 6000
  #define SERV_HOST_ADDR  "127.0.0.1" 
  
  char *pname;
  </code></pre>
  
  <p><strong>client.c</strong></p>
  
  <pre><code>#include "../defs.h"
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;strings.h&gt;
  
  main(argc, argv)
  int argc;
  char *argv[];
  {
    int sockfd;
    struct sockaddr_in serv_addr;
  
    pname=argv[0];
    bzero((char*) &amp;serv_addr, sizeof(serv_addr));
    serv_addr.sin_family=AF_INET;
    serv_addr.sin_addr.s_addr=inet_addr(SERV_HOST_ADDR);
    serv_addr.sin_port=htons(SERV_TCP_PORT);
    if((sockfd=socket(AF_INET,SOCK_STREAM,0)) &lt; 0) {
      printf("client: can't open stream socket\n");
      exit(0);
    }
    if (connect(sockfd,(struct sockaddr *) &amp;serv_addr, sizeof(serv_addr)) &lt; 0) {
      printf("client: can't connect to server\n");
      exit(0);
    }
   send(sockfd,"1",1,0);
    close(sockfd);
    exit(0);
  }
  </code></pre>
  
  <p><strong>server.c</strong></p>
  
  <pre><code>#include "../defs.h"
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;strings.h&gt;
  void error(char *msg)
  {
      perror(msg);
      exit(1);
  }
  main(argc, argv)
  int argc;
  char *argv[];
  {
    int sockfd,clilen, childpid;
   int newsockfd;
  char buff[255];
    struct sockaddr_in cli_addr, serv_addr;
    if ((sockfd=socket(AF_INET,SOCK_STREAM, 0)) &lt;0) {
      printf("server: can't open stream socket\n");
      exit(0);
    }
    bzero((char*) &amp;serv_addr, sizeof(serv_addr));
    serv_addr.sin_family=AF_INET;
    serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
    serv_addr.sin_port=htons(SERV_TCP_PORT);
  
    if(bind(sockfd, (struct sockaddr *) &amp;serv_addr, sizeof(serv_addr)) &lt; 0) {
      printf("server: can't bind local address\n");
      exit(0);
    }
    listen(sockfd, 5);
    for ( ; ; ) {
      clilen=sizeof(cli_addr);
      newsockfd = accept(sockfd, (struct sockaddr *) &amp;cli_addr, &amp;clilen);
    printf("%d\n",newsockfd);
      if(newsockfd &lt; 0) {
        printf("server: accept error\n");
      error("ERROR on accept");
        exit(0);
      }
      if((childpid=fork()) &lt;0) {
        printf("server: fork error\n");
        exit(0);
      }
      else if (childpid==0) {
          close(sockfd);  
        recv(newsockfd,buff,sizeof(buff),0);
      }
      close(newsockfd);
    }
  }
  </code></pre>
  
  <p>Thanks for your time.</p>