我正在编写服务器应用程序,并且在select
插槽上遇到问题。
即使客户端尝试连接,服务器端select
也始终返回0。当我评论select
并直接转到accept
行时,一切正常(但我的申请被屏蔽)
服务器代码(在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))
答案 0 :(得分:5)
retval = select(1, &fds, NULL, NULL, &tv);
select
的第一个参数应为sockfd + 1
。
答案 1 :(得分:0)
retval = select(1, &fds, NULL, NULL, &tv);
1有点低。尝试:
retval = select(sockfd+1, &fds, NULL, NULL, &tv);