关于socket编程的问题求助
源代码
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
{
printf("socket err!msg[%s]\n",strerror(errno));
return (-1);
}
ret=setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuseORnot, sizeof(int));/*整数型是否端口重用*/
if(ret)
{
printf("setsocketopterror!return:%d,errno=%d,errortext[%s]",ret,errno,strerror(errno));
return (-1);
}
ioflag=1;
/*ioctl(sockfd,FIONBIO,&ioflag);*/
bzero(&srv_addr, sizeof(srv_addr));
srv_addr.sin_port=htons(srv_port);
srv_addr.sin_family=AF_INET;
srv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
ret=bind(sockfd,(struct sockaddr_in *)&srv_addr,sizeof(srv_addr));
if(ret)
{
printf("bind err!msg[%s]\n", strerror(errno));
return (-1);
}
ret=listen(sockfd,5);
if(ret)
{
printf("listen err!msg[%s]\n",strerror(errno));
return (-1);
}
while(1)
{
bzero(&cli_addr,sizeof(cli_addr));
size= sizeof(cli_addr);
printf("size=[%d]\n",size);
cli_sockfd=accept(sockfd,(struct sockaddr_in *)(&cli_addr), &size);
if(cli_sockfd<0&&errno==EINTR)
{
continue;
printf("hi\n");
}else if(cli_sockfd<0)
{
printf("failed to accept connection,msg[%s]\n",strerror(errno));
return (-1);
}
ioctl注释以后程序正常,ioctl打开以后,执行程序就报 failed to accept connection,msg[Resource temporarily unavailable] ,求稳是什么原因呢?
------解决方案--------------------
Ioctl设置非阻塞了, accept没有connect当然返回EAGAIN了。