双管道实现进程数据通信

来源:互联网 发布:淘宝改后台是什么意思 编辑:程序博客网 时间:2024/05/01 15:23

我用的服务器端程序如下:

#include "cliser.h"
#include<fcntl.h>

int main(int argc,char *argv[])
{
  int res,client_fifo_fd;
  char *temp_data_ptr;
  int read_res;
  

  mkfifo(SERVER_FIFO_NAME,0777);
  res = open(SERVER_FIFO_NAME,O_RDONLY);
  if(res == -1)
  {
    printf("open read server_fifo_name failure/n");
    exit(EXIT_FAILURE);
  }
  
  printf("res == %d/n",res);
  read_res = read(res,temp_data_ptr,256);
  printf("read_res = %d/n",read_res);

  if(read_res > 0)
  {
    printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>server.c/n");
    printf("get data from client /n");
    printf("data was %s/n",temp_data_ptr);
    while(*temp_data_ptr)
    {
      *temp_data_ptr = toupper(*temp_data_ptr+1);
      temp_data_ptr++;
    }
    client_fifo_fd = open(CLIENT_FIFO_NAME,O_WRONLY);
    if(client_fifo_fd == -1)
    {
      printf("open write client_fifo_name/n");
      exit(EXIT_FAILURE);
    }
    write(client_fifo_fd,temp_data_ptr,BUFFER_SIZE);
  }
  close(client_fifo_fd);
  exit(EXIT_SUCCESS);
}
   
客户程序如下:#include "cliser.h"
#include<fcntl.h>

int main(int argc,char *argv[])
{
  int res;
  char buffer[BUFFER_SIZE];
  int write_res;
  int read_res;
  int client_fifo_fd; 

  memset(buffer,'/0',strlen(buffer));
  memcpy(buffer,"Hello world",30);
  
  
 //   unlink(SERVER_FIFO_NAME);
       res = open(SERVER_FIFO_NAME,O_WRONLY);
    printf("res = %d/n",res);
    if(res == -1)
    {
      printf("open %s write Failure/n",SERVER_FIFO_NAME);
      exit(EXIT_FAILURE);
    }
    write_res = write(res,buffer,strlen(buffer));
    if(write_res > 0)
    {
      printf("*****************************client.c/n");
      printf("write some data in fifo/n");
      printf("data content was %s/n",buffer);
    }
  
  
  mkfifo(CLIENT_FIFO_NAME,0777);
  sleep(5);
  client_fifo_fd = open(CLIENT_FIFO_NAME,O_RDONLY);
  
  if(client_fifo_fd == -1)
  {
    printf("CLinet_fifo_name no write data/n");
    exit(EXIT_FAILURE);
  }
  
  read_res = read(client_fifo_fd,buffer,strlen(buffer));
  if(read_res > 0)
  {
    printf("from client_fifo fetch data/n");
    printf("data content was %s/n",buffer);
  }

    close(res);
  close(client_fifo_fd);
  exit(EXIT_SUCCESS);
}
在运行过程中,红色标记的地方通不过,请问可以哪位大哥帮我看下,server从阻塞状态到执行过程中res = 3;但read_res = -1?不知为何解;