我正在尝试使用 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;