Socket for Linux network programming (15) UNIX domain socket programming and SOCKETPAIR functions

Source: Internet
Author: User
Tags bind socket socket error unix domain socket port number

First, UNIX Domain Socket IPC

The socket API was originally designed for network communications, but later on the socket framework developed an IPC mechanism, is the UNIX Domain socket. Although network sockets can also be used for interprocess communication with the same host (via the loopback address 127.0.0.1), UNIX Domain sockets are more efficient for IPC: No network protocol stack is required, no package unpacking, calculation checksum, maintenance sequence number and answer, etc. , just copy application-tier data from one process to another. UNIX domain sockets are twice times faster than TCP sockets at the same host as the former. This is because the IPC mechanism is inherently reliable communication, and network protocols are designed for unreliable communications. UNIX Domain sockets also provide both flow-oriented and packet-oriented API interfaces, similar to TCP and UDP, but message-oriented Unixdomain sockets are also reliable, and messages are neither lost nor sequentially disordered.

The process of using a UNIX Domain socket is very similar to the network socket, but you also need to call the socket () to create a socket file descriptor, and the address family specified as Af_unix,type can be selected Sock_ The Dgram or Sock_stream,protocol parameter is still specified as 0.

UNIX domain socket and network socket programming the most obvious difference is that the address format is different, with the structure of Sockaddr_un, network programming socket address is the IP address plus port number, and Unix Domain The socket's address is the path of a socket type file in the file system, created by bind () invocation, and the bind () error is returned if the file already exists when bind () is invoked.

#define UNIX_PATH_MAX 108

struct Sockaddr_un {

sa_family_tsun_family; * Af_unix * *

Charsun_path[unix_path_max]; * Pathname * *

};

Second, the return/client server program

The flow of communication is similar to the previous TCP/UDP, and the following is a direct look at the program:

/************************************************************************* > File name:echoser_tcp.c > Aut Hor:simba > Mail:dameng34@163.com > Created time:sun Mar 2013 06:13:55 PM CST *********************** /#include <stdio.h> #include <stdlib.h> #include < unistd.h> #include <errno.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/
        in.h> #include <string.h> #include <sys/un.h> #define ERR_EXIT (m) \ Do {\ perror (m); \ Exit (Exit_failure);
    \} while (0) void Echo_ser (int conn) {char recvbuf[1024];
    int n;
        while (1) {memset (recvbuf, 0, sizeof (RECVBUF));
        n = Read (conn, recvbuf, sizeof (RECVBUF));
    
            if (n = = 1) {if (n = = eintr) continue;
        Err_exit ("read error"); else if (n = = 0) {
            printf ("Client close\n");
        Break
        } fputs (Recvbuf, stdout);
    Write (conn, recvbuf, strlen (RECVBUF));
Close (conn); /* The UNIX domain socket is twice times faster than the TCP socket compared to the same host.
    */int main (void) {int listenfd;
    
    if ((LISTENFD = socket (Pf_unix, sock_stream, 0)) < 0) Err_exit ("Socket error"); Unlink ("/tmp/test socket");
    Address multiplexing struct Sockaddr_un servaddr;
    memset (&servaddr, 0, sizeof (SERVADDR));
    servaddr.sun_family = Af_unix;
    
    strcpy (Servaddr.sun_path, "/tmp/test socket");
    
    if (Bind (LISTENFD, (struct sockaddr *) &servaddr, sizeof (SERVADDR)) < 0) Err_exit ("Bind error");
    
    if (Listen (LISTENFD, Somaxconn) < 0) Err_exit ("Listen error");
    int conn;
    
    pid_t pid;
        while (1) {conn = accept (LISTENFD, NULL, NULL);
         if (conn = = 1) {if (conn = = eintr) continue;   Err_exit ("Accept error");
        PID = fork ();
        if (PID = = 1) err_exit ("fork Error");
            if (PID = = 0) {close (LISTENFD);
            Echo_ser (conn);
        Exit (exit_success);
    Close (conn);
return 0; }

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.