用命名管道实现进程间的通信-----简单聊天室的建立

本篇博文讲的是用两个命名管道实现简单聊天室的建立,本质上就是进程间的通信

程序共有四部分组成:命名管道myfifo

命名管道yourfifo

服务器端

客户端

功能实现过程:服务器端父进程往myfifo中写数据

     客户端子进程从myfifo中读取服务器端父进程发送来的数据

   客户端父进程往yourfifo中写数据

    服务器端子进程从yourfifo中读取客户端父进程发送来的数据


服务器端:

#include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <stdlib.h>
    #include <sys/stat.h>
    #include <string.h>
    #include <fcntl.h>

    int main(int argc,char *argv[])
    {
        int fd,fd1;
        pid_t fpid;
        char buf[20],buf1[20];
        int bytes_read,bytes_write;
        int bytes_read1,bytes_write1;

        if((mkfifo("myfifo",0666))<0)
        {
            printf("creat pipe error!!\n");
            exit(1);
        }
        if((mkfifo("yourfifo",0666))<0)
        {
            printf("2___creat pipe error!!\n");
            exit(2);
        }
        
        fpid=fork();

        /*子进程,用来接收客户端进程发送来的信息*/
        if(fpid==0)
        {
            while(1)
            {
                printf(">");
                if((fd1=open("yourfifo",O_RDWR))<0)
                {
                    printf("open yourfifo error!\n");
                    exit(10);
                }
                if((bytes_read=read(fd1,buf1,sizeof(buf1)))<0)
                {
                    printf("read yourfifo error!\n");
                    exit(11);
                }
                buf1[bytes_read]='\0';
                printf("%s\n",buf1);
                close(fd1);
            }
        }
        /*父进程,用于给客户端发送信息*/
        if(fpid>0)
        {
            while(1)
            {
                if((fd=open("myfifo",O_RDWR))<0)
                {
                    printf("open pipe error!\n");
                    exit(2);
                }
                printf(">");
                scanf("%s",buf);
                if((strcmp(buf,"quit"))==0)
                {
                    exit(0);
                }

                if((bytes_write=write(fd,buf,sizeof(buf)))<0)
                {
                    printf("write pipe error!!\n");
                    exit(3);
                }
                close(fd);
            }
    //  close(fd);  
              }
                      return 0;
                      }
客户端 :

#include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <stdlib.h>
    #include <sys/stat.h>
    #include <string.h>
    #include <fcntl.h>

    int main(int argc,char *argv[])
    {
        int fd;
        int fd1,fpid;
        char buf[20],buf1[20];
        int bytes_read,bytes_write;
        int bytes_read1,bytes_write1;

        fpid=fork();
        /*子进程,发送信息给服务器端*/
        if(fpid==0)
        {
            while(1)
            {
                printf(">");
                scanf("%s",buf1);
                if((strcmp(buf1,"quit"))==0)
                {
                    exit(0);
                }
                if((fd1=open("yourfifo",O_RDWR))<0)
                {
                    printf("open yourfifo error!\n");
                    exit(10);
                }
                if((bytes_write1=write(fd1,buf1,sizeof(buf1)))<0)
                {
                    printf("write yourfifo error!\n");
                    exit(11);
                }
                close(fd1);
            }
        }
        /*父进程,接收从服务器端发送来的信息*/
        if(fpid>0)
        {
            while(1)
            {

                if((fd=open("myfifo",O_RDWR))<0)
                {
                    printf("open pipe error!\n");
                    exit(2);
                }

                if((bytes_read=read(fd,buf,sizeof(buf)))<0)
                {
                    printf("read fifo error!!\n");
                    exit(2);
                }
                buf[bytes_read]='\0';
                printf("%s\n",buf);
                close(fd);

            }

    //  close(fd);  
            }
                    return 0;
    
      }                   
    
    



打开两个终端,在其中一个中先运行服务器端

gcc -o fuwuqi  fuwuqi.c

./fuwuqi

在另一个终端运行客户端

gcc  -o kehuduan  kehuduan.c

./kehuduan

至此,就可以在两个终端里对话了,输入quit退出

下一次运行前,删除上一次生成的myfifo   yourfifo两个管道,再执行 ./fuwuqi             ./kehuduan

  • 6
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值