我有一個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: %dn", 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:%sn", dataBuffer);
handleMessage(dataBuffer);
}
}