使用select()的客户端-服务器程序出现问题

时间:2019-06-18 14:28:20

标签: c linux sockets select

我目前正在学习套接字编程,并且正在尝试制作一个可以处理多个客户端的服务器。我已经用谷歌搜索并将一些模板解决方案合并为一个。但是,它不起作用,我也不知道为什么。

您可以在我的代码中看到,我希望有两个客户端通过向服务器发送一些数据来联系它。我将selectFD_ISSET放入了while(1)循环中,以便它始终检查套接字上是否有任何传入的IO操作。我的问题是,如果我同时运行服务器和两个客户端,然后从客户端1发送消息,则只能从客户端1发送消息。这些消息到达服务器,但是如果我以客户端2的身份发送消息,则不会t完全显示在服务器上(即使您在客户端2应用程序中看到“消息已发送”通知)。客户端2同样如此,如果我在客户端2中发送第一条消息,则只有他的消息在服务器上可见,客户端1的消息将被隐藏。

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>

#define SERVER_IP "192.168.201.95"
#define SERVER_PORT 8888

int main() {
    //create server socket
    int master_socket;
    if ((master_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("Failed to create socket.");
        exit(EXIT_FAILURE);
    }

    //configuring server ip and port
    struct sockaddr_in server_address;
    server_address.sin_family = AF_INET;
    server_address.sin_addr.s_addr = INADDR_ANY;
    server_address.sin_port = htons(SERVER_PORT);
    int addrlen = sizeof(server_address);

    //assign address to socket
    if ((bind(master_socket, (struct sockaddr * ) & server_address, sizeof(server_address))) < 0) {
        perror("Failed to bind.");
        exit(EXIT_FAILURE);
    }

    //listen for connections
    if ((listen(master_socket, 3)) < 0) {
        perror("Failed to listen.");
        exit(EXIT_FAILURE);
    }

    //accept incoming connections
    int client1;
    if ((client1 = accept(master_socket, (struct sockaddr * ) & server_address, (socklen_t * ) & addrlen)) < 0) {
        perror("Failed to accept.");
        exit(EXIT_FAILURE);
    }

    int client2;
    if ((client2 = accept(master_socket, (struct sockaddr * ) & server_address, (socklen_t * ) & addrlen)) < 0) {
        perror("Failed to accept.");
        exit(EXIT_FAILURE);
    }

    char message[64];

    fd_set readfds;
    FD_ZERO( & readfds);
    FD_SET(client1, & readfds);
    FD_SET(client2, & readfds);

    struct timeval time;
    time.tv_sec = 0;
    time.tv_usec = 0;

    int fd_size = client2 + 1;

    int state;

    while (1) {
        state = select(fd_size, & readfds, NULL, NULL, NULL);

        if (state == -1) {
            perror("Failed to select.");
            exit(EXIT_FAILURE);
        } else if (state == 0) {
            printf("No data after timeout.");
        } else {
            if (FD_ISSET(client1, & readfds)) {
                recv(client1, & message, sizeof(message), 0);
                printf("Client 1: %s\n", message);
            }
            if (FD_ISSET(client2, & readfds)) {
                recv(client2, & message, sizeof(message), 0);
                printf("Client 2: %s\n", message);
            }
        }
    }

    return 0;
}

0 个答案:

没有答案