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域套接字并不进行协议处理, ...
随机推荐
- ios7 ios8 cell中下划线偏移(separator Insets)处理方法
在ios7中,UITableViewCell左侧会有默认15像素的空白.这时候,设置setSeparatorInset:UIEdgeInsetsZero 能将空白去掉. 但是在ios8中,设置setS ...
- 黄聪:路由器WIFI连接无法正常访问个别网站及发送图片
打开路由,路由默认MTU是1500,改成1472 就解决了
- SpringToolSuite/Eclipse中集成的Tomcat无法add Project时的解决版本
- eclipse插件
#eclipse market http://www.eclipse.org/mpc/archive.php http://download.eclipse.org/mpc/mars/ #文件路径 p ...
- mir [20161220]
最近玩backmir,查询了一些资料,突然领悟到原来各个地方的boss攻击和防御都有一定的上限,而相对应的,玩家也有攻击和防御,只要玩家的攻防能对付boss的攻防,就可以无伤打boss. 小时候玩热血 ...
- 【转】easyui $.message.alert 点击右上角的关闭按钮时,不执行定义的回调函数
今天發現這個問題 easyui $.message.alert 点击右上角的关闭按钮时,不执行定义的回调函数
- No Architectures to Compile for (ONLY_ACTIVE_ARCH=
No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=armv7, VA 运行报错 出现的原因:armv7s是应用在iP ...
- lighttpd配置
1.lighttpd.conf server.modules = ( "mod_access", "mod_alias", "mod_compress ...
- ASPNET_MVC学习中的疑问
1.在mvc..net4.5.Entity Framewor都提供了多种验证规则. 请问,其中不需要提交到服务器验证的验证,是否是在客户端就完成的,还是说像之前的aspnet一样,都得提交到服务器验 ...
- ajaxpro返回值类型总结-DataTable(转)
ajaxpro使用总结系列其他内容 ajaxpro ajaxmethod 重载调用问题 ajaxpro方法ajaxmethod调用示例 ajaxpro返回值类型总结-string,int ajaxpr ...