UNIX域套接字(unix domain)
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <errno.h>
int main(void)
{
int fd,size;
struct sockaddr_un un;
un.sun_family = AF_UNIX; //unix域
strcpy(un.sun_path, "foo.socket");
if ((fd=socket(AF_UNIX, SOCK_STREAM, ))<) {
printf("socket failed\n");
exit(-);
}
size = sizeof(struct sockaddr_un);
if (bind(fd, (struct sockaddr *)&un, size) < ) {
printf("bind failed:[%s]\n",strerror(errno));
exit(-);
}
printf("UNIX domain socket bound\n");
exit();
}
# ls -l
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <time.h>
#include <stddef.h> #define QLEN 10
#define STALE 30 int main(void)
{
} //创建服务端,成功返回fd,错误返回值<0
int serv_listen(const char *name)
{
int fd,err,rval, len;
struct sockaddr_un un; if ((fd = socket(AF_UNIX, SOCK_STREAM, )) < )
return -;
unlink(name); //存在文件,先解除连接 //填充socket地址结构
memset(&un, , sizeof(un));
un.sun_family = AF_UNIX;
strcpy(un.sun_path, name); //绑定地址到描述符
if (bind(fd, (struct sockaddr *)&un, len) < ) {
rval = -;
goto errout;
} if(listen(fd, QLEN) < ) {
rval = -;
goto errout;
}
return(fd); errout:
err = errno;
close(fd);
errno = err;
return(rval);
} //等待客户连接,并接受它
//同时验证客户的身份
int serv_accept(int listenfd, uid_t *uidptr)
{
int clifd, rval, err;
socklen_t len;
struct sockaddr_un un;
struct stat statbuf;
time_t staletime; len = sizeof(un);
if ((clifd = accept(listenfd, (struct sockaddr *)&un, &len)) < )
return -; //确定客户进程的身份是该套接字的所有者
len -= offsetof(struct sockaddr_un, sun_path); //路径长
un.sun_path[len]=; //增加\0结束符 if (stat(un.sun_path, &statbuf) < ) {
rval = -;
goto errout;
} // 文件类型检查
if (S_ISSOCK(statbuf.st_mode)==) {
rval = -;
goto errout;
} // 文件权限检查
if((statbuf.st_mode & (S_IRWXG | S_IRWXO)) ||
statbuf.st_mode & S_IRWXU != S_IRWXU) {
rval = -;
goto errout;
} staletime = time(NULL) - STALE;
if (statbuf.st_atime < staletime ||
statbuf.st_ctime < staletime ||
statbuf.st_mtime < staletime) {
rval = -;
goto errout;
} if (uidptr != NULL)
*uidptr = statbuf.st_uid; //返回uid
unlink(un.sun_path);
return(clifd); errout:
err = errno;
close(clifd);
errno = err;
return rval;
}
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <string.h>
#include <sys/un.h>
#include <stddef.h> #define CLI_PATH "/var/tmp/"
#define CLI_PERM S_IRWXU int main(void)
{
exit();
} int cli_conn(const char *name)
{
int fd, len, err, rval;
struct sockaddr_un un; if ((fd = socket(AF_UNIX, SOCK_STREAM, )) < )
return -; //填充客户端地址
memset(&un, , sizeof(un));
un.sun_family = AF_UNIX;
sprintf(un.sun_path, "%s%05d", CLI_PATH, getpid());
len = offsetof(struct sockaddr_un, sun_path) + strlen(un.sun_path);
unlink(un.sun_path);
//绑定到套接字
if (bind(fd, (struct sockaddr *)&un, len) < ) {
rval = -;
goto errout;
} if (chmod(un.sun_path, CLI_PERM) < ) {
rval = -;
goto errout;
} //填充服务端地址
memset(&un, , sizeof(un));
un.sun_family = AF_UNIX;
strcpy(un.sun_path, name);
len = offsetof(struct sockaddr_un, sun_path) + strlen(un.sun_path);
if (connect(fd, (struct sockaddr *)&un, len) < ) {
rval = -;
goto errout;
}
return(fd); errout:
err = errno;
close(fd);
errno = err;
return(rval);
}
UNIX域套接字(unix domain)的更多相关文章
- UNIX 域套接字——UNIX domain socket
/*********************程序相关信息********************* * 程序编号:015 * 程序编写起始日期:2013.11.30 * 程序编写完成日期:2013.1 ...
- UNIX域套接字——UNIX domain socket(DGRAM)
#define UNIX_PATH_MAX 108 #include <sys/types.h> #include <sys/socket.h> #include <sy ...
- Unix域套接字(Unix Domain Socket)介绍【转】
本文转载自:http://blog.csdn.net/roland_sun/article/details/50266565 版权声明:本文为博主原创文章,未经博主允许不得转载. 在Linux系统中, ...
- 高级进程间通信之UNIX域套接字
UNIX域套接字用于在同一台机器上运行的进程之间的通信.虽然因特网域套接字可用于同一目的,但UNIX域套接字的效率更高.UNIX域套接字仅仅复制数据:它们并不执行协议处理,不需要添加或删除网络报头,无 ...
- 《网络编程》Unix 域套接字
概述 Unix 域套接字是一种client和server在单主机上的 IPC 方法.Unix 域套接字不运行协议处理,不须要加入或删除网络报头,无需验证和,不产生顺序号,无需发送确认报文,比因特网域套 ...
- UNIX网络编程——UNIX域套接字编程和socketpair 函数
一.UNIX Domain Socket IPC socket API原本是为网络通讯设计的,但后来在socket的框架上发展出一种IPC机制,就是UNIX Domain Socket.虽然网络soc ...
- UNIX域套接字编程和socketpair 函数
一.UNIX Domain Socket IPC socket API原本是为网络通讯设计的,但后来在socket的框架上发展出一种IPC机制,就是UNIX Domain Socket.虽然网络soc ...
- Unix域套接字简介
在Linux系统中,有很多进程间通信方式,套接字(Socket)就是其中的一种.但传统的套接字的用法都是基于TCP/IP协议栈的,需要指定IP地址.如果不同主机上的两个进程进行通信,当然这样做没什么问 ...
- unix进程间通信方式(下)-unix域套接字(转)
在之前的博客中已经总结了其它7种进程间的通信方式.unix域套接字用于在同一台计算机上的进程间通信,虽然因特网域套接字可用于同一目的,但是unix域套接字的效率更高.unix域套接字并不进行协议处理, ...
随机推荐
- C#.Net Mvc运营监控,计算方法/接口/action/页面执行时间
1.建立一个TimingActionFilter过滤器 public class TimingActionFilter : ActionFilterAttribute { public overrid ...
- npoi上传xlsx文件,并读取数据
视图 public PartialViewResult UploadIndex() { return PartialView(); } <div> <fieldset style=& ...
- Spring自定义缓存管理及配置Ehcache缓存
spring自带缓存.自建缓存管理器等都可解决项目部分性能问题.结合Ehcache后性能更优,使用也比较简单. 在进行Ehcache学习之前,最好对Spring自带的缓存管理有一个总体的认识. 这篇文 ...
- CRM Diagnostics CRM 2016 诊断
http://xxx.xxxxxx.xxx/Organization/tools/diagnostics/diag.aspx
- Linux disk_partition_dev_马士兵_note
一般装Linux会遇到的问题: 找不到硬件驱动 现在主流的一些硬件 不支持Linux驱动 尽量找主流的硬件,尽量找老一点的硬件 装系统: 1.记下 系统 ---->到时候要找驱动 2 ...
- UI和3D场景同时都需要响应触摸事件
比如战斗场景,UI和3D场景同时都需要响应触摸事件,如果同时响应可能就会出现触摸UI的时候影响到了3D部分.为了解决这个问题在判断3D响应之前要先判断手指是否点击在UI上. 以前NGUI的时候都是自己 ...
- Struts2基础使用教程:OGNL
取自<JAVAWEB整合开发王者归来> 是一种类似EL的语言,比EL强大的多 能访问对象的方法,例如list.size() 能访问静态属性与静态方法,方法是在类名前.方法前加上@.如@ja ...
- 剑指Offer:面试题31——连续子数组的最大和(java实现)
问题描述 : 输入一个整数数组,数组里面有正数也有负数.数组中一个或连续几个整数组成一个子数组.求所有子数组的和的最大值.要求时间复杂度为O(n) 思路1:常规解法,不知道怎么描述了.. 代码: bo ...
- 十三、File Translator怎么写
---恢复内容开始--- 1. File Translator可以将信息从maya中导入和导出. 2. 创建一个file translator需要从MPxFileTranslator继承. 3. 函数 ...
- Linux学习笔记——重点推荐的Linux网络在线学习资源
首先非常感谢百度,感谢网络的搜索引擎技术,也非常感谢学习资源的贡献者和组织! 1:http://billie66.github.io/TLCL/book/zh/ 2:http://www.ha97. ...