vendredi 29 mai 2015

How Can I receive messages from more than one named pipes?

I have to use a fifo in my code.

I use sock to accept new client. For each client I create new thread to send and receive message to him.

In the function of the thread I use fifo to send and receive messages also to another process and here is my code:

 int s_to_c=open(myfifo1,O_WRONLY);
 int c_to_s=open(myfifo2,O_RDONLY);

 char echoBuffer[RCVBUFSIZE];           
 int recvMsgSize; 

 for(;;)
 {   
     bzero(echoBuffer,RCVBUFSIZE);              
     read(c_to_s, echoBuffer, RCVBUFSIZE);  
     write(sock, echoBuffer, strlen(echoBuffer));
     bzero(echoBuffer,RCVBUFSIZE);

     read(sock, echoBuffer, RCVBUFSIZE);
     write(s_to_c,echoBuffer,strlen(echoBuffer));
}

close(c_to_s);
close(s_to_c);
close(sock);

And on the other side (The other process) my code:

int s_to_c=open(myfifo1,O_RDONLY);
int c_to_s=open(myfifo2,O_WRONLY);

char echoBuffer[RCVBUFSIZE];     
int recvMsgSize;
for(;;)
{
    bzero(echoBuffer,RCVBUFSIZE);
    fgets(echoBuffer,RCVBUFSIZE,stdin);
    echoBuffer[strlen(echoBuffer)-1]='\0';

    write(c_to_s, echoBuffer, strlen(echoBuffer));

    bzero(echoBuffer,RCVBUFSIZE);

    read(s_to_c, echoBuffer, RCVBUFSIZE);
    printf("%s\n", echoBuffer);
}

My problem is in this process : s_to_c and c_to_s take always the value(3,4).

So the first client connect correctly sending and receiving his message.

But when the second connect the first client become disable.And the messages of the second client sends and receives to and from the two processes.

Can I have some help please.Should I have to use tags for example??

Aucun commentaire:

Enregistrer un commentaire