主要注意流程:

STREAM SOCKET:

Server :  socket() --->  bind() ---> listen()  ---> accept()

Client:    scoket() ---> connect()

参考文章一篇就够:http://troydhanson.github.io/misc/Unix_domain_sockets.html

自己写的 一个 Server 和 一个Client:

  1. //Server
  2. //
  3. // unix_domain_server.c
  4. // UnixDomainServer
  5. //
  6. // Created by gtliu on 7/11/13.
  7. // Copyright (c) 2013 GT. All rights reserved.
  8. //
  9.  
  10. #include "MITLogModule.h"
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <errno.h>
  15. #include <stddef.h>
  16. #include <unistd.h>
  17. #include <sys/socket.h>
  18. #include <sys/un.h>
  19.  
  20. #define FUNC_FAIL -1
  21. #define FUNC_SUCCESS 0
  22. #define SOCKADDR_UN_SUN_PATH_MAX_LEN 103 // 104 - 1
  23. #define SER_ACCEPT_CON_NUM 1
  24.  
  25. /**
  26. * Create a server endpoint of a connection.
  27. *
  28. * @param scok_path: the unix domain socket path
  29. * @return return the file descirption if all ok, <0 on err
  30. */
  31. int create_serv_listen(const char *sock_path)
  32. {
  33. size_t path_len = strlen(sock_path);
  34. if (path_len == 0) {
  35. MITLogWrite(MITLOG_LEVEL_ERROR, "socket path can't be empty");
  36. return FUNC_FAIL;
  37. } else if (path_len > SOCKADDR_UN_SUN_PATH_MAX_LEN) {
  38. MITLogWrite(MITLOG_LEVEL_ERROR, "socket path length must less than%d", SOCKADDR_UN_SUN_PATH_MAX_LEN);
  39. return FUNC_FAIL;
  40. }
  41.  
  42. int fd = 0, size = 0;
  43. struct sockaddr_un server_un;
  44. memset(&server_un, 0, sizeof(server_un));
  45. server_un.sun_family = AF_UNIX;
  46. strncpy(server_un.sun_path, sock_path, sizeof(server_un.sun_path) - 1);
  47.  
  48. if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
  49. MITLogWrite(MITLOG_LEVEL_ERROR, "socket() faild:%s", strerror(errno));
  50. return FUNC_FAIL;
  51. }
  52. /* in case it already exists */
  53. unlink(sock_path);
  54.  
  55. size = offsetof(struct sockaddr_un, sun_path) + strlen(server_un.sun_path);
  56. if (bind(fd, (struct sockaddr *)&server_un, size) < 0) {
  57. MITLogWrite(MITLOG_LEVEL_ERROR, "bind() failed:%s", strerror(errno));
  58. close(fd);
  59. return FUNC_FAIL;
  60. }
  61.  
  62. if (listen(fd, 0) < 0) {
  63. MITLogWrite(MITLOG_LEVEL_ERROR, "listen() failed:%s", strerror(errno));
  64. close(fd);
  65. return FUNC_FAIL;
  66. }
  67.  
  68. return fd;
  69. }
  70.  
  71. /**
  72. * Accept a client's connection and return the relative file descriptor.
  73. *
  74. * @param listen_fd: the server socket file descriptor
  75. * @return return the file descirption if all ok, <0 on err
  76. */
  77. int serv_accept(int listen_fd)
  78. {
  79. int child_fd = 0, len = 0;
  80. struct sockaddr_un income_un;
  81. len = sizeof(income_un);
  82.  
  83. if ((child_fd = accept(listen_fd, (struct sockaddr *)&income_un, &len)) < 0) {
  84. MITLogWrite(MITLOG_LEVEL_ERROR, "accept() failed:%s", strerror(errno));
  85. /* often errno=EINTR, if signal caught */
  86. return FUNC_FAIL;
  87. }
  88. return child_fd;
  89. }
  90.  
  91. int main(int argc, const char * argv[])
  92. {
  93.  
  94. MITLogOpen("unix_domain_server");
  95.  
  96. char *sock_path = "/tmp/domain_socket_one";
  97. if (argc > 1) {
  98. sock_path = (char *) argv[1];
  99. }
  100.  
  101. MITLogWrite(MITLOG_LEVEL_COMMON, "Starting the server...");
  102. int listen_fd = create_serv_listen(sock_path);
  103. if (listen_fd == FUNC_FAIL) {
  104. return FUNC_FAIL;
  105. }
  106.  
  107. MITLogWrite(MITLOG_LEVEL_COMMON, "Wait for the client... listen_fd:%d", listen_fd);
  108. int child_fd = serv_accept(listen_fd);
  109. if (child_fd == FUNC_FAIL) {
  110. close(listen_fd);
  111. return FUNC_FAIL;
  112. }
  113.  
  114. char recv_buffer[1024] = {0};
  115. while (1) {
  116. MITLogWrite(MITLOG_LEVEL_COMMON, "Wait for the message...");
  117. if(read(child_fd, recv_buffer, sizeof(recv_buffer) - 1) > 0) {
  118. printf("Recieve message:%s\n", recv_buffer);
  119. }
  120. sleep(2);
  121. }
  122.  
  123. MITLogClose();
  124. return 0;
  125. }
  1. //Client
  2. //
  3. // main.c
  4. // UnixDomainClient
  5. //
  6. // Created by gtliu on 7/11/13.
  7. // Copyright (c) 2013 GT. All rights reserved.
  8. //
  9.  
  10. #include "MITLogModule.h"
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <errno.h>
  15. #include <stddef.h>
  16. #include <unistd.h>
  17. #include <sys/socket.h>
  18. #include <sys/un.h>
  19. #include <sys/stat.h>
  20.  
  21. #define FUNC_FAIL -1
  22. #define FUNC_SUCCESS 0
  23. #define SOCKADDR_UN_SUN_PATH_MAX_LEN 103 // 104 - 1
  24. #define SER_ACCEPT_CON_NUM 1
  25.  
  26. /**
  27. * Create a server endpoint of a connection.
  28. *
  29. * @param scok_path: the unix domain socket path
  30. * @return return the file descirption if all ok, <0 on err
  31. */
  32. int client_connection(const char *sock_path)
  33. {
  34. size_t path_len = strlen(sock_path);
  35. if (path_len == 0) {
  36. MITLogWrite(MITLOG_LEVEL_ERROR, "socket path can't be empty");
  37. return FUNC_FAIL;
  38. } else if (path_len > SOCKADDR_UN_SUN_PATH_MAX_LEN) {
  39. MITLogWrite(MITLOG_LEVEL_ERROR, "socket path length must less than%d", SOCKADDR_UN_SUN_PATH_MAX_LEN);
  40. return FUNC_FAIL;
  41. }
  42.  
  43. int fd = 0, len = 0;
  44. if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
  45. MITLogWrite(MITLOG_LEVEL_ERROR, "socket() faild:%s", strerror(errno));
  46. return FUNC_FAIL;
  47. }
  48.  
  49. struct sockaddr_un client_un;
  50. memset(&client_un, 0, sizeof(client_un));
  51. client_un.sun_family = AF_UNIX;
  52. strncpy(client_un.sun_path, sock_path, sizeof(client_un.sun_path) - 1);
  53.  
  54. len = offsetof(struct sockaddr_un, sun_path) + strlen(client_un.sun_path);
  55. if ((connect(fd, (struct sockaddr *)&client_un, len)) < 0) {
  56. MITLogWrite(MITLOG_LEVEL_ERROR, "connect() failed:%s", strerror(errno));
  57. close(fd);
  58. return FUNC_FAIL;
  59. }
  60.  
  61. return fd;
  62. }
  63.  
  64. int main(int argc, const char * argv[])
  65. {
  66.  
  67. MITLogOpen("unix_domain_client");
  68.  
  69. char *sock_path = "/tmp/domain_socket_one";
  70. if (argc > 1) {
  71. sock_path = (char *) argv[1];
  72. }
  73.  
  74. MITLogWrite(MITLOG_LEVEL_COMMON, "Starting the client...");
  75. int client_fd = client_connection(sock_path);
  76. if (client_fd == FUNC_FAIL) {
  77. return FUNC_FAIL;
  78. }
  79.  
  80. for (int i=1; i < 100; ++i) {
  81. MITLogWrite(MITLOG_LEVEL_COMMON, "Send message to server...");
  82. char msg[256] = {0};
  83. sprintf(msg, "client message :%d", i);
  84. if(write(client_fd, msg, strlen(msg)) > 0) {
  85. MITLogWrite(MITLOG_LEVEL_COMMON, "Send message :%d success", i);
  86. }
  87. sleep(2);
  88. }
  89.  
  90. MITLogClose();
  91. return 0;
  92. }

Unix Domain Socket 域套接字实现的更多相关文章

  1. 【转】PHP实现系统编程(四)--- 本地套接字(Unix Domain Socket)

    原文:http://blog.csdn.net/zhang197093/article/details/78143687?locationNum=6&fps=1 --------------- ...

  2. Unix domain socket

    转载:http://www.cnblogs.com/chekliang/p/3222950.html socket API原本是为网络通讯设计的,但后来在socket的框架上发展出一种IPC机制,就是 ...

  3. (unix domain socket)使用udp发送>=128K的消息会报ENOBUFS的错误

    一个困扰我两天的问题, Google和Baidu没有找到解决方法! 此文为记录这个问题,并给出原因和解决方法. 1.Unix domain socket简介 unix域协议并不是一个实际的协议族,而是 ...

  4. [apue] 作为 daemon, 启动 Unix Domain Socket 侦听失败?

    前段时间写一个传递文件句柄的小 demo,有 server 端.有 client 端,之间通过 Unix Domain Socket 通讯. 在普通模式下,双方可以正常建立连接,当server端作为d ...

  5. Envoy 基础教程:使用 Unix Domain Socket(UDS) 与上游集群通信

    Envoy Proxy 在大多数情况下都是作为 Sidecar 与应用部署在同一网络环境中,每个应用只需要与 Envoy(localhost)交互,不需要知道其他服务的地址.然而这并不是 Envoy ...

  6. Unix domain socket 简介

    Unix domain socket 又叫 IPC(inter-process communication 进程间通信) socket,用于实现同一主机上的进程间通信.socket 原本是为网络通讯设 ...

  7. mysql unix domain socket and network socket, ssh key

    当主机填写为localhost时mysql会采用 unix domain socket连接 当主机填写为127.0.0.1时mysql会采用tcp方式连接 这是linux套接字网络的特性,win平台不 ...

  8. linux一切皆文件之Unix domain socket描述符(二)

    一.知识准备 1.在linux中,一切皆为文件,所有不同种类的类型都被抽象成文件(比如:块设备,socket套接字,pipe队列) 2.操作这些不同的类型就像操作文件一样,比如增删改查等 3.主要用于 ...

  9. Unix domain socket IPC

    UNIX Domain socket 虽然网络socket也可用于同一台主机的进程间通讯(通过lo地址127.0.0.1),但是unix domain socket用于IPC更有效率:不需要经过网络协 ...

随机推荐

  1. perl 获取文件内容里第一个AAA和最后一个AAA

    <pre name="code" class="html">[root@wx03 ~]# cat -n aaa 1 3`13 2 edqae 3 d ...

  2. 基于visual Studio2013解决面试题之0501上台阶

     题目

  3. webview加载网页加载不出来

    1.webView.loadUrl(picTargetUrl); 写在最前面. 1.在无线城市迷你版的项目,用webview去loadUrl的时候出现加载的现象. url 地址是 http://go. ...

  4. ubuntu 10.04安装qtcreator并汉化

    最近最的项目中需要做出来一个带有界面的demo,所以想到了用qt做个简单的demo! 于是在ubuntu上安装了qt,很简单apt-get apt-get install qtcreator 大概几百 ...

  5. hive udaf 用maven打包运行create temporary function 时报错

    用maven打包写好的jar,在放到hive中作暂时函数时报错. 错误信息例如以下: hive> create temporary function maxvalue as "com. ...

  6. ubuntu12.04 安装 opencv 2.4.8(非源代码编译)

    一:安装所须要的各种库,如GTK3.xx 安装GCC:sudo apt-get install build-essential 安装CMakesudo apt-get install cmake su ...

  7. 使用gradle打包jar包

    近期用android studio来做android开发的IDE,它是使用gradle来构建的,于是開始学习gradle. 如今有一个项目,里面有一个android-library的模块.我想在做re ...

  8. 【Ubuntu】升到14,攻克了进入用户后没有菜单条导航栏的问题

    控制台还能够进,用ctrl+alt+f1用老账号登录,用sudo adduser test新建立一个名字叫test的帐号 然后就能够进去了,可能是配置文件坏掉了

  9. android花屏效果的实现(ViewPager的基本使用)

    1.程序运行效果图 二.代码实现 1.main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/re ...

  10. 利用ArrayList对Hashtable其进行排序

    前言: 最近在使用Hashtable的时候发现一个问题:就是当你对Hashtable进行遍历的时候整个输出结果是毫无顺序的, 上网查了一下说是Hashtable有自己内部的排序机制,如果要自定义排序的 ...