title: Bad file descriptor in recvfrom function
body: <p>This is my server side code for udp  </p>
  
  <pre><code>#include"headers.h"
  int main(int argc,char **argv)
  {
      //------------udp server socket connection------//
  
      int sd1;
      struct sockaddr_in serveraddr, clientaddr;
      char buffer[100];
      char *bufptr = buffer;
      int cnt=1,ret;
      socklen_t clen;
      clen=sizeof(clientaddr);
  
      //int buflen = sizeof(buffer);
  
      sd1=socket(AF_INET,SOCK_DGRAM,0);
  
      printf("udp socket id=%d\n",sd1);
  
      printf("socket created for udp..\n");
  
      if(sd1&lt;0)
      {
          perror("udp_sfd");
          exit(0);
      }
  
      printf("server socket created..\n");
  
      serveraddr.sin_family=AF_INET;
      serveraddr.sin_port=htons(atoi(argv[1]));
      serveraddr.sin_addr.s_addr=INADDR_ANY;
  
      if(bind(sd1,(struct sockaddr *)&amp;serveraddr,sizeof(serveraddr))&lt;0)
      {
          perror("bind\n");
          exit(0);
      }
      else
      {
  
          while(1)
          {
              printf("server accept from client\n");
  
              ret=recvfrom(sd1,(char *)bufptr,strlen(buffer),0,(struct sockaddr *)&amp;clientaddr,&amp;clen);
              printf("ret=%d\n",ret);
  
              //printf("hello\n");
              if(ret&lt;0)
              {
                  perror("recvfrom");
                  exit(0);
              }
              else
              {
                  printf("UDP Server received the following:\n \"%s\" message\n", bufptr);
              }
              //close(sd1);
          }
      }
  
      close(sd1);
      return 0;
  }
  </code></pre>
  
  <p>I am sending tht buffer from client side... and in server side it is giving me an error like this....</p>
  
  <p>Bad file descriptor .... what should I do...</p>
  
  <p>I also changed the name of file descriptor 2 times... still it is not working...</p>