如何通过C中的命名管道发送文件?

时间:2013-04-11 19:27:27

标签: c client-server named-pipes fifo mkfifo

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

server.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;
}

client.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;
}

2 个答案:

答案 0 :(得分:1)

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

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

我得到了客户端程序打印的源代码的副本。类似地,当我使用:

运行命令时
$ ./server &
$ ./client
$

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

server.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;
}

client.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字节,包括末尾的换行符),client.out中的输出也与out.txt中的输入完全匹配。

答案 1 :(得分:0)

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