c - 为什么我在这个管道上得到垃圾?

标签 c unix named-pipes

我正在运行在 Oracle 网站上找到的全双工服务器/客户端代码:

当写 ./fd_client 哈哈哈我得到这样的东西:

哈哈哈0�$0

大写字母没问题(这是它应该返回的服务器)但是,我该死的如何避免尾随的垃圾?

fd_client.c

#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "fullduplex.h" /* For name of the named-pipe */


int main(int argc, char *argv[])
{
    int wrfd, rdfd, numread;
    char rdbuf[MAX_BUF_SIZE];

    /* Check if an argument was specified. */

    if (argc != 2) {
        printf("Usage : %s \n", argv[0]);
        exit (0);
    }

    /* Open the first named pipe for writing */
    wrfd = open(NP1, O_WRONLY);

    /* Open the second named pipe for reading */
    rdfd = open(NP2, O_RDONLY);

    /* Write to the pipe */
    write(wrfd, argv[1], strlen(argv[1]));

    /* Read from the pipe */
    numread = read(rdfd, rdbuf, MAX_BUF_SIZE);

    rdbuf[numread] = '0';

    printf("Full Duplex Client : Read From the  Pipe : %s\n", rdbuf);

    return 0;
}

fd_server.c

#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "fullduplex.h" /* For name of the named-pipe */

int main(int argc, char *argv[])
{
    int rdfd, wrfd, ret_val, count, numread;
    char buf[MAX_BUF_SIZE];

    /* Create the first named - pipe */
    ret_val = mkfifo(NP1, 0666);

    if ((ret_val == -1) && (errno != EEXIST)) {
        perror("Error creating the named pipe");
        exit (0);
    }

    ret_val = mkfifo(NP2, 0666);

    if ((ret_val == -1) && (errno != EEXIST)) {
        perror("Error creating the named pipe");
        exit (0);
    }

    /* Open the first named pipe for reading */
    rdfd = open(NP1, O_RDONLY);

    /* Open the second named pipe for writing */
    wrfd = open(NP2, O_WRONLY);

    /* Read from the first pipe */
    numread = read(rdfd, buf, MAX_BUF_SIZE);

    buf[numread] = '0';

    printf("Full Duplex Server : Read From the pipe : %s \n", buf);

    /* Convert to the string to upper case */
    count = 0;
    while (count < numread) {
        buf[count] = toupper(buf[count]);
        count++;
    }

    /* 
     * Write the converted string back to the second 
     * pipe 
     */    
    write(wrfd, buf, strlen(buf));
}

全双工.h

#define NP1     "/tmp/np1"
#define NP2     "/tmp/np2"
#define MAX_BUF_SIZE    255

最佳答案

你的意思是:

rdbuf[numread] = '\0';

fd_server.c中的buf也有同样的问题。

关于c - 为什么我在这个管道上得到垃圾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4308870/

相关文章:

c - 用 C 中充满数字的 char 数组填充 int 数组(char 数组到 int 数组)

c - 一些初学C的问题

bash - 检查命名管道/FIFO 是否打开写入

c# - 远程关闭后保持管道打开的最佳方法

linux - 使用命名管道将一个 shell 脚本的输出作为另一个输入

c - win 检查功能在用 c 编写的连接四中无法正常工作;

c - 如何替换C中的某个字符

python - 如何在 python 中处理 cpu 缓存(或调用一次函数的最快方法)

c - 为什么 ICMP ECHO 数据长度与原始 ping 源代码中的 timeval 大小相比?

linux - 如何使 bash 脚本按特定编号对文件夹中的文件进行分类