title: Non blocking socket in c
body: <p>Currently I am working on a single server,single client udp chat application. Initially I used blocking sockets which is the default condition. Now I want to convert the socket into non-blocking so that communication between client and server could be done without the obstacle of turns...
  I've implemented the select function on the server side for now,but the when it starts the client gets to send a message once which is displayed on the server side,afterwards both client and server get unresponsive, so now I am showing how have I implemented the select() function on the server side:</p>
  
  <pre><code>            //Declaring a non-blocking structure
                fd_set readfds,writefds;
             // clear the set ahead of time
                FD_ZERO(&amp;readfds);
                FD_ZERO(&amp;writefds);
             // add our descriptor to the set
                FD_SET(sd, &amp;readfds);
                FD_SET(sd, &amp;writefds);
                /value of sd+1
                int n=sd+1;
  </code></pre>
  
  <p>Since I want to both receive and send data,I've implemented the select function in the loop:</p>
  
  <pre><code>                int client_length = (int)sizeof(struct sockaddr_in);
                  int rv = select(n, &amp;readfds, NULL, NULL, NULL);
                  if(rv==-1)
                  {
                   printf("Error in Select!!!\n");
                   exit(0);
                  }
                 else if(rv==0)
                  { 
                   printf("Timeout occurred\n");
                  }
                 else 
                  if (FD_ISSET(sd, &amp;readfds))
                  {
                  int bytes_received = recvfrom(sd, buffer,SIZE, 0, (struct sockaddr *)&amp;client, &amp;client_length);
                  if (bytes_received &lt; 0)
                 {
                 fprintf(stderr, "Could not receive datagram.\n");
                 closesocket(sd);
                 WSACleanup();
                 exit(0);
                }
                  }
  </code></pre>
  
  <p>further for sending data:</p>
  
  <pre><code>              fgets(buffer,SIZE,stdin);
                int rv1 = select(n, &amp;writefds, NULL, NULL, NULL);
                if(rv1==-1)
                {
             printf("Error in Select!!!\n");
             exit(0);
                }
               else if(rv1==0)
               {
              printf("Timeout occurred\n");
               }
              else 
               if(FD_ISSET(sd,&amp;writefds))
                    {
                       if(sendto(sd, buffer,strlen(buffer), 0, (struct sockaddr *) &amp;client,client_length)&lt;0)
                           {
                              printf("Error sending the file! \n");
                              exit(1);
                           }
                    }
  
                  }
  </code></pre>
  
  <p>So I would really appreciate if somoone let me know whether I've done this right or not,if this is ok then will the same implelementation on the client side resolve my issue?</p>