Unix Domain Socket 域套接字实现
主要注意流程:
STREAM SOCKET:
Server : socket() ---> bind() ---> listen() ---> accept()
Client: scoket() ---> connect()
参考文章一篇就够:http://troydhanson.github.io/misc/Unix_domain_sockets.html
自己写的 一个 Server 和 一个Client:
//Server
//
// unix_domain_server.c
// UnixDomainServer
//
// Created by gtliu on 7/11/13.
// Copyright (c) 2013 GT. All rights reserved.
// #include "MITLogModule.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stddef.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h> #define FUNC_FAIL -1
#define FUNC_SUCCESS 0
#define SOCKADDR_UN_SUN_PATH_MAX_LEN 103 // 104 - 1
#define SER_ACCEPT_CON_NUM 1 /**
* Create a server endpoint of a connection.
*
* @param scok_path: the unix domain socket path
* @return return the file descirption if all ok, <0 on err
*/
int create_serv_listen(const char *sock_path)
{
size_t path_len = strlen(sock_path);
if (path_len == 0) {
MITLogWrite(MITLOG_LEVEL_ERROR, "socket path can't be empty");
return FUNC_FAIL;
} else if (path_len > SOCKADDR_UN_SUN_PATH_MAX_LEN) {
MITLogWrite(MITLOG_LEVEL_ERROR, "socket path length must less than%d", SOCKADDR_UN_SUN_PATH_MAX_LEN);
return FUNC_FAIL;
} int fd = 0, size = 0;
struct sockaddr_un server_un;
memset(&server_un, 0, sizeof(server_un));
server_un.sun_family = AF_UNIX;
strncpy(server_un.sun_path, sock_path, sizeof(server_un.sun_path) - 1); if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
MITLogWrite(MITLOG_LEVEL_ERROR, "socket() faild:%s", strerror(errno));
return FUNC_FAIL;
}
/* in case it already exists */
unlink(sock_path); size = offsetof(struct sockaddr_un, sun_path) + strlen(server_un.sun_path);
if (bind(fd, (struct sockaddr *)&server_un, size) < 0) {
MITLogWrite(MITLOG_LEVEL_ERROR, "bind() failed:%s", strerror(errno));
close(fd);
return FUNC_FAIL;
} if (listen(fd, 0) < 0) {
MITLogWrite(MITLOG_LEVEL_ERROR, "listen() failed:%s", strerror(errno));
close(fd);
return FUNC_FAIL;
} return fd;
} /**
* Accept a client's connection and return the relative file descriptor.
*
* @param listen_fd: the server socket file descriptor
* @return return the file descirption if all ok, <0 on err
*/
int serv_accept(int listen_fd)
{
int child_fd = 0, len = 0;
struct sockaddr_un income_un;
len = sizeof(income_un); if ((child_fd = accept(listen_fd, (struct sockaddr *)&income_un, &len)) < 0) {
MITLogWrite(MITLOG_LEVEL_ERROR, "accept() failed:%s", strerror(errno));
/* often errno=EINTR, if signal caught */
return FUNC_FAIL;
}
return child_fd;
} int main(int argc, const char * argv[])
{ MITLogOpen("unix_domain_server"); char *sock_path = "/tmp/domain_socket_one";
if (argc > 1) {
sock_path = (char *) argv[1];
} MITLogWrite(MITLOG_LEVEL_COMMON, "Starting the server...");
int listen_fd = create_serv_listen(sock_path);
if (listen_fd == FUNC_FAIL) {
return FUNC_FAIL;
} MITLogWrite(MITLOG_LEVEL_COMMON, "Wait for the client... listen_fd:%d", listen_fd);
int child_fd = serv_accept(listen_fd);
if (child_fd == FUNC_FAIL) {
close(listen_fd);
return FUNC_FAIL;
} char recv_buffer[1024] = {0};
while (1) {
MITLogWrite(MITLOG_LEVEL_COMMON, "Wait for the message...");
if(read(child_fd, recv_buffer, sizeof(recv_buffer) - 1) > 0) {
printf("Recieve message:%s\n", recv_buffer);
}
sleep(2);
} MITLogClose();
return 0;
}
//Client
//
// main.c
// UnixDomainClient
//
// Created by gtliu on 7/11/13.
// Copyright (c) 2013 GT. All rights reserved.
// #include "MITLogModule.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stddef.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h> #define FUNC_FAIL -1
#define FUNC_SUCCESS 0
#define SOCKADDR_UN_SUN_PATH_MAX_LEN 103 // 104 - 1
#define SER_ACCEPT_CON_NUM 1 /**
* Create a server endpoint of a connection.
*
* @param scok_path: the unix domain socket path
* @return return the file descirption if all ok, <0 on err
*/
int client_connection(const char *sock_path)
{
size_t path_len = strlen(sock_path);
if (path_len == 0) {
MITLogWrite(MITLOG_LEVEL_ERROR, "socket path can't be empty");
return FUNC_FAIL;
} else if (path_len > SOCKADDR_UN_SUN_PATH_MAX_LEN) {
MITLogWrite(MITLOG_LEVEL_ERROR, "socket path length must less than%d", SOCKADDR_UN_SUN_PATH_MAX_LEN);
return FUNC_FAIL;
} int fd = 0, len = 0;
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
MITLogWrite(MITLOG_LEVEL_ERROR, "socket() faild:%s", strerror(errno));
return FUNC_FAIL;
} struct sockaddr_un client_un;
memset(&client_un, 0, sizeof(client_un));
client_un.sun_family = AF_UNIX;
strncpy(client_un.sun_path, sock_path, sizeof(client_un.sun_path) - 1); len = offsetof(struct sockaddr_un, sun_path) + strlen(client_un.sun_path);
if ((connect(fd, (struct sockaddr *)&client_un, len)) < 0) {
MITLogWrite(MITLOG_LEVEL_ERROR, "connect() failed:%s", strerror(errno));
close(fd);
return FUNC_FAIL;
} return fd;
} int main(int argc, const char * argv[])
{ MITLogOpen("unix_domain_client"); char *sock_path = "/tmp/domain_socket_one";
if (argc > 1) {
sock_path = (char *) argv[1];
} MITLogWrite(MITLOG_LEVEL_COMMON, "Starting the client...");
int client_fd = client_connection(sock_path);
if (client_fd == FUNC_FAIL) {
return FUNC_FAIL;
} for (int i=1; i < 100; ++i) {
MITLogWrite(MITLOG_LEVEL_COMMON, "Send message to server...");
char msg[256] = {0};
sprintf(msg, "client message :%d", i);
if(write(client_fd, msg, strlen(msg)) > 0) {
MITLogWrite(MITLOG_LEVEL_COMMON, "Send message :%d success", i);
}
sleep(2);
} MITLogClose();
return 0;
}
Unix Domain Socket 域套接字实现的更多相关文章
- 【转】PHP实现系统编程(四)--- 本地套接字(Unix Domain Socket)
原文:http://blog.csdn.net/zhang197093/article/details/78143687?locationNum=6&fps=1 --------------- ...
- Unix domain socket
转载:http://www.cnblogs.com/chekliang/p/3222950.html socket API原本是为网络通讯设计的,但后来在socket的框架上发展出一种IPC机制,就是 ...
- (unix domain socket)使用udp发送>=128K的消息会报ENOBUFS的错误
一个困扰我两天的问题, Google和Baidu没有找到解决方法! 此文为记录这个问题,并给出原因和解决方法. 1.Unix domain socket简介 unix域协议并不是一个实际的协议族,而是 ...
- [apue] 作为 daemon, 启动 Unix Domain Socket 侦听失败?
前段时间写一个传递文件句柄的小 demo,有 server 端.有 client 端,之间通过 Unix Domain Socket 通讯. 在普通模式下,双方可以正常建立连接,当server端作为d ...
- Envoy 基础教程:使用 Unix Domain Socket(UDS) 与上游集群通信
Envoy Proxy 在大多数情况下都是作为 Sidecar 与应用部署在同一网络环境中,每个应用只需要与 Envoy(localhost)交互,不需要知道其他服务的地址.然而这并不是 Envoy ...
- Unix domain socket 简介
Unix domain socket 又叫 IPC(inter-process communication 进程间通信) socket,用于实现同一主机上的进程间通信.socket 原本是为网络通讯设 ...
- mysql unix domain socket and network socket, ssh key
当主机填写为localhost时mysql会采用 unix domain socket连接 当主机填写为127.0.0.1时mysql会采用tcp方式连接 这是linux套接字网络的特性,win平台不 ...
- linux一切皆文件之Unix domain socket描述符(二)
一.知识准备 1.在linux中,一切皆为文件,所有不同种类的类型都被抽象成文件(比如:块设备,socket套接字,pipe队列) 2.操作这些不同的类型就像操作文件一样,比如增删改查等 3.主要用于 ...
- Unix domain socket IPC
UNIX Domain socket 虽然网络socket也可用于同一台主机的进程间通讯(通过lo地址127.0.0.1),但是unix domain socket用于IPC更有效率:不需要经过网络协 ...
随机推荐
- spark中各种连接操作以及有用方法
val a = sc.parallelize(Array(("123",4.0),("456",9.0),("789",9.0)) val ...
- CentOS 配置 ssh
默认安装ssh是有的.只是hosts访问问题. 1.在hosts.deny文件尾添加sshd:ALL意思是拒绝所有访问请求 [root@localhost ~]# vi /etc/hosts.de ...
- 平衡树 - 红黑树(JQuery+Js+Canvas版本的,帮助大家理解)
红黑树 1.红黑树介绍 年写的一篇论文中获得的.它是复杂的,但它的操作有着良好的最坏情况运行时间,并且在实践中是高效的:它可以在O(log n)时间内做查找,插入和删除,这里的n是树中元素的数目. 2 ...
- [Unity3D]Unity3D游戏开发之《愤慨的小鸟》弹弓实现
各位朋友,大家晚上好, 我是秦元培.欢迎大家关注我的博客,我的博客地址是blog.csdn.net/qinyuanpei.今天我们来做一个高端大气上档次的东西. 我相信大家都玩过一款叫做<愤慨的 ...
- cocos2d-x游戏开发 跑酷(两) 物理世界
原创.转载请注明出处:http://blog.csdn.net/dawn_moon/article/details/21240343 泰然的跑酷用的chipmunk物理引擎.我没有细致学过这个东西. ...
- JQuery - 留言之后,不重新加载数据,直接显示发表内容
留言板中,发表信息的时候,使用Ajax存储到后台数据库,如果存储成功,不重新加载数据库,直接显示发表内容. 代码: var Nicehng = ''; var kkimgpath = ''; var ...
- ASP.NET - 服务器控件button 先执行js 再执行后台的方法
关于button这个服务器控件,我一直想减少它向服务器提交数据.那些检测,还是在客户端实现就好了.这就需要javascript,但是我发现仅仅有javascript还是不够的.button服务器控件的 ...
- vim 操作指令2
VIM命令大全 光标控制命令 命令 光标移动 h 向左移一个字符 j 向下移一行 k 向上移一行 l 向右移一个字符 G 移到文件的最后一行 w 移到下一个字的开头 W 移到下一个字的开头,忽略标点符 ...
- C#递归复制文件夹
/// <param name="sources">原路徑</param> /// <param name="dest">目 ...
- [置顶] oracle 快速查询数据库各种信息、及转换对应java代码
1 查询表中数据量 select 'select '||''''||t.TABLE_NAME||''''||' as table_name, count(*) from '|| t.TABLE_NAM ...