You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于FIFO与结构体的Server-Client程序循环交互功能异常排查求助

基于FIFO与结构体的Server-Client程序循环交互功能异常排查求助

我现在能通过FIFO传递结构体数据,但想实现客户端和服务端持续来回交互,直到客户端选择退出程序。我尝试用while循环实现,但就是不正常——程序能运行,但数据没法通过FIFO传递;去掉while循环的话,数据就能正常传递。有没有大佬能给点帮助或者提示?

客户端代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>

struct Client {
    int clientNum;
    int systemCall;
    int num;
    int result;
} client;

int main(int argc, char* argv[]) {
    if (mkfifo("ClientFifo", 0777) == -1) {
        if (errno != EEXIST) {
            printf("Could not create fifo file\n");
            return 1;
        }
        printf("Opened\n");
        int fd = open("ClientFifo", O_RDONLY);
        if (fd == -1) {
            return 1;
        }
        // while statement
        while(1) {
            printf("Reading\n");
            read(fd, &client, sizeof(client));
            printf("Client number:  %d\n", client.clientNum);
            printf("System call number:  %d\n", client.systemCall);
            printf("Parameter number %d\n", client.num);
            printf("Result %d\n", client.result);
            close(fd);
        } // while
        return 0;
    } // end if
} // end main

服务端代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>

struct Client {
    int clientNum;
    int systemCall;
    int num;
    int result;
} client;

struct Client get_student_data()
{
    struct Client s;
    s.clientNum = getpid();
    printf("Client Number:%d", s.clientNum);
    printf("\n");
    printf("Enter System Call number:");
    scanf("%d", &s.systemCall);
    printf("Enter Number:");
    scanf("%d", &s.num);
    printf("system call:%d\n", s.systemCall);
    switch (s.systemCall)
    {
        case 1:
            printf("Youre in case 1\n");
            s.result = s.num * s.num;
            break;
        case 2:
            printf("Youre in case 2\n");
            s.result = s.num * s.num * s.num;
            break;
        default:
            printf("INVALID CHOICE\n");
            s.result = 0;
            break;
    }
    return s;
}

int main(int argc, char* argv[]) {//main
    // makes fifo
    if(mkfifo("ClientFifo", 0777) == -1) {
        // error message in case fifo isnt created
        if(errno != EEXIST) {
            printf("Could not create fifo file\n");
            return 1;
        }// end error checking
        // area to do work
        printf("\n");
        // while Statement
        while(client.systemCall != 0) {
            struct Client c1 = get_student_data();
            printf("\nClient number: %d\n",c1.clientNum);
            printf("System call number: %d\n",c1.systemCall);
            printf("Paramet number: %d\n",c1.num);
            printf("Result number: %d\n",c1.result);
            // opens and closes FIFO
            printf("Opening...\n");
            int fd = open("ClientFifo", O_WRONLY);
            printf("Opened\n");
            if(write(fd, &c1, sizeof(c1)) == -1) {
                return 2;
            }
            // } // ends write
            // Closes fd
            printf("Written..\n");
            close(fd);
        }
        return 0;
    } // ends if fifo
} // ends main

我已经把while循环放在代码里标注的位置了,但程序就是不能正常运行。

备注:内容来源于stack exchange,提问作者jjmoe43

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 11:59:35