2010-11-29 62 views
4

我运行一个全双工服务器/客户端的代码,我在Oracle的网站上找到:为什么我在这个管道上收到垃圾?

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

HAHAHA0 $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)); 
} 

fullduplex.h

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

回答

2

你的意思是:

rdbuf[numread] = '\0'; 

ff_server.c中的buf具有相同的问题。

+0

谢谢,我做了两个更改,垃圾都没有了,但现在我在响应中得到一个尾随0。 – andandandand 2010-11-29 23:04:38

+0

喜欢:HAHAHA0的回复 – andandandand 2010-11-29 23:06:18

+0

@omgzor对不起,没有看到代码,我只能猜测。而当你说“响应”你的意思是在fd_client.c或fd_server.c? – 2010-11-29 23:25:06

2

此:

buf[numread] = '0'; 

是错误的。你想:

buf[numread] = '\0'; 

(同样的,rdbuf[numread] = '0';

-1

这些线路产生不好的输出:

buf[numread] = '0'; 
printf("Full Duplex Server : Read From the pipe : %s \n", buf); 

首先,buf[numread] = '0';覆写空终止符。
在覆盖之后,printf(%s)不知道停止打印的位置。

null结束符告诉C字符串结束的位置。
重写它后,C不再知道字符串的末尾在哪里,并打印出您的字符串“HAHAHA”,但在此之后继续打印垃圾。