UNIX domain Socket binding path does not create file problem [C language]__c language

Source: Internet
Author: User
Tags unix domain socket

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.