0
I am trying to make a chat using Pipes (chat between server and client). I made an output condition, in case the server wants to quit/turn off the chat writes "quit" on the client in the same way. Making "quit" from the server is working, however from the client no. My code is this:
client. c
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <pthread.h>
#include <string.h>
#include <fcntl.h>
#include "fifo.h"
int main()
{
int readfd, writefd;
umask(0);
if((writefd=open(FIFO1, 1))<0)
printf("client: Erro a abrir write fifo\n");
if((readfd=open(FIFO2, 0))<0)
printf("client: Erro ao abrir read fifo\n");
char name[20], mensagem[200], OtherName[20];
printf("Bem-vindo ao chat!\n");
printf("Introduza o seu nome: ");
fgets(name, 20, stdin);
printf("Introduza quit caso deseja fechar o chat\n");
if(fork()==0)
{
while(1)
{
fgets(mensagem, 200, stdin);
printf("\n");
if(strncmp(mensagem, "quit", 4) == 0)
exit(0);
write(writefd, name, 20);
write(writefd, mensagem, 200);
}
}
else
{
while(1)
{
read(readfd, OtherName, 20);
read(readfd, mensagem, 200);
printf("\n%s -->%s\n", OtherName, mensagem);
}
}
close(readfd);
close(writefd);
if(unlink(FIFO1)<0)
printf("client: não foi possível fazer unlink\n");
if(unlink(FIFO2)<0)
printf("client: não foi possível fazer unlink\n");
return 0;
}
server. c
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <pthread.h>
#include <string.h>
#include <fcntl.h>
#include "fifo.h"
int main()
{
int readfd, writefd;
umask(0);
unlink(FIFO1);
unlink(FIFO2);//unlink dos fifos anteriores
if((mknod(FIFO1, S_IFIFO | PERMS, 0))<0)
printf("Erro a criar o fifo\n");
if((mknod(FIFO2, S_IFIFO | PERMS, 0))<0)//criação de fifos
printf("Erro a criar o fifo\n");
if((readfd=open(FIFO1, 0))<0)//abrir o fifo em modo de leitura
printf("Erro ao abrir read fifo\n");
if((writefd=open(FIFO2, 1))<0)//abrir o fifo em modo de escrita
printf("Erro ao abrir write fifo\n");
char name[20], mensagem[200], OtherName[20];
printf("Bem-vindo ao chat!\n");
printf("Introduza o seu nome: ");
fgets(name, 20, stdin);//nome do user no chat
printf("Introduza quit caso deseja fechar o chat\n");
if(fork()==0)//se fork()==0, então o server vai receber uma mensagem
{
while(1)
{
read(readfd, OtherName, 20);//lê o nome do outro user
read(readfd, mensagem, 200);//lê a mensagem do outro user
printf("\n%s -->%s\n", OtherName, mensagem);//escreve a mensagem
}
}
else//se o servidor vai enviar uma mensagem
{
while(1)
{
fgets(mensagem, 200, stdin);//user introduz a mensagem
printf("\n");
if(strncmp(mensagem, "quit", 4) == 0)
exit(0);
write(writefd, name, 20);//escrever o nome para o pipe de escrita
write(writefd, mensagem, 200);//escrever a mensagem
}
}
close(readfd);
close(writefd);
return 0;
}
No client. c, no seguinte if:
if(strncmp(mensagem, "quit", 4) == 0)
exit(0);
The program enters if but does not execute instruction. Why does this happen? Thank you!
sera' that there is no ' n' at the end of message? Sorry, I am not accentuated on my Linux.
– lemoce
What do you mean? I don’t understand @lemoce
– Gazelle
Are you sure you enter if, try to put a pause inside if... I think what the lemoce meant is that if the message is not " quit n" it is going with a line break.. then it is not entering if, because the message is bigger.
– Matheus Francisco
already tested the if, it really enters the if so I don’t think the error is there @Matheusfrancisco
– Gazelle
@Gazelle, the pitch is that you have two processes. Father and son. The parent process will wait until the child finishes, s'o that the son is in an infinite loop. I’m sure, but you have to send an end message to the process, son. I’ll run some tests.
– lemoce