在使用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
#includestruct sockaddr_un un; memset(&un, 0, sizeof(un); un.sun_family = AF_UNIX; un.sun_path[0] = ' '; strcpy(un.sun_path + 1, "hello");



