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域套接字并不进行协议处理, ...
随机推荐
- Flex air修改外部xml文件 (转)
AIR的文件操作不难,看完教程应该可以满足你对文件的所有基本操作.这篇教程主要以实际操作中遇到的情况来讲解 我们想想文件操作都会有什么内容,无非是创建,修改,删除,移动,拷贝.在这个过程中我们会涉及到 ...
- mysql 笔记
mysql配置主从复制的时候,不能将server-id设置成非数字,这样会导致mysqld启动失败. mysql重启的时候,自动会释放锁(这个锁应该是位于内存的) 执行sql脚本:source /ho ...
- java和h5 canvas德州扑克开发中(二)
德州扑克网页源码在github上分享 https://github.com/lxr1907/pokers 感兴趣的可以上去看下. 1.通讯使用websocket,主要在message.js中. 2.用 ...
- VS2013环境问题
1.多字节支持问题,多字节默认(GB2312格式),需要安装一个补丁: https://www.microsoft.com/zh-CN/download/confirmation.aspx?id=40 ...
- OS X升级El Capitan后,git difftool无法打开diffmerge的解决方法
在git项目下执行git difftool,出现如下报错 /Library/Developer/CommandLineTools/usr/libexec/git-core/mergetools/dif ...
- GAudio是一个音频播放SDK
gaudio是一个基于C/C++混合编程的跨平台音频库,当前支持windows32/64操作系统 免费使用 - 有问题和建议请联系 谢谢 修改记录1.2013.04.01 初次发布2.2013. ...
- OpenGL学习笔记1——第一个程序
学习的参考书基本是按照GL编程指南,如果有消息机制概念,对于GLUT的理解是很自然的.下面就按照自己写的第一个程序详细解释一下GL,还是比较容易上手的. 程序实现的功能是,根据当前随即种子摇出来的结果 ...
- Java中的闪光点:ThreadLocal是线程Thead的局部变量,可替代同步机制的设计,值得学习和研究
线程局部变量ThreadLocal,是Java支持的一种线程安全机制,目的是解决多线程的并发问题. 具体来讲,就是多个线程访问该实例对象的变量时,该实例对象将其存储为键值对的形式,保证各个线程(键)分 ...
- SQL语句的增删查改
一.增:有2种方法 1.使用insert插入单行数据: 语法:insert [into] <表名> [列名] values <列值> 例:insert into Strdent ...
- jsp Request获取url信息的各种方法比较
从Request对象中可以获取各种路径信息,以下例子: 假设请求的页面是index.jsp,项目是WebDemo,则在index.jsp中获取有关request对象的各种路径信息如下 String p ...