• FIFO实现客户端服务器端通信



    FIFO解决了在进程通信的时候产生大量临时文件的问题,并且可以实现非血缘关系进程间的通信,而且可以保留给后来的进程使用。

    FIFO的读写规则和匿名管道的读写规则相似,不过FIFO保存在磁盘上,而匿名管道保存在内存里。

    当FIFO的写进程关闭的时候,会向FIFO的读进程发送一个文件结束符。

    客户端:

    1. #include<stdio.h>
    2. #include<unistd.h>
    3. #include<sys/stat.h>
    4. #include<sys/types.h>
    5. #include<fcntl.h>
    6. #include<string.h>
    7. int main()
    8. {
    9. char* msg = "Hello, i am client! ";
    10. int fd = open("./fifo1",O_WRONLY);
    11. int i = 0;
    12. for(;i<10;i++)
    13. {
    14. printf(msg);
    15. if(write(fd,msg,strlen(msg)+1)<0)
    16. {
    17. _exit(-1);
    18. }
    19. sleep(2);
    20. }
    21. close(fd);
    22. return 0;
    23. }


    服务器端:

    1. #include<stdio.h>
    2. #include<unistd.h>
    3. #include<sys/stat.h>
    4. #include<sys/types.h>
    5. #include<fcntl.h>
    6. #include<string.h>
    7. int main()
    8. {
    9. int fd = open("./fifo1",O_RDONLY);
    10. int i = 0;
    11. char buf[24]={0};
    12. while(read(fd,buf,24)>0)
    13. {
    14. printf(buf);
    15. }
    16. close(fd);
    17. return 0;
    18. }






  • 相关阅读:
    selectHelper
    Windows Server 2003 下实现网络负载均衡(2) (转)
    顺序栈
    线性表链式存储
    线性表顺序存储
    Swift
    组件化
    swift
    Swift
    Swift
  • 原文地址:https://www.cnblogs.com/ZhangJinkun/p/4563138.html
Copyright © 2020-2023  润新知