Hey guys,
I'm sending a phrase through a named pipe and my server receives it well but when printing the phrase he prints it well but then prints some garbage aswell
This is my client :
Code:
#include <sys/types.h>#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#define MAX_LINE 80
int main(int argc, char** argv) {
char line[MAX_LINE];
if(argc < 2) printf("Invocação do programa cliente mal realizada!\n");
else if(strncmp(argv[1],"add",strlen(argv[1]))==0)
{
if(argc != 2) printf("O comando add foi mal invocado.\n Exemplo: ./cliente add n");
else
{ int pipe = open("trabfifo", O_WRONLY);
// get a line to send
printf("Enter line: ");
fgets(line, MAX_LINE, stdin);
line[strlen(line)-1] = '\0';
// actually write out the data and close the pipe
write(pipe, line, strlen(line));
}
}
else if(strncmp(argv[1],"help",strlen(argv[1])) == 0)
{
ajuda();
}
else printf("Comando Inexistente.\n");
return 1;
}
And now my server code :
Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define MAX_LINE 80
int main(int argc, char** argv) {
int pipe;
int n;
char line[MAX_LINE];
mkfifo("trabfifo",0646);
// abrir um named pipe
pipe = open("trabfifo", O_RDONLY);
// ler a informação e fechar o pipe
n = read(pipe, line, strlen(line));
printf("%s \n",line);
printf("%d \n",n);
close(pipe);
return 0;
}
After sending the phrase 'ola' the result is the following :
ola
�
4
Why is that garbage appearing in the mid ?
Thanks.