如何使用C发送带有命名管道的广播消息?

时间:2015-11-09 16:10:40

标签: c named-pipes broadcast

我想写一个有1个发送者和3个接收者的程序。发送方可以向每个接收方发送单独的消息,并将消息分组给所有接收方。我使用命名管道来实现这一点但不能同步向所有接收器发送组消息。有想法用命名管道发送广播消息吗?

发件人计划:

/* Sender */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main()
{
 char pipename1[] = "/tmp/pipe1";
 char pipename2[] = "/tmp/pipe2";
 char pipename3[] = "/tmp/pipe3";
 char pipename4[] = "/tmp/pipe4";
 char buf1[80];
 char buf2[80];
 char buf3[80];
 char buf4[80];
 int fd1, fd2, fd3, fd4;
 int select1, select2;
 int n,pid;

 /* Pipe Creation */
 if (access(pipename1, F_OK) == -1) {
  fd1 = mkfifo(pipename1, 0700);
  if (fd1 != 0) {
   printf("Pipe creation error\n");
   exit(1);
  }
 }
 if (access(pipename2, F_OK) == -1) {
  fd2 = mkfifo(pipename2, 0700);
  if (fd2 != 0) {
   printf("Pipe creation error\n");
   exit(1);
  }
 }
 if (access(pipename3, F_OK) == -1) {
  fd3 = mkfifo(pipename3, 0700);
  if (fd3 != 0) {
   printf("Pipe creation error\n");
   exit(1);
  }
 }
  if (access(pipename4, F_OK) == -1) {
  fd4 = mkfifo(pipename4, 0700);
  if (fd4 != 0) {
   printf("Pipe creation error\n");
   exit(1);
  }
 }

 pid = fork();
 if (pid < 0) {
 printf("Fork failed\n");
 exit(1);
 } else if (pid == 0) {

  printf("1. Send individual message\n");
  printf("2. Send group message\n");
  printf("Please select an option: ");
  scanf("%d", &select1);

  switch(select1) {  
   case 1: 
    printf("1. Receiver 1 (Mary)\n");
    printf("2. Receiver 2 (John)\n");
    printf("3. Receiver 3 (Peter)\n");
    printf("Please select a receiver: ");
    scanf("%d", &select2);
    switch(select2) {
      case 1:
      /* Open pipe for writing */
      if ((fd1 = open(pipename1, O_WRONLY)) < 0) {
        printf("Pipe open error\n");
        exit(1);
       }
      while (1) {
       printf("Send message to Mary: \n");
       n = read(STDIN_FILENO,buf1,80);
       if (n <= 0) break;
       buf1[--n] = 0;
       printf("Sending message [%s] to Mary\n",buf1);
       write(fd1,buf1,n);
      }
      close(fd1);
     break;
     case 2:
      /* Open pipe for writing */
      if ((fd2 = open(pipename2, O_WRONLY)) < 0) {
        printf("Pipe open error\n");
        exit(1);
       }
      while (1) {
       printf("Send message to John: \n");
       n = read(STDIN_FILENO,buf2,80);
       if (n <= 0) break;
       buf2[--n] = 0;
       printf("Sending message [%s] to John\n",buf2);
       write(fd2,buf2,n);
      }
     break;
     case 3:
      /* Open pipe for writing */
      if ((fd3 = open(pipename3, O_WRONLY)) < 0) {
        printf("Pipe open error\n");
        exit(1);
       }
      while (1) {
       printf("Send message to Peter: \n");
       n = read(STDIN_FILENO,buf3,80);
       if (n <= 0) break;
       buf3[--n] = 0;
       printf("Sending message [%s] to Peter\n",buf3);
       write(fd3,buf3,n);
      }
     break;
     default:
      printf("Receiver not found\n");
     break;
   }
   case 2:
   /* Open pipe for writing */
     if ((fd4 = open(pipename4, O_WRONLY)) < 0) {
      printf("Pipe open error\n");
      exit(1);
     }
     while (1) {
      printf("Send message to Group: \n");
      n = read(STDIN_FILENO,buf4,80);
      if (n <= 0) break;
      buf4[--n] = 0;
      printf("Sending message [%s] to Group\n",buf4);
      write(fd4,buf4,n);
     }
   break;
   default:
    printf("Wrong Input!\n");
   break;
  }
 } else {
  wait(NULL);
 }
 unlink(pipename1);
 unlink(pipename2);
 unlink(pipename3);
 unlink(pipename4);
 exit(0);
}

Receiver1计划:

/* Receiver1 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main()
{
 char pipename1[] = "/tmp/pipe1";
 char pipename4[] = "/tmp/pipe4";
 char buf1[80];
 char buf4[80];
 int fd1, fd4;
 int n, pid;

 printf("Mary is online\n");

 pid = fork();
 if (pid < 0) {
 printf("Fork failed\n");
 exit(1);
 } else if (pid == 0) {
  /* Open pipe for reading */
  if ((fd1 = open(pipename1, O_RDONLY)) < 0) {
  printf("Pipe open error\n");
  exit(1);
  } 
  while ((n = read(fd1, buf1, 80)) > 0) {
   buf1[n] = 0;
   printf("[Message received:] %s\n", buf1, n);
  }
  close(fd1);
  exit(0);
 } else {
  /* Open pipe for reading */
  if ((fd4 = open(pipename4, O_RDONLY)) < 0) {
  printf("Pipe open error\n");
  exit(1);
  } 
  while ((n = read(fd4, buf4, 80)) > 0) {
   buf4[n] = 0;
   printf("[Message received:] %s\n", buf4, n);
  }
  close(fd4);
  wait(NULL);
  exit(0);
 }
}

Receiver2计划:

/* Receiver2 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main()
{
 char pipename2[] = "/tmp/pipe2";
 char pipename4[] = "/tmp/pipe4";
 char buf2[80];
 char buf4[80];
 int fd2, fd4;
 int n, pid;

 printf("John is online\n");

 pid = fork();
 if (pid < 0) {
 printf("Fork failed\n");
 exit(1);
 } else if (pid == 0) {
  /* Open pipe for reading */
  if ((fd2 = open(pipename2, O_RDONLY)) < 0) {
  printf("Pipe open error\n");
  exit(1);
  } 
  while ((n = read(fd2, buf2, 80)) > 0) {
   buf2[n] = 0;
   printf("[Message received:] %s\n", buf2, n);
  }
  close(fd2);
  exit(0);
 } else {
  /* Open pipe for reading */
  if ((fd4 = open(pipename4, O_RDONLY)) < 0) {
  printf("Pipe open error\n");
  exit(1);
  } 
  while ((n = read(fd4, buf4, 80)) > 0) {
   buf4[n] = 0;
   printf("[Message received:] %s\n", buf4, n);
  }
  close(fd4);
  wait(NULL);
  exit(0);
 }
}

Receiver3计划:

/* Receiver3 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main()
{
 char pipename3[] = "/tmp/pipe3";
 char pipename4[] = "/tmp/pipe4";
 char buf3[80];
 char buf4[80];
 int fd3, fd4;
 int n, pid;

 printf("Peter is online\n");

 pid = fork();
 if (pid < 0) {
 printf("Fork failed\n");
 exit(1);
 } else if (pid == 0) {
  /* Open pipe for reading */
  if ((fd3 = open(pipename3, O_RDONLY)) < 0) {
  printf("Pipe open error\n");
  exit(1);
  } 
  while ((n = read(fd3, buf3, 80)) > 0) {
   buf3[n] = 0;
   printf("[Message received:] %s\n", buf3, n);
  }
  close(fd3);
  exit(0);
 } else {
  /* Open pipe for reading */
  if ((fd6 = open(pipename4, O_RDONLY)) < 0) {
  printf("Pipe open error\n");
  exit(1);
  } 
  while ((n = read(fd4, buf4, 80)) > 0) {
   buf4[n] = 0;
   printf("[Message received:] %s\n", buf4, n);
  }
  close(fd4);
  wait(NULL);
  exit(0);
 }
}

0 个答案:

没有答案