FD_ISSET 返回 0
我在这里看到过类似的帖子,但我仍然找不到答案。 我正在尝试在 Windows 上用 C 语言执行此接收函数:
void receiving(int server_fd){
struct sockaddr_in address;
int valread;
char buffer[2000] = {0};
int addrlen = sizeof(address);
fd_set current_sockets, ready_sockets;
//Initialize my current set
FD_ZERO(¤t_sockets);
FD_SET(server_fd, ¤t_sockets);
int k = 0;
while (1)
{
k++;
ready_sockets = current_sockets;
if (select(FD_SETSIZE, &ready_sockets, NULL, NULL, NULL) < 0)
{
perror("Error");
exit(EXIT_FAILURE);
}
for (int i = 0; i < FD_SETSIZE; i++)
{
if (FD_ISSET(i, &ready_sockets))
{
if (i == server_fd)
{
int client_socket;
if ((client_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0)
{
perror("accept");
exit(EXIT_FAILURE);
}
FD_SET(client_socket, ¤t_sockets);
}
else
{
valread = recv(i, buffer, sizeof(buffer), 0);
printf("IP address recv2 is: %s\n", inet_ntoa(address.sin_addr));
printf("\n%s\n", buffer);
FD_CLR(i, ¤t_sockets);
}
}
}
if (k == (FD_SETSIZE * 2))
break;
}
}
不幸的是,程序没有进入 if(FD_ISSET(i,&ready_socket))。 FD_ISSET 始终返回 0。 有谁知道为什么? 谢谢
I've seen a similar post here, but I still can't figure out the answer.
I’m trying to do this receiving function in C on Windows:
void receiving(int server_fd){
struct sockaddr_in address;
int valread;
char buffer[2000] = {0};
int addrlen = sizeof(address);
fd_set current_sockets, ready_sockets;
//Initialize my current set
FD_ZERO(¤t_sockets);
FD_SET(server_fd, ¤t_sockets);
int k = 0;
while (1)
{
k++;
ready_sockets = current_sockets;
if (select(FD_SETSIZE, &ready_sockets, NULL, NULL, NULL) < 0)
{
perror("Error");
exit(EXIT_FAILURE);
}
for (int i = 0; i < FD_SETSIZE; i++)
{
if (FD_ISSET(i, &ready_sockets))
{
if (i == server_fd)
{
int client_socket;
if ((client_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0)
{
perror("accept");
exit(EXIT_FAILURE);
}
FD_SET(client_socket, ¤t_sockets);
}
else
{
valread = recv(i, buffer, sizeof(buffer), 0);
printf("IP address recv2 is: %s\n", inet_ntoa(address.sin_addr));
printf("\n%s\n", buffer);
FD_CLR(i, ¤t_sockets);
}
}
}
if (k == (FD_SETSIZE * 2))
break;
}
}
Unfortunately, the program doesn’t enter in the if(FD_ISSET(i,&ready_socket)). FD_ISSET always returns 0.
Does anyone know why ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论