title: C: pthread failing to listen, bind, and accept on a socket
body: <p>I am trying to create a process that will listen for connections on a socket. It seems to work when I bind, listen, and wait for accepts in the main() function. But when I attempt to create a new thread and bind, listen, and accept on that new thread, it fails. Here is my code.</p>
  
  <pre><code>void request_handler(int clientSock) {
      FILE *requestedFile = NULL;
      long fileSize = 0;
      struct stat st;
      long bytesRead;
      char buffer[1024];
  
      requestedFile = fopen("/PATH/book.txt", "rb");
  
      while(!feof(requestedFile)) {
          bytesRead = fread(buffer, 1, sizeof(buffer), requestedFile);
          send(clientSock, buffer, bytesRead, 0);
      }
  
  }
  
  void listener() {
      int server_sock_desc;
      struct sockaddr_in name;
  
      int client_sock_desc;
      struct sockaddr_in client_name;
      socklen_t addr_size;
  
      pthread_t handler_thread;
  
      printf("waiting");
  
      //connection setup
      server_sock_desc = socket(PF_INET, SOCK_STREAM, 0);
  
  
      if(server_sock_desc != -1) {
          memset(&amp;name, 0, sizeof(name));
          name.sin_family = AF_INET;
          name.sin_port = htons(5000);
          name.sin_addr.s_addr = htonl(INADDR_ANY);
          int bind_result = bind(server_sock_desc, (struct sockaddr *) &amp;name, sizeof(name));
          if(bind_result == 0) {
              if(listen(server_sock_desc, BACKLOG) &lt; 0) {
                  perror("listen failed");
              }
  
              addr_size = sizeof(client_name);
  
              //Server Loop will continue to run listening for clients connecting to the server
              while(1) {
  
                  //new client attempting to connect to the server
  
                  client_sock_desc = accept(server_sock_desc, (struct sockaddr *) &amp;client_name, &amp;addr_size);
                  if(client_sock_desc == -1) {
                      if(errno == EINTR) {
                          continue;
                      }
                      else {
                          perror("accept failed");
                          exit(1);
                      }
                  }
  
                  //connection starts here
  
                  //create a thread for the new clients request to be handled
                  if(pthread_create(&amp;handler_thread, NULL, request_handler, client_sock_desc) != 0) {
                      perror("pthread_create failed");
                  }
              }
          }
          else {
              perror("bind failed");
          }
      }
      else {
          perror("socket failed");
      }
  
  }
  
  int main(int argc, const char * argv[])
  {
      pthread_t listenerThread;
  
      if(pthread_create(&amp;listenerThread, NULL,listener, NULL) != 0) {
          perror("Listener thread create failed");
      }
  }
  </code></pre>
  
  <p>The weird thing is, when I try to run it through a debugger, sometimes part of listener() will execute, then just stop out of nowhere. </p>