简体   繁体   English

如何通过同一管道向多个进程发送消息? (C语言)

[英]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 ). 预报台将数据发送到控制塔,然后将数据分发给所有由PID(未知数量的飞机,可能进入或离开由控制塔控制的区域)识别的飞机。

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... NB:对不起,如果我犯了一些错误...我正在尽力而为,但这不是我的母语...

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() 我发现当您有一个客户端(飞机)时,PIPE很好用...如果您有多个目标,最好使用socket()

Here's a version of sending data using socket() etc.: 这是使用socket()等发送数据的版本:

        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则可以使用以下方法模拟任何飞机:

socat UDP-RECV:9163,reuseaddr 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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