模拟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); 最好能给出步骤。
谢谢。
用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); 最好能给出步骤。
谢谢。
while (1)
{
ii = select(nfds, &readfds, NULL, NULL, &timeout)
if ( ii > 0)
{
k = recvfrom(....);
}
Sleep(100);
}
struct timeval
{
long tv_sec; /*秒数*/
long tv_usec; /*微秒*/
};