2

我有两个程序,服务器和客户端。服务器应该读取一个文件,然后通过命名管道将其内容发送到客户端。但是我的服务器只从文件中读取两个字符,然后退出。这段代码有什么问题?

服务器.c:

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

#define FIFO_NAME "american_maid"

int main(void)
{
    char line[300];
    int num, fd;
    FILE *fp;
    fp = fopen("out.txt","r");

    mknod(FIFO_NAME, S_IFIFO | 0666, 0);

    printf("waiting for readers...\n");
    fd = open(FIFO_NAME, O_WRONLY);
    printf("got a reader--type some stuff\n");

    while (fgets(line, sizeof(line), fp)) {
        if ((num = write(fd, line, strlen(line))) == -1)
            perror("write");
        else
            printf("speak: wrote %d bytes\n", num);
    }

    fclose(fp);

    return 0;
}

客户端.c:

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

#define FIFO_NAME "american_maid"

int main(void)
{
    char s[300];
    int num, fd;

    mknod(FIFO_NAME, S_IFIFO | 0666, 0);

    printf("waiting for writers...\n");
    fd = open(FIFO_NAME, O_RDONLY);
    printf("got a writer\n");

    do {
        if ((num = read(fd, s, 300)) == -1)
            perror("read");
        else {
            s[num] = '\0';
            printf("tick: read %d bytes: \"%s\"\n", num, s);
        }
    } while (num > 0);

    return 0;
}
4

2 回答 2

1

当我使用命令序列运行下面显示的代码时:

$ ln -s server.c out.txt
$ ./client &
$ ./server
$

我得到了客户端程序打印的源代码的副本。同样,当我使用以下命令运行命令时:

$ ./server &
$ ./client
$

修改后的代码并没有太大的修改。它避免了do { } while(...)循环——它们很少真正有益——并且非常小心不要溢出缓冲区。该代码还删除了多余的标题。

服务器.c

#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define FIFO_NAME "american_maid"

int main(void)
{
    const char infile[] = "out.txt";
    FILE *fp = fopen(infile, "r");

    if (fp == 0)
    {
        fprintf(stderr, "Failed to open %s for reading", infile);
        return(1);
    }

    mknod(FIFO_NAME, S_IFIFO | 0666, 0);

    printf("waiting for readers...\n");
    int fd = open(FIFO_NAME, O_WRONLY);
    if (fd > 0)
    {
        char line[300];
        printf("got a reader--type some stuff\n");

        while (fgets(line, sizeof(line), fp))
        {
            int len = strlen(line);
            int num = write(fd, line, len);
            if (num != len)
                perror("write");
            else
                printf("speak: wrote %d bytes\n", num);
        }
        close(fd);
    }

    fclose(fp);

    return 0;
}

客户端.c

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

#define FIFO_NAME "american_maid"

int main(void)
{
    const char outfile[] = "client.out";
    FILE *fp = fopen(outfile, "w");

    if (fp == 0)
    {
        fprintf(stderr, "Failed to open %s for writing\n", outfile);
        return 1;
    }

    printf("waiting for writers...\n");
    mknod(FIFO_NAME, S_IFIFO | 0666, 0);

    int fd = open(FIFO_NAME, O_RDONLY);
    if (fd > 0)
    {
        int num;
        char s[300];
        printf("got a writer\n");

        while ((num = read(fd, s, sizeof(s))) > 0)
        {
            printf("tick: read %d bytes: \"%.*s\"\n", num, num, s);
            fprintf(fp, "%.*s", num, s);
        }

        close(fd);
    }
    fclose(fp);

    return 0;
}

请注意,此版本将其输出写入文件client.out;即使给定一个包含一些很长行要处理的文件(2049 字节,包括末尾的换行符),输出 inclient.out与 .in 中的输入完全匹配out.txt

于 2013-04-12T14:37:43.150 回答
0

mknod(FIFO_NAME, S_IFIFO | 0666, 0);从文件中删除该行client.c。然后程序将按预期工作。服务器将创建一个文件并将文件内容发送给fifo。

于 2013-04-11T19:51:37.330 回答