简体   繁体   中英

How to send a message to several processes through the same pipe? (C language)

PROJECT:

The forecast station send the data to the control tower wich dispatch the datas to all aircrafts identified by is PID (unknown number of plane, who may enter or leave the zone controlled by the control tower ).

Project Diagram

CURRENTLY:

The Forecast send the data to the Control Tower (wich is listenning on the pipe) then send the data to one plane (the last connected) and resends the data if we change the forecast by resend new info to the control tower.

PROBLEM:

How to make the control tower sends the forecast to all connected plane:

  1. if one new plane connects himself to the control tower
  2. if new forecast --> resend data to all the connected airplanes

     // Librairies de fonction. #include "Extends.h" // fifo_s représente le chemin d'accès a la fifo du controltower. const char fifo_s[] = "Fifo_Server"; // fifo_p représente le chemin d'accès a la fifo des Planes const char fifo_p[] = "Fifo_Planes"; // Structure des données qui seront reçues puis envoyées. typedef struct { char data[1000]; int pid; } T_Data; // Prototype de la fonction STOP semblable a un getchar() mais sans attente. int STOP(); /*******************************/ // Fonction main sans arguments. int main() { int serv = 0, nbOct = 0, dest = 0; char fifo_d[1000]; T_Data controltower; system("clear"); printf("(1) Création des FIFOS\\n"); // Creation de la fifo de transfert du controltower (fifo_controltower). // Et des fifos tampons entre le controltower et les programmes destinataires. if ((mkfifo(fifo_s, 0666) < 0) || (mkfifo(fifo_p, 0666) < 0)) { if (errno != EEXIST) { printf("-->FIFO déjà existante!\\n"); exit(EEXIST); } else { printf("-->Impossible de créer la FIFO!\\n"); exit(2); } } printf("(1-1) Bonne creation des fifos!\\n"); // Ouverture de la fifo du controltower en mode lecture et ecriture. // serv reçoit le descripteur de cette fifo. serv = open(fifo_s, O_RDWR | O_NONBLOCK); printf("(2) Ouverture de la FIFO de transfert\\n"); if (serv > 0) { printf("(2-1) FIFO correctement ouverte!\\n"); } printf("(3) Attente des information Météos !\\n"); printf("================================================================\\n"); // Tant que la fonction STOP ne renvoi pas 1. while (STOP() == 0) { // Lecture des informations dans la fifo de transfert du controltower. if ((nbOct = read(serv, &controltower, sizeof(T_Data))) != -1) { printf("(4) Lecture de %d octets effectuée dans la FIFO de transfert\\n", nbOct); printf("================================================================\\n"); //printf ("(5) Données Météo : %s\\n",controltower.data); printf("(5) Envoi des données vers les avions...\\n"); // Ouverture de la fifo tampon du destinataire en mode ecriture dest = open(fifo_p, O_WRONLY); // Ecriture des informations récupérées dans la fifo controltower dans la fifo tampon du destinataire. write(dest, controltower.data, sizeof(T_Data)); printf("Données Envoyée!"); printf("Tapez 'S' pour terminer le controltower !\\n"); printf("================================================================\\n"); } else { sleep(1); } } printf("\\n================================================================\\n"); printf("Fermeture du controltower\\n"); printf("================================================================\\n"); // Delinkage et destruction des fifos controltower et tampon destinataires. if ((-1 == unlink(fifo_s)) || (-1 == unlink(fifo_p))) { perror("unlink Erreur"); } // Fermeture de la fifo tampon utlisée. close(dest); // Fermeture de la fifo tampon. close(serv); printf("Effacement de la console dans 3secondes!\\n"); sleep(3); system("clear"); return (0); } 

Thanks in advance...

Dimitri

NB: Excuse me if I 've done some mistakes... I 'm doing my best but it's not my first language...

I've found out that PIPE is good to use when you have one client (plane)... If you have more than one destination, it is better to use socket()

Here's a version of sending data using socket() etc.:

        dest = socket(PF_INET, SOCK_DGRAM, 0);
        if (dest == -1) return perror("socket"), 1;
        // address the port 9163 (chosen arbitrarily)
        struct sockaddr_in a = { AF_INET, htons(9163), htonl(0x7fffffff) };
        const int true = 1;
        setsockopt(dest, SOL_SOCKET, SO_BROADCAST, &true, sizeof true);
        sendto(dest, &controltower, nbOct, 0, (struct sockaddr *)&a, sizeof a);
        close(dest);

If socat is available, you can simulate any plane with:

socat UDP-RECV:9163,reuseaddr 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM