3

我正在尝试使用 Select 调用的客户端服务器代码。但是,一旦我从客户端发送到服务器,服务器就会不断地从客户端发送的缓冲区中读取数据,即使客户端在 Ist 数据包之后没有发送任何内容。我在哪里做错了?

下面是代码片段:

fd_set readSet,master_list_read;
FD_ZERO(&master_list_read);
FD_SET(STDIN, &master_list_read);//stdin manually trigger reading
FD_SET(sockfd, &master_list_read);//tcp socket

struct timeval tv;
int retval;
tv.tv_sec = 10;
tv.tv_usec = 0;
while(1)
{ 
bool idle=false;
char *buffer=NULL;
int n;

tv.tv_sec = 10;
readSet=master_list_read;
retval = select(sockfd+1, &readSet, NULL, NULL,&tv);
if (retval == -1) 
{
    perror("Error in select\n");
    exit(4);
}
else if(retval==0)
{ 
    printf("*********SEND SUCCESSFUL******\n");
}       
else 
{   
if(FD_ISSET(0, &readSet))
{
    bzero(buf,256);
    fgets(buf,256,stdin); 
    if (send(sockfd, buffer,n, 0) == -1) perror("error in sending MSG to server\n");
}

else  if (FD_ISSET(sockfd, &readSet))  // receives data from server,print it on StdOutput
{  
    bzero(buf,256);
    int nbytes = recv(sockfd, buf, 256,0);
    if(nbytes<=0)
    {
        perror("recv error from server \n");
        exit(0);
    }
}

} //END OF ELSE 
} //end of while
close(sockfd);          
return 0;
4

1 回答 1

6

循环中的select()调用保持不变。这意味着稍后 FD_ISSET() 检查成功。while(1)readSet

解决方案很简单:在每次迭代时清除 fd_set:

FD_ZERO(&master_list_read);
FD_SET(STDIN, &master_list_read);
FD_SET(sockfd, &master_list_read);
// only now the select() returns what you need
retval = select(sockfd+1, &master_list_read, NULL, NULL,&tv);

// use the master_list_read later

简单地复制 fd_set 并不能解决问题。

于 2012-09-17T22:44:53.433 回答