c - 退格字符被添加到 FIFO 中

标签 c unix ipc fifo

我正在学习 FIFO,并编写了一个迭代服务器来接收来自多个客户端的请求。客户端通过写入服务器众所周知的 fifo 来请求文件。服务器从它的 FIFO 中读取并将请求文件的内容放入客户端读取的新 FIFO 中。我运行服务器。当我第一次运行客户端时,一切正常,客户端读取文件的内容。当我第二次运行客户端时,来自客户端的消息以退格字符为前缀。我不知道这个退格键是从哪里来的。有任何想法吗? 这是服务器代码:

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

#define SERVFIFO "/tmp/fifo.serv"
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)

int main(int argc, char** argv) {
    int readfifo, dummywrite, filefd, writefd, n;
    int clientpid;
    char buff[MAXLINE], *spaceptr, fifoname[MAXLINE];

    if (mkfifo(SERVFIFO, FILE_MODE) < 0 && errno != EEXIST) {
        printf("Can't create %s", SERVFIFO);
    }

    readfifo = open(SERVFIFO, O_RDONLY, 0);
    dummywrite = open(SERVFIFO, O_WRONLY, 0);

    while ((n = Readline(readfifo, buff, MAXLINE)) > 0) {
        printf("Read data from the fifo:%s and the length is:%d and the character is:%d\n", buff, strlen(buff), buff[0]);

        if (buff[n - 1] == '\n') {
            n--;
            printf("I am also omitting the newline\n");
        }

        buff[n] = '\0';
        printf("Buff just after read is:%s and length is %d", buff, strlen(buff));

        if ((spaceptr = strchr(buff, ' ')) == NULL) {
            printf("Bad request from client");
            continue;
        }

        printf("Found the space:%c\n", *(spaceptr + 1));
        *spaceptr++ = '\0';
        printf("The value of buffer now is:%s and the length of buffer is:%d and the culprit is %d\n", buff, strlen(buff), *(buff + 0));
        clientpid = atol(buff);
        printf("The client pid is %ld\n", clientpid);
        snprintf(fifoname, sizeof(fifoname), "/tmp/fifoname.%ld", clientpid);

        if (mkfifo(fifoname, FILE_MODE) < 0 && errno != EEXIST) {
            perror("Can't create the fifo");
            continue;
        }

        printf("Successfully created fifo %s for client\n", fifoname);

        if ((writefd = open(fifoname, O_WRONLY, 0)) < 0) {
            printf("Cannot open %s", fifoname);
            continue;
        }

        if ((filefd = open(spaceptr, O_RDONLY, 0)) < 0) {
            printf("Error opening file\n");
            continue;
        }
        else {
            while ((n = read(filefd, buff, MAXLINE))) {
                write(writefd, buff, n);
            }

            close(filefd);
            close(writefd);
        }
    }
}

这是客户端代码:

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

#define SERVER_FIFO "/tmp/fifo.serv"
#define MAXLINE 100
#define MSG "%ld sup.c"
#define READFIFO "/tmp/fifoname.%ld"
int main(int argc, char** argv) {
    int writefifo, readfifo, n;
    char buff[MAXLINE];
    pid_t self_pid = getpid();
    printf("Started client with PID:%ld\n", self_pid);
    writefifo = open(SERVER_FIFO, O_WRONLY, 0);
    snprintf(buff, sizeof(buff), MSG, self_pid);
    printf("The message to be written to the server is:%s and the length of the message is %d\n", buff, strlen(buff));

    if ((n = write(writefifo, buff, sizeof(buff))) != sizeof(buff)) {
        perror("Unable to write to server fifo");
        exit(0);
    }

    printf("Message written to the server; Waiting to read contents\n");
    snprintf(buff, sizeof(buff), READFIFO, self_pid);
    readfifo = open(buff, O_RDONLY, 0);

    while ((n = read(readfifo, buff, MAXLINE))) {
        write(STDOUT_FILENO, buff, n);
    }

    close(readfifo);
    close(writefifo);
    unlink(buff);
}

最佳答案

也许这是您真实程序的截断版本,因为它不会显示您描述的行为。几件事:

(1) 在服务器中

readfifo = open(SERVFIFO, O_RDONLY, 0);
dummywrite = open(SERVFIFO, O_WRONLY, 0);

我假设您知道如果您在另一端没有任何东西的情况下打开众所周知的 fifo,打开将阻塞。这仍然会阻塞 O_RONLY 打开,因为在正常情况下,您将在任何客户端之前运行服务器。如果您想避免阻塞,只需将其以读/写方式打开一次即可。

(2) 在客户端

snprintf(buff, sizeof(buff), READFIFO, self_pid);
readfifo = open(buff, O_RDONLY, 0);

这不是按照你的想法去做。您的客户端将消息写入服务器的 fifo,服务器 使用 mkfifo 创建客户端的 fifo。由于客户端首先运行,因此当您执行此 open 操作时,fifo 将不存在。实际上,您是在尝试以只读方式打开一个普通文件(不存在)。您无法读取不存在的文件,因此此打开 失败,但由于您不检查打开状态,所以您不知道它。

(3) 当你用完它们时取消链接你的 FIFO。

关于c - 退格字符被添加到 FIFO 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18501830/

相关文章:

c++ - 奇怪的程序挂起,这在调试中意味着什么?

c++ - Inflate HTML gZIP 内容时出现错误 `invalid distance too far back`

linux - Grep 具有一定字符数的行,包括换行符

python - 阻塞套接字并选择

c - Lua错误没有传递 bool 值

C 未收到来自定时器的信号

mysql - 在 spring security unix 中,即使详细信息正确,也重定向到authentication-failure-url

c - 我是否需要在 fork 之后在父进程中等待()?

c - 尝试使用 semget() 获取信号量集,但我不断收到 EEXIST 错误,即使在创建新 key 后也是如此

java - 这是检查 Postgres 是否提交事务 id 的正确方法吗