模拟ping程序,当要ping的主机不存在时recvfrom就会一直阻塞,能设定recvfrom阻塞的时间吗?
用select怎样实现?用下面的程序无法实现://定义套接字
SOCKET rawSocket;
rawSocket=socket(AF_INET, SOCK_RAW,IPPROTO_ICMP);//IPPROTO_ICMP); //定义等待时间
FD_SET readfds,writefds,expectfds;
int nfds=0;
struct timeval timeout;
timeout.tv_sec=1;
timeout.tv_usec=50;
FD_ZERO(&readfds); 
FD_SET(rawSocket,&readfds);
int ii=select(nfds,&readfds,NULL,NULL,&timeout);
……
int k=recvfrom(rawSocket,rcvbuff,rcvmaxsize, 0,(struct sockaddr*)&from, &fromlen); 最好能给出步骤。
谢谢。

解决方案 »

  1.   

    ........
    while (1)
    {
       ii = select(nfds, &readfds, NULL, NULL, &timeout)
       if ( ii > 0)
       {
           k = recvfrom(....);
       }
       Sleep(100); 
    }
      

  2.   

    据我所知是不能的,而且你用的是Select模型,其实对于非阻塞模式,立即会返回结果的,如果失败的话就会给你一个“SOCK_ERROR”的值。
      

  3.   

    哈,我都忘了啊,磨剑是对的,确实可以设置最多等待I/O操作完成多久的时间。timeval结构定义:
    struct timeval
    {
    long tv_sec; /*秒数*/
    long tv_usec; /*微秒*/
    };
      

  4.   

    可以啊,把socket设为非阻塞,然后用一楼的方法就可以了
      

  5.   

    setsockopt,RCV_TIME_O可能也可以设