Introduction to Unix domain socket

Source: Internet
Author: User
Tags unix domain socket

First, using Unix domain socket has three advantages:
1) on the same host, Unix domain socket is twice faster than the average TCP socket. This is a major reason for performance.
2) Unix domain socket can transmit file descriptors between different processes on the same host.
3) the newer Unix domain socket implementation provides the customer ID and group ID to the server, allowing the server to perform security checks.

 

The Client/Server structure in the GUI system is usually implemented based on UNIX domain socket. For example, in the X Window System, before connecting to the X11 server, the X11 client first determines the host where the X11 server is located based on the settings of environment variables such as display. If the host is the same host, the Unix domain socket is used to connect to the server.

Basic Unix domain socket Process

Shows the basic process of communication using Unix domain socket:


Socket

Socket () creates an endpoint for communication and returns a descriptor.

Bind

BIND () gives the socket sockfd the local address my_addr. It is normally necessary to assign a local address using bind () before a sock_stream socket may receive connections.

Listen

To accept connections, a socket is first created with socket (), a willingness to accept incoming connections and a queue limit for incoming connections are specified with listen (), and then the connections are accepted with accept. the listen ()
Call applies only to sockets of Type sock_stream or sock_seqpacket.

Accept

The accept () system call is used with connection-based socket types (sock_stream, sock_seqpacket ). it extracts the first connection request on the queue of pending connections, creates a new connected socket, and returns a new file descriptor
Referring to that socket.

Connect

The CONNECT () system call connects the socket referred to by the file descriptor sockfd to the address specified by serv_addr.

Read and Write

After establishing a connection between two processes that communicate with each other, the data can be read and written through the Read and Write Functions.

Between read and write processes, the operating system kernel provides a data buffer; when the write function is called to write data, the data is written into the data buffer; when the READ function is called to read data, read data from the buffer zone. When the buffer zone is empty, the READ function will wait until there is data in the buffer zone. When the buffer is full, the write function waits until the buffer has free space.

Use with select

UNIX domain socket programming is often used with select. The select function is used to listen to sockets. When there are connection requests or existing connections have data to be read and written, the accept function is called to accept connection requests and establish connections, call read/write to read and write data.

The fdsets and its interfaces can be used to listen to multiple file descriptors by the SELECT statement. The SELECT statement returns the number of file descriptors in the ready state and determines whether a file descriptor is ready through the fd_isset interface.

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.