memcpy:unix套接字的sun_路径的起始地址索引
因此,我有如下unix套接字初始化代码memcpy:unix套接字的sun_路径的起始地址索引,c,sockets,memcpy,C,Sockets,Memcpy,因此,我有如下unix套接字初始化代码 #define IETADM_NAMESPACE "IET_ABSTRACT_NAMESPACE" struct sockaddr_un addr; memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_LOCAL; memcpy((char *) &addr.sun_path + 1, IETADM_NAMESPACE, strlen(IETADM_NAMESPACE)); 我得
#define IETADM_NAMESPACE "IET_ABSTRACT_NAMESPACE"
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_LOCAL;
memcpy((char *) &addr.sun_path + 1, IETADM_NAMESPACE, strlen(IETADM_NAMESPACE));
我得到了memcpy
从索引地址开始的IETADM\u名称空间副本,即&addr.sun\u路径+1
我的问题是关于&addr.sun\u路径+1
expr的+1
部分
为什么要增加地址并在那里复制字符串,而不是仅仅复制
&addr.sun\u path
?有答案:
Address format
[...]
abstract: an abstract socket address is distinguished (from a
pathname socket) by the fact that sun_path[0] is a null byte
('\0'). 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. When the address of an abstract socket is returned,
the returned addrlen is greater than sizeof(sa_family_t) (i.e.,
greater than 2), and the name of the socket is contained in the
first (addrlen - sizeof(sa_family_t)) bytes of sun_path. The
abstract socket namespace is a nonportable Linux extension.
根据,有三种类型的地址可以通过sockaddr\u un
结构来区分:路径名、未命名和摘要
您显示的代码显示,在memcpy
之后,sun\u path
成员的第一个字节将为'\0'
,因为前面的memset
。引用手册页的相关部分:
* abstract: an abstract socket address is distinguished (from a
pathname socket) by the fact that sun_path[0] is a null byte
('\0'). 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. When the address of an abstract socket is returned,
the returned addrlen is greater than sizeof(sa_family_t) (i.e.,
greater than 2), and the name of the socket is contained in the
first (addrlen - sizeof(sa_family_t)) bytes of sun_path. The
abstract socket namespace is a nonportable Linux extension.
好的,理解它是一个抽象的套接字地址。但现在我的下一个问题是,为什么我们需要抽象套接字,为什么不使用常规unix套接字。是否有相同的原因。@drt:在某些情况下,您的进程没有读写公共目录的权限。在这些情况下,抽象名称空间是一个选项。使用抽象名称空间创建套接字,给它一个名称,内核将把这个套接字保存在内存中。其余的都应该以完全相同的方式工作。该名称充当标识符-两个愿意通信的进程应该引用相同的名称。