Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么select()没有在我的代码中返回?_C_Sockets_Networking - Fatal编程技术网

为什么select()没有在我的代码中返回?

为什么select()没有在我的代码中返回?,c,sockets,networking,C,Sockets,Networking,我需要帮助调试一个多人游戏的一些代码,其中一个服务器充当主服务器,多个客户端。我想使用select和FD_Set功能将这些多个客户端连接到服务器 客户机连接到服务器后,我将其文件描述符保存在一个数组中,并使用select检查文件中的数据是否可用。如果是,请读取数据,否则我要向所有客户端广播消息。这将在循环中重复运行。每一秒,我都要向所有客户广播一条消息。同时,我也想从客户端接收数据。因此,我正在使用select函数检查数据的可用性 这是我的密码: 服务器c: 客户c: 向客户端传递一个IP地址

我需要帮助调试一个多人游戏的一些代码,其中一个服务器充当主服务器,多个客户端。我想使用select和FD_Set功能将这些多个客户端连接到服务器

客户机连接到服务器后,我将其文件描述符保存在一个数组中,并使用select检查文件中的数据是否可用。如果是,请读取数据,否则我要向所有客户端广播消息。这将在循环中重复运行。每一秒,我都要向所有客户广播一条消息。同时,我也想从客户端接收数据。因此,我正在使用select函数检查数据的可用性

这是我的密码:

服务器c:

客户c:

向客户端传递一个IP地址


这是行不通的。调试服务器时,它会在select处等待,不会接收或发送任何内容。可能是什么问题?

可能是因为您没有使用正确的端口进行侦听。成员中的sockaddr_需要按网络字节顺序配置:

memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(PORT);

上面的示例是select的工作原理,在while循环中,必须设置timeval结构和套接字sd

 while (1) {

    FD_ZERO(&readfds);
    FD_SET(sd, &readfds);

    tv.tv_sec = 0;
    tv.tv_usec = 0;

    rv = select(sd + 1, &readfds, NULL, NULL, &tv);

    if (rv == 1) {        
         /*recvfrom*/

    }
 }
您似乎检查了FD_是否为ISSETAR[2],但从未在arr[2]中的描述符上调用过FD_集,并且忘记检查arr[0]和arr[1]上的FD_ISSET
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(PORT);
 while (1) {

    FD_ZERO(&readfds);
    FD_SET(sd, &readfds);

    tv.tv_sec = 0;
    tv.tv_usec = 0;

    rv = select(sd + 1, &readfds, NULL, NULL, &tv);

    if (rv == 1) {        
         /*recvfrom*/

    }
 }
In security firstly FD_ZERO(&readfds)
and you not have to FDSET your clientfds every while loop. You can set one time before the while.

Id u check the client side and clients send data succesfully check the this line 

for (i = 0; i < 2; i++) {
                if (FD_ISSET(arr[2], &fds)) {
                    ret = recvfrom(arr[i], (void*) &buffer, sizeof(buffer), 0,
                            NULL, NULL);

In your code FD_ISSET(arr[2], &fds)    you can change the like this

FD_ISSET(arr[i], &fds)   because your for loop do nothing here if you can not change the arr[i]