各位好,今天遇到一个奇怪的问题。在写socket客户端的程序时,发现connect()函数有时阻塞,有时非阻塞。且大部分时间是非阻塞的。下面是代码:
socket_list[i].socket = socket(AF_INET,SOCK_STREAM,0);
setsockopt(socket_list[i].socket,SOL_SOCKET,SO_KEEPALIVE,&optval,sizeof(optval));
address.sin_family = AF_INET;
address.sin_addr.s_addr = netParam.srv[i].ip.s_addr;
address.sin_port = htons(netParam.srv[i].port);//(in_port_t)->(uint16_t)
len = sizeof(address);
printf("connect to srv %dn",i);
result = connect(socket_list[i].socket,(struct sockaddr *)&address,len);
if(result == -1)
{
socket_list[i].used = 0;
socket_list[i].pass = 0;
printf("can not connected to server %d.n",i);
}
else
{
printf("connected to server %d.n",i);
socket_list[i].used = 1;
xml_request(i);
}
请大家帮忙看看是啥个问题么,谢谢了。