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域套接字并不进行协议处理, ...
随机推荐
- 换一个思路压缩图片,RGB转YUV
一般的压缩方案 做移动平台,终究都是要考虑纹理压缩的问题 IOS/PVR平台上一般会选用PVRTC格式,这个格式压缩还是很给力. Android上设备种类很多,支持的格式各有不同.如果平台能支持下载前 ...
- some code of c
// // main.c // LineList // // Created by Rubert on 16/9/11. // Copyright © 2016年 Study. All rights ...
- windows下安装xgboost
Note that as of the most recent release the Microsoft Visual Studio instructions no longer seem to a ...
- 【OpenCV】视频取坐标
今天实现了视频播放以后暂停获取鼠标点击处坐标的功能. #include <iostream> #include <opencv2/highgui/highgui.hpp> #i ...
- 03-c#入门(简易存款利息计算器v1.0)
本想把练习题做了的结果放上来,不过发现附录是有答案的,就算了吧,自己做了没问题就行了哈.之前提到过,要是有朋友有想法,需要做小工具我可以帮忙实现,不过貌似大家都很忙.SO,自己学完第4章后,决定做一个 ...
- Oracle 作业设置完不执行解决
在日常的工作当中,已经几次遇到Oracle数据库 建立了新的作业但是不执行的问题.写下来给大家分享一下. 我们日常在 dbms_job这个包建立了相关作业,但是到点也不会执行,在百度上看了一下 并且给 ...
- 一次性插入多条sql语句的几种方法
第一种:通过 insert select语句向表中添加数据 从现有表里面把数据插入到另外一张新表去前提必须先有test_2表的存在,并且test_2表中的列的数据类型必须和test表里面列的数据类型一 ...
- storm 配置,呵呵。
配置项 配置说明 storm.zookeeper.servers ZooKeeper服务器列表 storm.zookeeper.port ZooKeeper连接端口 storm.local.dir s ...
- flex-布局,轻松制作移动端网页
Flex 布局教程 网页布局(layout)是CSS的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display属性 + position属性 + float属性.它对于那些特殊布局非常不 ...
- MongoDB(七)MongoDb数据结构
首先,向数据库插入一条bjson数据 首先是定义文档,然后使用admin用户名密码登录,进入test数据库,向test数据库中插入此文档("表名称和表中的记录") 插入结果,查看m ...