当前位置: 技术问答>linux和unix
udp socket recvfrom偶尔会返回-1,perror打印出的提示是"recvfrom: Resource temporarily unavailable"
来源: 互联网 发布时间:2016-02-15
本文导语: 我在用socket接收udp数据包时,select函数却返回正确的情况下recvfrom偶尔会返回-1,perror打印出的提示是"recvfrom: Resource temporarily unavailable",然后重新循环接收,执行recvfrom又返回正确了,代码如下,不知道是什么原因?大家帮我...
我在用socket接收udp数据包时,select函数却返回正确的情况下recvfrom偶尔会返回-1,perror打印出的提示是"recvfrom: Resource temporarily unavailable",然后重新循环接收,执行recvfrom又返回正确了,代码如下,不知道是什么原因?大家帮我找找原因.
.....
struct timeval timeout;
fd_set fdread;
int ret;
for (;;)
{
timeout.tv_sec=1;
timeout.tv_usec=0;
FD_ZERO(&fdread);
pthread_mutex_lock(&mutex_recvsock);
FD_SET(recv_sock, &fdread);
ret = select(recv_sock+1, &fdread, NULL,NULL, &timeout);
if(ret == -1){
pthread_mutex_unlock(&mutex_recvsock);
old_switch_addr = 0;//source_addr;
perror("recv_data select1");
continue;
}
else if(ret == 0){
pthread_mutex_unlock(&mutex_recvsock);
printf("select timeoutn");
perror("recv_data select2");
continue;
}
else if (FD_ISSET(recv_sock,&fdread)) {
nsize = recvfrom(recv_sock,buf, sizeof(buf), 0, (struct sockaddr *)&source2, &sin_len);
if(nsize == -1){
printf("recv_data recvfrom errorn");
perror("recv_data recvfrom");
pthread_mutex_unlock(&mutex_recvsock);
continue;
}
}
else{
pthread_mutex_unlock(&mutex_recvsock);
perror("recv_data select3");
continue;
}
.......
.....
struct timeval timeout;
fd_set fdread;
int ret;
for (;;)
{
timeout.tv_sec=1;
timeout.tv_usec=0;
FD_ZERO(&fdread);
pthread_mutex_lock(&mutex_recvsock);
FD_SET(recv_sock, &fdread);
ret = select(recv_sock+1, &fdread, NULL,NULL, &timeout);
if(ret == -1){
pthread_mutex_unlock(&mutex_recvsock);
old_switch_addr = 0;//source_addr;
perror("recv_data select1");
continue;
}
else if(ret == 0){
pthread_mutex_unlock(&mutex_recvsock);
printf("select timeoutn");
perror("recv_data select2");
continue;
}
else if (FD_ISSET(recv_sock,&fdread)) {
nsize = recvfrom(recv_sock,buf, sizeof(buf), 0, (struct sockaddr *)&source2, &sin_len);
if(nsize == -1){
printf("recv_data recvfrom errorn");
perror("recv_data recvfrom");
pthread_mutex_unlock(&mutex_recvsock);
continue;
}
}
else{
pthread_mutex_unlock(&mutex_recvsock);
perror("recv_data select3");
continue;
}
.......
|
很正常, UDP, 就这样.
|
应该是线程同步的原因吧,可能是多个线程同时读数据了.