I have the following issue. I have two clients each one has one pipe for reading called .name_of_client and I have a pipe for writing to the server with constant name.
When client 1 sends a message to the server then sends a message to same client (possible success message) but when I tried to send to client 2 the server gets trapped , it can't receive another message , and client2 must only read a pipe if have something on them I tried while(!feof(fd))
but it doesn't appear to work. Am I using the wrong function for the job?
My code only necessary to get more information of what I'm doing: It's only a essential part of my code server:
void process_msg(char* s){
char* from;
char* to;
char* msg;
strtok(s,":");
from = strtok(NULL,",");
strtok(NULL,":");
to = strtok(NULL,",");
strtok(NULL,":");
msg = strtok(NULL,";");
char path[50];
char path2[50];
sprintf(path,".%s",from);
sprintf(path2,".%s",to);
char buffer[250];
if((fp = fopen(path, "w")) == NULL) {
perror("fopen");
exit(1);
}
if(strcmp(from,to)==0){
sprintf(buffer,"não podes enviar uma mensagem a ti proprio");
fputs(buffer,fp);
fclose(fp);
return;
}
int is_online =0;
int i=0;
for(i=0;i<tamanho;i++)
if(strcmp(list[i].user,to)==0)
is_online =1;
if(!is_online){
sprintf(buffer,"%s nao esta online!\n",to);
fputs(buffer,fp);
fclose(fp);
return;
}
else
printf("-> %s mandou uma mensagem a %s\n",from,to);
fputs("ENVIADO",fp);
fclose(fp);
printf("%s",path2);
if((fp = fopen(path2, "w")) == NULL) {
perror("fopen");
exit(1);
}
sprintf(buffer,"%s > %s\n",from,msg);
fputs(buffer,fp);
fclose(fp);
}
client:
int main(int argc, char *argv[])
{
FILE *fp;
if(argc==1){
printf("Não escolheu utilizador\n");
exit(0);
}
char* password;
sprintf(user,"%s",argv[1]);
printf("Password:\n");
password = get_password();
char mensagem[250];
if((fp = fopen(FIFO_FILE, "w")) == NULL) {
perror("fopen");
exit(1);
}
strcpy(path,".");
strcat(path,user);
umask(0);
mknod(path, S_IFIFO|0666, 0);
sprintf(mensagem,"U:%s,%s;",user,password);
fputs(mensagem, fp);
fclose(fp);
if((fp = fopen(path, "r")) == NULL) {
perror("fopen");
exit(1);
}
fgets(mensagem,250,fp);
fclose(fp);
if(strcmp(mensagem,"SUCCESS")==0){
printf("\n\nUtilizador %s autenticado. Pode começar a usar o sistema!\n\n",user);
}
else if(strcmp(mensagem,"FAIL!")==0){
printf("Password incorrecta!\n");
exit(1);
}
while(1){
printf("name\n"); //DEBUG
if((fp = fopen(path, "r")) == NULL) {
perror("fopen");
exit(1);
}
printf("name2");//DEBUG
while(fgets(mensagem,250,fp)!=NULL){
printf("%s",mensagem);
}
fclose(fp);
printf("**Menu**\n1) Listar utilizadores online\n2) Mandar SMS a um utilizador\n3) Logout\n\n");
char opcao;
op:deactivate_input();
scanf("%c",&opcao);
activate_input();
if(opcao!='1' && opcao!='2' && opcao!='3')
goto op;
if(opcao=='1'){
if((fp = fopen(FIFO_FILE, "w")) == NULL) {
perror("fopen");
exit(1);
}
sprintf(mensagem,"W:%s;",user);
fputs(mensagem, fp);
fclose(fp);
if((fp = fopen(path, "r")) == NULL) {
perror("fopen");
exit(1);
}
printf("////ONLINE LIST\\\\\\\\");
while(!feof(fp)){
fgets(mensagem, 250, fp);
printf("%s\n",mensagem);
}
printf("//// END\\\\\\\\\n");
fflush(fp);
fclose(fp);
}
else if(opcao=='2'){
if((fp = fopen(FIFO_FILE, "w")) == NULL) {
perror("fopen");
exit(1);
}
char tmp[50];
printf("Para quem vais mandar a mensagem?\n");
scanf("%s",tmp);
getchar();
printf("Escreve a mensagem?\n");
//scanf("%s",mensagem);
strcpy(mensagem,"");
char s[50];
fgets(mensagem,250,stdin);
char buffer[500];
sprintf(buffer,"F:%s,T:%s,M:%s;",user,tmp,mensagem);
fputs(buffer, fp);
fclose(fp);
if((fp = fopen(path, "r")) == NULL){
perror("fopen");
exit(1);
}
fgets(mensagem, 250, fp);
fclose(fp);
if(strcmp(mensagem,"ENVIADO")==0){
}else{
printf("%s\n",mensagem);
}
}
else if(opcao=='3'){
if((fp = fopen(FIFO_FILE, "w")) == NULL) {
perror("fopen");
exit(1);
}
sprintf(mensagem,"R:%s;",user);
fputs(mensagem, fp);
fclose(fp);
break;
}
}
return(0);
}
UPDATE: I used two strings to debug but name2 is never printed. I am sure the fp is not busy but it never enters on f((fp = fopen(path, "r")) == NULL) Does someone know why it stops before it enters on while cause prints name1 but I have nothing in between.