亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1856 | 回復(fù): 0
打印 上一主題 下一主題

[網(wǎng)絡(luò)] 單線程客戶端處理多個(gè)socket連接的問題 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2014-12-06 12:42 |只看該作者 |倒序?yàn)g覽
程序要實(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)聽到?
  1. int s, maxfd;
  2.         int tries, ns, i;
  3.         int n, sendlen, resplen;
  4.         struct sockaddr_in nsap, from;
  5.         socklen_t nsaplen, fromlen;
  6.         struct timeval timeout;
  7.         fd_set rset;

  8.         FD_ZERO(&rset);
  9.         maxfd = -1;

  10.         /* No name servers or res_init() failure */
  11.         if (statp->nscount == 0 ) {
  12.                 goto fail;
  13.         }

  14.         /*
  15.          * Send request, RETRY times, or until successful.
  16.          */

  17.         for (tries = 0; tries < statp->retry; tries++) {

  18.             for (ns = 0; ns < statp->nscount; ns++) {
  19.                         /* Use datagrams. */
  20.                         if(statp->socks[ns] == -1)
  21.                         {
  22.                                 statp->socks[ns] = socket(PF_INET,SOCK_DGRAM,0);
  23.                                 setnonblocking(statp->socks[ns]);

  24.                         }
  25.                         s = statp->socks[ns];
  26.                         printf("fd %d\n",s);
  27.                        
  28.                         nsap = statp->nsaddr_list[ns];
  29.                         nsaplen = sizeof(struct sockaddr_in);
  30.                 /*        if(connect(s,(struct sockaddr *)&nsap,nsaplen) == -1)
  31.                         {
  32.                                 fprintf(stderr,"connect error\n");
  33.                                 goto fail;
  34.                         }*/
  35.                                
  36.                         sendlen = sendto(s,(const char *)buf,buflen,0,(struct sockaddr *)&nsap,nsaplen);
  37.                         if(sendlen <= 0)
  38.                         {
  39.                                 printf("send to failed\n");
  40.                                 goto fail;
  41.                         }

  42.                         if(sendlen != buflen)
  43.                         {
  44.                                 printf("send to error\n");
  45.                                 goto fail;
  46.                         }

  47.                         FD_SET(s, &rset);
  48.                         if(s > maxfd)
  49.                                 maxfd = s;
  50.                         timeout.tv_sec = 0;
  51.                         timeout.tv_usec = 90000;
  52.                         n = select(maxfd+1, &rset, NULL, NULL, &timeout);
  53.                
  54.                         if (n == 0) {
  55.                                 printf("select timeout\n");
  56.                                 continue;
  57.                         }
  58.                         if (n < 0) {
  59.                
  60.                                 printf("select error\n");
  61. //                                res_nclose(statp);
  62.                                 goto fail;
  63.                         }
  64.                         for(i=0;i<=ns;i++)
  65.                         {       
  66.                                 if(FD_ISSET(statp->socks[i],&rset))
  67.                                 {
  68.                                         printf("active fd %d\n",statp->socks[i]);
  69.                                         fromlen = sizeof(from);
  70.                                         resplen = recvfrom(statp->socks[i], (char*)ans, anssiz,0,
  71.                                                    (struct sockaddr *)&from, &fromlen);
  72.                                         if (resplen <= 0) {
  73.                                                 printf("recvfrom error\n");
  74. //                                                res_nclose(statp);
  75.                                                 goto fail;       
  76.                                         }
  77.                                         return resplen;
  78.                                 }
  79.                                
  80.                         }
  81.                 } /*foreach ns*/
  82.         } /*foreach retry*/
  83.         res_nclose(statp);
  84.         return -1;
  85. fail:
  86.     res_nclose(statp);
  87.         return -1;
復(fù)制代碼
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP