title: Linux C Socket:"bad file descriptor" occurs when accept() invokes
body: <p>I am writing a send/receive project using linux/socket/tcp, I create some threads to send or recieve message,and use a global queue to store message 
  Code:</p>
  
  <pre><code>void EnQueue (M_queue queue,char * message,char * target_IP,char * target_IP_BAK)
  {
      char a;
      char  * m=&amp;a;
      strcpy(m,target_IP);
      M_element node=(M_element)malloc(sizeof(struct MessageNode ));
      node-&gt;message=message;
      node-&gt;target_IP=target_IP;
      node-&gt;next=NULL;
      if(IsQueueEmpty(queue))
      {
       queue-&gt;rear=queue-&gt;front=node;
      }
      else
      {
          queue-&gt;rear-&gt;next=node;
          queue-&gt;rear=node;
      }
  }
  M_queue messages;
  void* receive_message();
  void* send_message(void* args);
  int main()
  {
  
  
  
      pthread_t in_thread,out_thread;
  
      messages=(M_queue)malloc(sizeof(struct MessageQueue));
      init(messages);
      int ret=pthread_create(&amp;in_thread,NULL,receive_message,NULL);
      if(ret==-1)
  
      {
          perror("thread receive:");
      }
  
      while(1)
      {
          if(!IsQueueEmpty(messages))
          {
  
              M_element message=DeQueue(messages);
  
              pthread_create(&amp;out_thread,NULL,send_message,(void*)message);
  
          }
  
  
      }
      pthread_join(in_thread,NULL);
      return 0;
  
  
  }
  void*  send_message(void * args)
  {
    struct sockaddr_in  sockaddr_out;
    M_element message=(M_element)args;
    int cfd=socket(AF_INET,SOCK_STREAM,0);
    if(cfd==-1)
    {
        perror("send_socket");
  
        exit(1);
    }
    sockaddr_out.sin_family=AF_INET;
    sockaddr_out.sin_port=htons(9734);
    printf("%s\n",message-&gt;target_IP);
    sockaddr_out.sin_addr.s_addr=inet_addr(message-&gt;target_IP);
    int ret=connect(cfd,(struct sockaddr *)&amp;sockaddr_out,sizeof(sockaddr_out));
    if(ret==-1)
    {
        perror("connect");
        offline_message(message-&gt;target_IP,message-&gt;message); 
        exit(1);
    }
    if(write(cfd,message-&gt;message,strlen(message-&gt;message)+1)==-1)
    {
        perror("write");
        exit(1);
    }
    close(cfd);
  }
  void*  receive_message()
  {
      char  buf[256];
      char IP[256];
      memset(IP,0,256);
      int first_socket=socket(AF_INET,SOCK_STREAM,0);
      if(first_socket==-1)
      {
          perror("socket");
          exit(1);
      }
      struct sockaddr_in in_socketaddr;
      in_socketaddr.sin_family=AF_INET;
      in_socketaddr.sin_port=htons(9999);
      in_socketaddr.sin_addr.s_addr=INADDR_ANY;
      int ret=bind(first_socket,(struct  sockaddr *)&amp;in_socketaddr,sizeof(in_socketaddr));
      if (ret==-1)
      {
          perror("bind");
          exit(1);
      }
      ret=listen(first_socket,10);
      if(ret==-1)
      {
          perror("listen");
          exit(1);
      }
  
      int accept_socket;
  
  
      while(1)
      {
          **//error occurs**
          **accept_socket=accept(first_socket,NULL,NULL);**
          printf("%d!",first_socket);
          if(accept_socket==-1)
          {
              perror("accept");
              exit(1);
          }
          int ret=read(accept_socket,buf,sizeof(buf));
          close(accept_socket);
          if(ret==-1)
          {
              perror("read1");
              exit(1);
          }
  
          findIP(buf,IP);
          **EnQueue(messages,buf,IP,IP);**
          printf("%d!",first_socket);
  
  
      }
      close(first_socket);
  }
  </code></pre>
  
  <p>I send  message "test" to the appliaction , accecpt() works well
  but I send message "test111111" ,"bad file descriptor" occurs when accept() invokes;
  and I find the 'first_socket' changes when EnQueue() invokes,but i cant find a solution ..anyone can help me ,THX</p>