我有一个 UDP 客户端,它必须从两个不同的套接字接收。
我正在使用select
系统调用来多路复用recv
调用。
但我看到客户端在第二次recv
通话中被阻止。
我该如何解决这个问题?
struct timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
int activity;
FD_ZERO(&socketfds);
FD_SET(usocket,&socketfds);
max_sd = std::max(max_sd, usocket);
FD_SET(msocket,&socketfds);
max_sd = std::max(max_sd, msocket);
rset = socketfds;
do
{
rset = socketfds;
activity = select( max_sd + 1 , &rset , NULL , NULL , &timeout);
}
while(activity<0 && errno == EINTR);
if ((activity < 0) && (errno!=EINTR))
{
printf("select error");
}
if(FD_ISSET(usocket, &socketfds))
{
int len;
printf("Receiving from unicast socket..\n");
if((len = recvfrom(usocket, dataBuffer, dataLength, 0, (struct sockaddr *)
&clientAddr, &clen) < 0) )
{
printf("Error reading message \n");
close(msocket);
exit(-1);
}
else
{
printf("Size of message: %d\n", strlen(dataBuffer));
handleMessage(dataBuffer);
}
}
if(FD_ISSET(msocket, &socketfds))
{
printf("Receiving from multicast socket..\n");
if((recvfrom(msocket, dataBuffer, dataLength, 0, (struct sockaddr *)
&multicastClientAddr, &mlen) < 0) )
{
printf("Error reading message \n");
close(msocket);
exit(-1);
}
else
{
printf("Message from server:%s\n", dataBuffer);
handleMessage(dataBuffer);
}
}