- 论坛徽章:
- 0
|
想问一下,是不是设置了socket为非阻塞模式后,就不能再使用select了啊?
因为我自己写的程序中设置了socket为非阻塞模式后再利用select等待接收数据包时,每次都是超时,接收不到数据包。然后一怒之下把socket设置回普通模式,再把select去掉就可以了。不知道为什么。具体程序是这样的
/*sockopts = fcntl(thisint->sockInt,F_GETFL,0);
if(sockopts<0)
{
printf("\neror getting socket options\n");
return -3;
}
sockerr = fcntl(thisint->sockInt,F_SETFL,sockopts | O_NONBLOCK);
if(sockerr<0)
{
printf("\nerror setting options for interface\n");
return -3;
}*/
这是设置为非阻塞模式
然后
while(1)
{
/* (void)sigfillset(&sigset_full);
(void)sigdelset(&sigset_full,SIGINT);
FD_ZERO(&read_set); FD_SET(p_fd,&read_set);
timeout.tv_sec = 1;
timeout.tv_nsec = 0;
//wait with all signals(except SIGINT) blocked.
switch(pselect(p_fd+1,&read_set,NULL,NULL,&timeout,&sigset_full))
{
case -1://Normally, this case should not happen since sig_intr() never returns!
printf("\nit is bad\n");
case 0://time out
switch(m_state)
{
case 0:
printf("\ntime out ,not receive a packet\n");
if(++packetCount_SentFindServer>3)
{
printf("\nrestarting authentication\n");
goto beginAuthentication;
}
(void)SendFindServerPacket(intcur);
continue;//jump to next loop of while(1) to receive next packet
case 1:
if(++packetCount_SentName>3)
{
printf("\nrestarting authentication\n");
goto beginAuthentication;
}
(void)SendNamePacket(intcur,pkt_data);
continue;
case 2:
if(++packetCount_SentPassword>3)
{
printf("\nrestarting authentication\n");
goto beginAuthentication;
}
(void)SendPasswordPacket(intcur,pkt_data);
continue;
default:
printf("\nit is bad\n");
}
}*/
printf("\nready to get packet\n");
newsize = recvfrom(intcur->sockInt,pkt_data,1550,0,(struct sockaddr *)&intcur->sll,&socketlen);
这就接收不到包了。 |
|