经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C » 查看文章
c/c++ linux 进程间通信系列2,使用UNIX_SOCKET
来源:cnblogs  作者:小石王  时间:2018/10/20 15:34:06  对本文有异议

linux 进程间通信系列2,使用UNIX_SOCKET

1,使用stream,实现进程间通信

2,使用DGRAM,实现进程间通信

关键点:使用一个临时的文件,进行信息的互传。

  1. s_un.sun_family = AF_UNIX;
  2. strcpy(s_un.sun_path, "/tmp/afunix_text");

使用stream,server端:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <sys/socket.h>
  5. #include <sys/un.h>
  6. #define FILEPATH "/tmp/afunix_text"
  7. int main(){
  8. int s0, sock;
  9. sockaddr_un s_un;
  10. sockaddr_un s_un_accept;
  11. socklen_t addrlen;
  12. s0 = socket(AF_UNIX, SOCK_STREAM, 0);
  13. if(s0 < 0){
  14. perror("socket");
  15. return 1;
  16. }
  17. s_un.sun_family = AF_UNIX;
  18. strcpy(s_un.sun_path, FILEPATH);
  19. if(bind(s0, (sockaddr*)&s_un, sizeof(s_un)) != 0){
  20. perror("bind");
  21. return 1;
  22. }
  23. if(listen(s0, 5) != 0){
  24. perror("listen");
  25. return 1;
  26. }
  27. addrlen = sizeof(s_un_accept);
  28. sock = accept(s0, (sockaddr*)&s_un_accept, &addrlen);
  29. if(sock < 0){
  30. perror("accept");
  31. return 1;
  32. }
  33. printf("after accept\n");
  34. write(sock, "the msg is send from server", 27);
  35. close(sock);
  36. close(s0);
  37. unlink(FILEPATH);
  38. return 0;
  39. }

github源代码

使用stream,client端:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <sys/socket.h>
  5. #include <sys/un.h>
  6. int main(){
  7. int sock;
  8. sockaddr_un s_un;
  9. int n;
  10. char buf[128];
  11. sock = socket(AF_UNIX, SOCK_STREAM, 0);
  12. if(sock < 0){
  13. perror("socket");
  14. return 1;
  15. }
  16. s_un.sun_family = AF_UNIX;
  17. strcpy(s_un.sun_path, "/tmp/afunix_text");
  18. if(connect(sock, (sockaddr*)&s_un, sizeof(s_un)) != 0){
  19. perror("connect");
  20. return 1;
  21. }
  22. printf("after connect\n");
  23. memset(buf, 0, sizeof(buf));
  24. n = read(sock, buf, sizeof(buf));
  25. if(n < 0){
  26. perror("read");
  27. return 1;
  28. }
  29. printf("%s\n", buf);
  30. close(sock);
  31. return 0;
  32. }

github源代码

使用dgram,发送端:

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <sys/un.h>
  7. int main(){
  8. int sock;
  9. sockaddr_un addr;
  10. socklen_t addrlen;
  11. sock = socket(AF_UNIX, SOCK_DGRAM, 0);
  12. addr.sun_family = AF_UNIX;
  13. strcpy(addr.sun_path, "/tmp/afu_dgram");
  14. int n = sendto(sock, "HELLO\n", 6, 0, (sockaddr*)&addr, sizeof(addr));
  15. printf("send data\n");
  16. close(sock);
  17. return 0;
  18. }

github源代码

使用dgram,接收端:

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <sys/un.h>
  7. int main(){
  8. int sock;
  9. sockaddr_un addr;
  10. socklen_t addrlen;
  11. char buf[1024];
  12. int n;
  13. sock = socket(AF_UNIX, SOCK_DGRAM, 0);
  14. addr.sun_family = AF_UNIX;
  15. strcpy(addr.sun_path, "/tmp/afu_dgram");
  16. bind(sock, (sockaddr*)&addr, sizeof(addr));
  17. while(1){
  18. memset(buf, 0, sizeof(buf));
  19. n = recv(sock, buf, sizeof(buf) - 1, 0);
  20. printf("recv:%s\n", buf);
  21. }
  22. close(sock);
  23. return 0;
  24. }

github源代码

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号