栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

unix socket抽象路径名

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

unix socket抽象路径名

在使用AF_UNIX编写本地套接字进行进程间通信的时候,我们需要对struct sockaddr_un中的sun_path域填充一个文件名,在bind的时候会自动创建一个S_IFSOCK类型的文件。如果文件存在就会报错。所以每次创建之前需要先把存在同名的文件删除。但是删除一个文件,可能是别的程序需要的,就会导致很多其他的问题。

Linux引入了一种称为抽象路径名的机制,这种方式不会真的创建一个文件,只会在虚拟文件系统中创建一个标识,可以很好的避免名字冲突,当套接字关闭的时候,会自动删除一个路径对应的标识,不需要手动删除。

抽象路径名的第一个字符必须是NULL(''), 后面填充抽象名。与普通路径名不同,普通路径名遇到NULL就结束了,而抽象路径名除了第一个字符之外,会把sun_path中的所有字符作为抽象名。

abstract: an abstract socket address is distinguished (from a
pathname socket) by the fact that sun_path[0] is a null byte
(’’). The socket’s address in this namespace is given by the
additional bytes in sun_path that are covered by the specified
length of the address structure. (Null bytes in the name have no
special significance.) The name has no connection with filesystem
pathnames

#include 
struct sockaddr_un un;

memset(&un, 0, sizeof(un);
un.sun_family = AF_UNIX;
un.sun_path[0] = '';
strcpy(un.sun_path + 1, "hello");
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/706077.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号