UNIX domain Protocol

Source: Internet
Author: User
Tags posix

Http://blog.chinaunix.net/u3/94142/showart_1900042.html

The Unix domain protocol is not an actual protocol family, but a method for executing Client/Server Communication on a single host. It is actually one of IPC (interprocess communication, by the way, inter-process communication can be implemented in the following ways: pipelines (Half Duplex), fifos (name management), Stream pipelines (Full Duplex), command Stream pipelines, message queues, semaphores, shared storage, set interface, and stream. The first two types are usually restricted to communication between processes on the same host. The last two types can be inter-process communication between different hosts. UNIX provides two types of interfaces: byte stream set interface and datagram set interface. There are three reasons for using the Unix domain interface: 1. In the implementation of the source Berkeley, the Unix domain interface is usually twice as fast as the TCP interface at both ends of the communication on the same host. 2. the Unix domain interface can be used to transmit descriptive words between different processes on the same host. 3. The new implementation of the UNIX domain interface provides the customer's creden (user ID and group ID) to the server, so as to provide additional security check measures. The Unix domain interface is defined in <sys/UN. h> struct sockaddr_un {sa_family_t sun_family;/* af_local */Char sun_path [104];/* null-terminated pathname */}; note that the path name stored in the sun_path array must end with an empty character. The sun_len macro provided by the implementation takes a pointer to the sockaddr_un structure as the parameter and returns the length of the structure, including the number of non-empty characters in the path name. We can use the following method to access the Unix interface and bind a path name to it: sockfd = socket (af_local, sock_stream, 0); unlink (argv [1]); memset (& ADDR, 0, sizeof (ADDR); ADDR. sun_family = af_local; strncpy (ADDR. sun_path, argv [1], sizeof (ADDR. sun_path)-1); BIND (sockfd, (struct sockaddr *) & ADDR, sun_len (& ADDR); here the first parameter argv [1] of the function is used as the path name, first, call the unlink function to prevent the file system from having this path name. If it exists, the BIND will become invalid. If this does not exist, unlink returns an error, which we ignore. In the strncpy function, the last character of sun_path must be null. Therefore, when copying, we subtract 1 to prevent the last character from being changed. The length of the BIND is obtained using sun_len. Note that POSIX specifies that the file access permission of the result path should be modified according to the value of unmask, that is, the value determines the read and write permissions of the created domain. Socketpair function # include <sys/socket. h> intsocketpair (INT family, int type, int protocol, int sockfd [2]); this function is only used for Unix domain interface, and its family must be af_local, the Protocol Parameter must be 0 and the type can be sock_stream. The result is called a Stream pipeline. It is similar to a common pipeline created by calling pipe. The difference is that the Stream pipeline is full-duplex, of course, it can also be sock_dgram. POSIX requirements for Unix domain interface 1. The default access permission for the path name created by BIND should be 0777, and is corrected according to the umask value. 2. The path name associated with the Unix domain interface should be an absolute path name rather than a relative path name. 3. The path name specified in the Connect call must be the path name currently bound to an open Unix domain interface. If the Connect call to a Unix domain interface finds that the queue of the listening interface is full, an econnrefused error is returned immediately after the call. This is different from TCP. If the TCP listener interface queue is full, the TCP listener ignores the newly arrived SYN, And the TCP connection sender sends SYN several times for retry. 4. Sending a datagram to an unbound Unix domain interface does not automatically bind a path name to this interface, which is different from UDP. Therefore, on the client side, we must also bind a path name to our set of interfaces.

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.