- 論壇徽章:
- 0
|
程序要實(shí)現(xiàn)的功能:現(xiàn)在由n個(gè)DNS權(quán)威服務(wù)器,我依次向這些服務(wù)器發(fā)送同一個(gè)DNS查詢請求,每次發(fā)送完用select監(jiān)聽描述符集,獲取最先給出的DNS應(yīng)答,但是程序目前每次輪詢到的給出應(yīng)答的連接都是最后一個(gè)建立的描述符,但是通過wireshark抓包,在這之前已經(jīng)有服務(wù)器給出應(yīng)答了,為什么select沒有監(jiān)聽到?- int s, maxfd;
- int tries, ns, i;
- int n, sendlen, resplen;
- struct sockaddr_in nsap, from;
- socklen_t nsaplen, fromlen;
- struct timeval timeout;
- fd_set rset;
- FD_ZERO(&rset);
- maxfd = -1;
- /* No name servers or res_init() failure */
- if (statp->nscount == 0 ) {
- goto fail;
- }
- /*
- * Send request, RETRY times, or until successful.
- */
- for (tries = 0; tries < statp->retry; tries++) {
- for (ns = 0; ns < statp->nscount; ns++) {
- /* Use datagrams. */
- if(statp->socks[ns] == -1)
- {
- statp->socks[ns] = socket(PF_INET,SOCK_DGRAM,0);
- setnonblocking(statp->socks[ns]);
- }
- s = statp->socks[ns];
- printf("fd %d\n",s);
-
- nsap = statp->nsaddr_list[ns];
- nsaplen = sizeof(struct sockaddr_in);
- /* if(connect(s,(struct sockaddr *)&nsap,nsaplen) == -1)
- {
- fprintf(stderr,"connect error\n");
- goto fail;
- }*/
-
- sendlen = sendto(s,(const char *)buf,buflen,0,(struct sockaddr *)&nsap,nsaplen);
- if(sendlen <= 0)
- {
- printf("send to failed\n");
- goto fail;
- }
- if(sendlen != buflen)
- {
- printf("send to error\n");
- goto fail;
- }
- FD_SET(s, &rset);
- if(s > maxfd)
- maxfd = s;
- timeout.tv_sec = 0;
- timeout.tv_usec = 90000;
- n = select(maxfd+1, &rset, NULL, NULL, &timeout);
-
- if (n == 0) {
- printf("select timeout\n");
- continue;
- }
- if (n < 0) {
-
- printf("select error\n");
- // res_nclose(statp);
- goto fail;
- }
- for(i=0;i<=ns;i++)
- {
- if(FD_ISSET(statp->socks[i],&rset))
- {
- printf("active fd %d\n",statp->socks[i]);
- fromlen = sizeof(from);
- resplen = recvfrom(statp->socks[i], (char*)ans, anssiz,0,
- (struct sockaddr *)&from, &fromlen);
- if (resplen <= 0) {
- printf("recvfrom error\n");
- // res_nclose(statp);
- goto fail;
- }
- return resplen;
- }
-
- }
- } /*foreach ns*/
- } /*foreach retry*/
- res_nclose(statp);
- return -1;
- fail:
- res_nclose(statp);
- return -1;
復(fù)制代碼 |
|