C无限循环中的客户端/服务器失败

时间:2019-04-18 20:15:43

标签: c linux client-server

我正在使用C在Linux中实现客户端/服务器,同时启动客户端和服务器后,服务器正在等待来自客户端的查询,并且这似乎正在运行,但是,我希望服务器能够正常运行能够不断等待查询。然而,它保持循环不断,而不是等待客户端的新查询。

server.c:

int main(void)
{
    int sockfd, newsockfd, portno, clilen, connfd = 0;
    struct sockaddr_in serv_addr, cli_addr;

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
    {
        printf("socket failed\n");
        exit(0);
    }

    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof(int)) < 0)
    {
        printf("setsockopt failed\n");
        exit(0);
    }

    memset((char *) &serv_addr, 0, sizeof(serv_addr));
    portno = 8888;
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);

    if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    {
        printf("bind failed\n");
        exit(0);
    }

    listen(sockfd, 5);

    clilen = sizeof(cli_addr);
    newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
    if (newsockfd < 0)
    {
        printf("accept failed\n");
        exit(0);
    }

    printf("[Server] Connected\n");
    char query[BUFFER_SIZE];
    char result[BUFFER_SIZE];

    while (1)
    {
        connfd = read(newsockfd, query, 20);
        if (connfd > 0)
        {
            printf("success\n");
        }
        connfd = write(newsockfd, "I got your message", 18);
        if (connfd >= 0)
        {
            printf("write back worked\n");
        }
        //close(newsockfd);
        //close(sockfd);
        sleep (1);
    }

    printf("%d", connfd);
    printf("[Server] Done\n");
    return 0;
}

如您所见,我已经尝试添加关闭命令,但是它们似乎要做的就是完全停止连接,这意味着我在第一个命令之后根本无法发送查询。

client.c:

int main(void)
{
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
    {
        printf("socket failed\n");
        exit(0);
    }

    portno = 8888;
    server = gethostbyname("localhost");
    if (server == NULL)
    {
        printf("gethostbyname failed\n");
        exit(0);
    }

    memset((char *)&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    memcpy((void*)&serv_addr.sin_addr.s_addr, (void*)server->h_addr, server->h_length);
    serv_addr.sin_port = htons(portno);

    if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    {
        printf("connect failed\n");
        exit(0);
    }

    printf("[Client] Connected\n");

    char query[BUFFER_SIZE];
    char result[BUFFER_SIZE];

    while (1)
    {
        memset(query, 0, BUFFER_SIZE);
        fgets(query, BUFFER_SIZE, stdin);
        if(strncmp(query, "exit", 4) == 0)
        {
            break;
        }

        n = write(sockfd, query, sizeof(query));
        if (n < 0) {
            printf("can't write\n");
        }

        bzero(result, BUFFER_SIZE);
        n = read(sockfd, result, BUFFER_SIZE);
        if (n < 0) {
            printf("can't read back from server\n");
        }

        printf("%s\n", result);
        sleep(1);
    }
    close(sockfd);
    return 0;
}

使用此代码,服务器将继续运行并无限地写回客户端“我收到了您的消息”。我该如何等待? 尝试添加close(newsockfd),但一旦这样做,它只能让我与服务器联系一次,并且客户端的第二次查询失败。

1 个答案:

答案 0 :(得分:2)

问题是客户端中的问题:

n = write(sockfd, query, sizeof(query));

sizeof(query)BUFFER_LEN相同。服务器运行时:

connfd = read(newsockfd, query, 20);

对于客户端发送的每个查询,它将成功BUFFER_LEN / 20次。这不是无限循环,但是如果BUFFER_LEN很大,它将重复很多次。

客户端不应发送整个query缓冲区,而仅发送从stdin读取的行。它应该做:

n = write(sockfd, query, strlen(query));

还请注意,无法保证服务器中对read()的单个调用将读取客户端编写的所有内容,它可能会拆分成多个调用。由于您输入的内容以换行符分隔(因为在客户端使用fgets()),因此您应该继续阅读直到获得换行符为止。

有关更多信息,请参见difference between sizeof and strlen in c