使用命名管道将字符串发送到两个接收器

时间:2015-11-21 08:22:55

标签: c pointers named-pipes

我想从发送方向两个接收方发送一个字符串,但是我无法收到接收方中我希望接收方的消息。当我发送消息时,两个接收器会收到一些乱码。 怎么了?

发信人:

int fd1, fd2;
char groupm[80];
char pipename1[] = "/tmp/pipe1";
char pipename2[] = "/tmp/pipe2";

/* Open pipe for writing */
 if ((fd1 = open(pipename1, O_WRONLY)) < 0) {
  printf("Pipe open error\n");
  exit(1);
 }

  printf("Send message to Group: \n");
  fflush(stdin);
  scanf("%s", &groupm);

  char * temp_groupm_1;
  char * temp_groupm_2;
  temp_groupm_1 = groupm;
  temp_groupm_2 = groupm;

  printf("Sending message [%s] to Group\n", temp_groupm_1);
  write(fd1,temp_groupm_1,80);
  close(fd1);

 if ((fd2 = open(pipename2, O_WRONLY)) < 0) {
  printf("Pipe open error\n");
  exit(1);
 }

  write(fd2,temp_groupm_2,80);
  close(fd2); 

接收者1:

int fd1;
char groupm[80];
char pipename1[] = "/tmp/pipe1";

/* Open pipe for reading */
if ((fd1 = open(pipename1, O_RDONLY)) < 0) {
   printf("Pipe open error\n");
   exit(1);
  } 
  read(fd1, &groupm, 80);
  printf("[Group message received:] %s\n", groupm);
  close(fd1);

接收者2:

 int fd2;
 char groupm[80];
 char pipename2[] = "/tmp/pipe2";

  /* Open pipe for reading */
  if ((fd2 = open(pipename2, O_RDONLY)) < 0) {
  printf("Pipe open error\n");
  exit(1);
  } 
  read(fd2, &groupm, 80);
  printf("[Group message received:] %s\n", groupm);
  close(fd2);

0 个答案:

没有答案