Hi,

I have to write a UDP client to send a request to the server. Sometimes server does not respond in time. I need to timeout recvfrom() after certain time (say 5 secs) and send another request to the server. I know that one probable answer is using select() which I am trying myself. However, I am having some problem there. I send one request to the server and enter select(). Following is my code.

Code:
tout.tv_sec = 5;
tout.tv_usec = 1;

FD_SET(NBClientSockId,&read_set);
FD_SET(NBClientSockId,&write_set);

    fd_max = NBClientSockId;

    if ((bytes_sent = sendto(NBClientSockId,(nb_query_t *)&q_pkt,
                                     sizeof(nb_query_t),0,
                                     (NBSock *)&NBServerSock,
                                     sizeof(NBSock))) == -1)
    {
        perror("sendto");
        exit(1);
    } else {
        printf("Netbios: Query Sent to %s for Netbios Name: %s \n",
                 inet_ntoa(NBServerSock.sin_addr),q_pkt.nbname);
        fprintf(fp,"Netbios: Query Sent to %s for Netbios Name: %s \n",
                 inet_ntoa(NBServerSock.sin_addr),q_pkt.nbname);
    }


    for (;;)
    {
        nb = select(fd_max+1,&read_set,&write_set,(fd_set *) 0,&tout);
        if (nb < 0)
        {
            perror("select");
     close(NBClientSockId);
            exit(1);
        }

        if (nb)
        {
            if (FD_ISSET(NBClientSockId,&read_set)) {
              if ((bytes_rcvd = recvfrom(NBClientSockId,(nb_response_t *)&r_pkt,
                                         sizeof(nb_response_t),0,
                                         (NBSock *)&from, &sin_size)) == -1)
              {
                  perror("recvfrom");
                  exit(1);
              } else {
                  printf("nbname: %s r_pkt.nbname %s\n",nbname,r_pkt.nbname);
                  if (strcmp(nbname,r_pkt.nbname) != 0) continue;
                  printf("Netbios: Response received from %s for Netbios Name \"%s\" ",
                                inet_ntoa(from.sin_addr),r_pkt.nbname);
                  fprintf(fp,"Netbios: Response received from %s for Netbios Name \"%s\"",
                                inet_ntoa(from.sin_addr),r_pkt.nbname);
                  printf("IP address: %s\n",inet_ntoa(r_pkt.hip));
                  fprintf(fp,"IP address: %s\n",inet_ntoa(r_pkt.hip));
                  return;
              }

            } else if (FD_ISSET(NBClientSockId,&write_set)) {
                if ((bytes_sent = sendto(NBClientSockId,(nb_query_t *)&q_pkt,
                                     sizeof(nb_query_t),0,
                                     (NBSock *)&NBServerSock,
                                     sizeof(NBSock))) == -1)
                 {
                     perror("sendto");
                     exit(1);
                 } else {
                     printf("Netbios: Query Sent to %s for Netbios Name: %s \n",
                                    inet_ntoa(NBServerSock.sin_addr),q_pkt.nbname);
                      fprintf(fp,"Netbios: Query Sent to %s for Netbios Name: %s \n",
                                    inet_ntoa(NBServerSock.sin_addr),q_pkt.nbname);
                 }

            }
    }

    }
The problem with this code is if I don't receive the response the first time itself, client continueously send the request to the server (write_fds), it does not receive anymore. Even when the server actually sends the reply it does not return.

Anyone has any solution to this problem.? I am developing on Linux

Thanks,