linux socket recv Resource temporarily unavailable
代码如下:
if((socket_fd=socket(AF_INET,SOCK_STREAM,0))==-1){
perror( "error socket ");
exit(1);
}
setsockopt(socket_fd, SOL_SOCKET, SO_RCVTIMEO, &tv_out, sizeof(tv_out));
setsockopt(socket_fd, SOL_SOCKET, SO_SNDTIMEO, &tv_out, sizeof(tv_out));
op_addr.sin_family = AF_INET;
op_addr.sin_port = htons(atoi(port));
op_addr.sin_addr.s_addr = inet_addr(ip);
bzero(&(op_addr.sin_zero), 8);
if(connect(socket_fd, (struct sockaddr *)&op_addr, sizeof(struct sockaddr)) == -1)
{
perror( " hlrscan connect ");
userlog( " hlrscan connect ");
strcpy(send_buf, "接口失败 ");
close(socket_fd);
sleep(3);
return -1;
}
printf( "len=%d\n ",strlen(send1));
printf( "send=%s\n ",send1);
if((send(socket_fd, send1, strlen(send1), 0)) == -1)
{
perror( " error send ");
userlog( " hlrscan send ");
strcpy(send_buf, "接口失败 ");
close(socket_fd);
return -1;
}
if((recv(socket_fd,(void *)(recv_buff),sizeof(recv_buff),0)) <= 0)
{
perror( " recv error! ");
userlog( " hlrscan recv ");
strcpy(send_buf, "1~接口失败~ ");
close(socket_fd);
return -1;
}
郁闷 recv在繁忙的时候程序总报Resource temporarily unavailable ,据说非阻塞socket都是这样的,但各位有没有好的解决办法, 借鉴一下,谢谢
[解决办法]
用select、poll、epoll等都可解决
[解决办法]
推荐看一下unix网络编程
[解决办法]
没有!最好的办法就是稍后重试。
非阻塞模式下,你调用了阻塞操作,在该操作没有完成就返回后就返回这个错误,这个错误不会破坏socket的同步,不用管他,下次循环接着recv就可以了。