我正在编写服务器应用程序,但在套接字上的select操作有问题。
即使客户端尝试连接,服务器端也始终返回0。当我注释select并直接转到select行时,一切正常(但我的应用程序被阻止)
服务器代码(C):

int sockfd = socket(AF_INET, SOCK_STREAM, 0);
memset((char *) &serv_addr, 0, sizeof (serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(System_->commParams.port);
bind(sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr));
fd_set fds;
timeval tv;
FD_ZERO(&fds);
FD_SET(sockfd, &fds);
int retval = 0;
tv.tv_sec = 1;
tv.tv_usec = 0;
listen(sockfd, 5);
while (retval == 0)
{
   retval = select(1, &fds, NULL, NULL, &tv);
   tv.tv_sec = 1;
   tv.tv_usec = 0;
   FD_ZERO(&fds);
   FD_SET(sockfd, &fds);
}
unsigned int clilen = sizeof (cli_addr);
int newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);

客户端代码(在python中):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((RemoteIP, Port))

最佳答案

retval = select(1, &fds, NULL, NULL, &tv);

select的第一个参数应该是sockfd + 1

10-08 04:08