NDK learns 16:unix domain socket

Source: Internet
Author: User
Tags unix domain socket


First,UNIX Domain Socket Concept:UNIX Domain Socket is an inter-process communication (IPC) for the same host developed on the socket architecture
Features:1. It does not need to go through the network protocol stack, do not need to package unpacking, calculate checksum, maintain serial number and answer, etc.2. Just copy the application layer data from one process to another.
operating mode:
Sock_dgram similar toUDP
sock_stream similar to TCP

use:UNIX domain sockets can be used for two unrelated processes, full-duplex, the most widely used IPC mechanism, such as the X Window server and GUI programs that are communicated through the UNIX Domain socket.

second, the work flow different points from the network socket:1.address family to Af_unix
2. Unix Domain socket does not need IP and port, instead of file path to represent "network address" principle :                        The unixdomain socket is represented by the struct Sockaddr_un, which is a socket type file in the file system path            The socket file is created by the bind () call and if the file already exists when bind () is called, the bind () error returns            
            the UNIX Domain socket client typically calls the BIND function explicitly, rather than relying on the automatically assigned address of the system like a network socket.             the socket file name of the client bind can contain the client's PID, so that the server can distinguish between different clients
Work Flow:Server-side: Create socket-binding file (port)-Listen-Accept Client Connection-receive/Send data —...— shutdown client: Create socket-bind file (port)-connect-send/Receive data —...— off

third, blocking and non-blocking (sock_stream mode)There are two modes of operation for read and write operations: blocking and non-blocking 1. Blocking modeIn blocking mode, the sending and receiving data parties behave as if they were named pipes
2. Non-blocking modeWhen you set msg_dontwait in the flag parameter of the Send or recv function, both the send and receive are returned. If not successful, the return value is -1,errno to Eagain or Ewouldblock
Four. Example Service-side code: #include <stdio.h> #include <sys/stat.h> #include <sys/socket.h> #include <sys/un.h> #include <errno.h> #include <stddef.h> #include <string.h> #include <unistd.h> #include <time.h> #include <stdlib.h> #define unix_domain "/data/local/tmp/ndk_cmd" int main(void) { int listen_fd = socket(Pf_unix, sock_stream, 0 ); If (Listen_fd < 0) { perror("Cannot create communication socket\n"); return 1;    } //set server Addr_param struct sockaddr_un srv_addr;srv_addr. sun_family = Af_unix; strncpy(srv_addr. Sun_path, unix_domain, sizeof(srv_addr. Sun_path)- 1); unlink (unix_domain); //bind sockfd & addr int ret = bind(listen_fd,struct sockaddr*) &srv_ addr, sizeof(srv_addr)); if (ret = = -1) { perror("Cannot bind server socket"); Close(LISTEN_FD); unlink(unix_domain); return 1;    } //listen sockfd ret = Listen(listen_fd, 1); if (ret = = -1) { perror("Cannot listen the client connect request"); Close(LISTEN_FD); unlink(unix_domain); return 1;    } //have Connect request use accept struct sockaddr_un clt_addr; int len = sizeof(CLT_ADDR); int com_fd = Accept(listen_fd,struct sockaddr*) &clt_addr, &am P;len); If (Com_fd < 0) { perror("Cannot accept client connect request"); Close(LISTEN_FD); unlink(unix_domain); return 1;    } //read and printf sent client info printf("/n=====info=====/n"); Static char recv_buf[1024x768];For (int i = 0; i < 4; i++) { memset(recv_buf, 0, 1024x768); int num = Read(com_fd, recv_buf, sizeof(recv_buf)); printf("Message from Client (%d)):%s\n", num, recv_buf);    } Close(COM_FD); Close(LISTEN_FD); unlink(unix_domain); return 0;}
Client code: #include <stdio.h> #include <stddef.h> #include <sys/stat.h> #include <sys/socket.h> #include <sys/un.h> #include <errno.h> #include <string.h> #include <unistd.h> #include <time.h> #include <stdlib.h> int main(void) { //creat UNIX sockets int connect_fd = socket(Pf_unix, sock_stream, 0 ); If (Connect_fd < 0) { perror("Cannot create communication socket"); return 1;    } static struct sockaddr_un srv_addr;srv_addr. sun_family = Af_unix; strcpy(srv_addr. Sun_path, "/data/local/tmp/ndk_cmd"); //connect Server int ret = Connect(connect_fd,struct sockaddr*) &srv_addr, sizeof(SRV_ADDR)); if (ret = = -1) { perror("Cannot connect to the server"); Close(CONNECT_FD); return 1;    } Char snd_buf[1024x768]; memset(snd_buf, 0, 1024x768); strcpy(snd_buf, "message from client"); //send Info Server For (int i = 0; i < 4; i++) Write(connect_fd, snd_buf, sizeof(snd_buf)); Close(CONNECT_FD); return 0;}
Operation Result:






From for notes (Wiz)

NDK learns 16:unix domain socket

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.