UNIX domain Socket binding address, you need to specify a file path, there are two scenarios:
(1) The socket automatically creates a file path corresponding to the file specified by bind
(2) Do not need to create a socket file, only need to name a global name for the client to connect according to this name. The latter implementation process differs from the former in that the latter must place the first byte at 0, i.e. sun_path[0] = 0, when assigning values to the members of the address structure Sun_path.
The following code explains:
First, create a file path
Address structure settings:
Name the server socket
server_addr.sun_family = Af_unix;
strcpy (Server_addr.sun_path, "/tmp/test1");
Server_len = sizeof (struct sockaddr_un);
Client_len = Server_len;
The second type. Do not create files
Address structure settings
Name the server socket
server_addr.sun_family = Af_unix;
Server_addr.sun_path[0] = ' 0 '
strcpy (Server_addr.sun_path + 1, "/tmp/test2");
Server_len = strlen (server_name) + offsetof (Sockaddr_un, Sun_path);
1. The 2 UNIX domain socket binding paths were created using the above two address structure settings:
/tmp/test1
/tmp/test2
2. In Linux down these 2 paths to find the corresponding file, found that the first 1 does not exist, only the last path corresponding to the file exists,
By netstat the socket state, you can see that the first three sockets correspond to the path preceded by the @ mark
(1) Existence @ Mark does not create file
(2) does not exist @ Mark creates file
[root@usr]# Netstat
Active Internet connections (w/o servers)
Proto recv-q send-q Local address Foreign
TCP 0 0 192.168.150.151:SSH 192.168.150.254:35087 established
TCP 0 0 192.168.150.151:SSH 192.168.150.254:31958 established
TCP 0 0 192.168.150.151:SSH 192.168.150.254:32175 established
TCP 0 0 192.168.150.151:SSH 192.168.150.254:35016 established
TCP 0 0 192.168.150.151:SSH 192.168.150.254:35032 established
TCP 0 0 192.168.150.151:SSH 192.168.150.254:35020 established
TCP 0 0 192.168.150.151:SSH 192.168.150.254:32023 established
TCP 0 0 192.168.150.151:SSH 192.168.150.254:35177 established
Active UNIX domain sockets (w/o servers)
Proto refcnt Flags Type State I-node Path
UNIX 2 [] Dgram 33744496/tmp/test1
UNIX 2 [] dgram 33744711 @/tmp/test2
Attention:
The above is the C language, if it is python that seems to create the file unconditionally