zoukankan      html  css  js  c++  java
  • Linux基础——通过有名管道实现简单的聊天程序

    在这里,我们根据参数,创建两个有名管道,mkfifo函数可以帮我们实现管道的创建。

    这里需要注意,服务器端先打开的哪个管道,客户端也需要先打开哪个管道,而且必须是一端为写,另一端为读。

    在创建父子进程时,我们需要注意,当该进程只读取数据时,先要将写端关闭,反之,就需先将读端关闭。

    服务器实现代码如下:

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 #include <sys/stat.h>
     5 #include <sys/types.h>
     6 #include <fcntl.h>
     7 int main(int argc, const char *argv[])
     8 {
     9     if(argc < 3)
    10     {
    11         printf("参数不足!!
    ");
    12         exit(1);
    13     }
    14     if( mkfifo(argv[1], 0666) == -1 || mkfifo(argv[2], 0666) == -1)
    15     {
    16         printf("创建管道失败 !!
    ");
    17         exit(1);
    18     }
    19     int server, client;
    20     server = open(argv[1], O_WRONLY);
    21     client = open(argv[2], O_RDONLY);
    22     if(server == -1 || client == -1)
    23     {
    24         printf("open error!!
    ");
    25         exit(1);
    26     }
    27     if(fork() == 0)
    28     {
    29         if(fork() == 0)
    30         {
    31             close(server);
    32             char buf[128];
    33             while(memset(buf, 0, 128), read(client, buf, 128) )
    34                 printf("recv : %s
    ", buf);
    35             close(client);
    36             exit(1);
    37         }
    38         exit(1);
    39     }
    40     wait(NULL);
    41     close(client);
    42     char msg[128];
    43     while(memset(msg, 0, 128), fgets(msg, 128, stdin) != NULL)
    44         write(server, msg, strlen(msg));
    45     close(server);
    46     unlink(argv[1]);
    47     unlink(argv[2]);
    48     return 0;
    49 }
    View Code


    客户端实现代码如下:

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 #include <fcntl.h>
     5 #include <sys/stat.h>
     6 #include <sys/types.h>
     7 int main(int argc, const char *argv[])
     8 {
     9     int server, client;
    10     server = open(argv[1], O_RDONLY);
    11     client = open(argv[2], O_WRONLY);
    12     if(server == -1 || client == -1)
    13     {
    14         printf("open error!!
    ");
    15         exit(1);
    16     }
    17     if(fork() == 0)
    18     {
    19         if(fork() == 0)
    20         {
    21             close(client);
    22             char buf[128];
    23             while(memset(buf, 0, 128), read(server, buf, 128) )
    24                 printf("recv : %s
    ", buf);
    25             close(server);
    26             exit(1);
    27         }
    28         exit(1);
    29     }
    30     wait(NULL);
    31     close(server);
    32     char msg[128];
    33     while(memset(msg, 0, 128), fgets(msg, 128, stdin) != NULL)
    34         write(client, msg, strlen(msg));
    35     close(client);
    36     return 0;
    37 }
    View Code

    注意:
      在fork()中再次创建子进程是为了防止僵尸进程。

  • 相关阅读:
    SpringCloud(四)GateWay网关
    C++中的间接宏函数
    一个C++引用库的头文件预编译陷阱
    谈谈C++中的数据对齐
    在C++中实现aligned_malloc
    WPF中的DesignerProperties
    在.NET 6中使用DateOnly和TimeOnly
    在 Ubuntu 上安装 .NET SDK 或 .NET 运行时
    Microsoft Build 2021第二天
    Microsoft Build 2021大会开始后,Develop Blog一系列更新
  • 原文地址:https://www.cnblogs.com/gjn135120/p/4009290.html
Copyright © 2011-2022 走看看