Unix Domain Socket Sockets implementation

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

The socket API was originally designed for network communication, but later developed an IPC mechanism on the socket frame, which is the unixdomain socket. Although the network socket can also be used for interprocess communication with the same host (via loopback address 127.0.0.1), the UNIX Domain socket is more efficient for IPC: it does not need to go through the network protocol stack, does not need to package unpacking, calculate checksum, maintain serial number and answer, etc. , just copy the application layer data from one process to another. This is because the IPC mechanism is inherently reliable communication, and network protocols are designed for unreliable communications. UNIX domain sockets also provide both stream-oriented and packet-oriented API interfaces, similar to TCP and UDP, but the message-oriented UNIX domain sockets are also reliable, and messages are neither lost nor sequenced.

UNIX domain socket is full-duplex, API interface is rich in semantics, compared to other IPC mechanisms have obvious advantages, has now become the most widely used IPC mechanism, such as X Window server and GUI program is through the UNIX Domain socket communication.

The process of using the UNIX Domain socket is very similar to the network socket, and the socket () is called to create a socket file descriptor, and address family is specified as Af_unix,type to select Sock_ The Dgram or Sock_stream,protocol parameter is still specified as 0.

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

Main Note process:

STREAM SOCKET:

Server:socket ()---> Bind ()---> Listen ()---> Accept ()

Client:scoket ()---> Connect ()

Refer to Article One article is enough: http://troydhanson.github.io/misc/Unix_domain_sockets.html

Write yourself a Server and a client:

[CPP]  View plain copy//server  //  //  unix_domain_server.c  //   UnixDomainServer  //  //&NBSP;&NBSP;CREATED&NBSP;BY&NBSP;GTLIU&NBSP;ON&NBSP;7/11/13.   //  copyright  (c)  2013 GT. All rights reserved.   //      #include   "MITLogModule.h"    #include  <stdio.h>    #include  <stdlib.h>   #include  <string.h>   #include  < errno.h>   #include  <stddef.h>   #include  <unistd.h>   # include <sys/socket.h>   #include  <sys/un.h>      #define  FUNC_FAIL                            -1   #define &NBSP;FUNC_ success                         0   #define  SOCKADDR_UN_SUN_PATH_MAX_LEN         103  // 104 - 1   #define  ser_accept_con_num                   1      /**   * Create a server endpoint of a  connection.   *   *  @param   scok_path: the unix domain  socket path   *  @return  return the file descirption if all  ok, <0 on err   */   Int create_serv_listen (Const char  *sock_path)    {       size_t path_len = strlen (sock _path);      if  (path_len == 0)  {            mitlogwrite (mitlog_level_error,  "Socket path can ' T be empty");            return FUNC_FAIL;        } else if  (Path_len > sockaddr_un_sun_path_max_len)  {           mitlogwrite (mitlog_level_error,  "socket path  length must less than%d ",  sockaddr_un_sun_path_max_len);            return FUNC_FAIL;       }               int fd = 0, size = 0;        struct sockaddr_un server_un;        memset (&server_un,&nbsP;0, sizeof (Server_un));       server_un.sun_family = AF_UNIX;        strncpy (server_un.sun_path, sock_path, sizeof (Server_un.sun_path )  - 1);              if  ((fd =  socket (af_unix, sock_stream, 0))  < 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.