linux-interprocess communication (quad): Domain sockets

Source: Internet
Author: User

1. Domain Sockets:

(1) can only be used for communication between different processes on the same device;

(2) efficiency is higher than network sockets. A domain socket is simply a copy of the data and does not go through the protocol stack;

(3) reliable, full duplex;

2. Domain Socket address structure:

struct Sockaddr_un {    /*af_unix*/    char sun_path[108/*  Pathname*/};

When we bind an address to a domain socket, the system creates a file of type S_ifsock with that pathname, tells the client name, does not open, and cannot communicate.

Do not automatically delete, need to delete the program after completion, if the file already exists, Bind failed;

3. Test the code:

Server.c

1#include <sys/types.h>2#include <sys/socket.h>3#include <sys/un.h>4#include <stdio.h>5#include <stdlib.h>6 7 #defineUn_path "/var/tmp/test_domain_socket"8 9 Ten intMainintargcChar*argv[]) One { A     intSOCKFD =-1; -     structsockaddr_un un; -     intLen =0; the  -     if(SOCKFD = socket (Af_unix, Sock_stream,0)) <0){ -Perror ("Create socket error\n"); -         Goto_error; +     } -  +memset (&un,0,sizeof(un)); Aun.sun_family =Af_unix; at strcpy (Un.sun_path, un_path); -  -Len =sizeof(un.sun_family) +sizeof(Un.sun_path); -  -     if(Bind (SOCKFD, (structSOCKADDR *) &un, Len) <0){ -Perror ("socket bind error\n"); in         Goto_error; -     } to  +     if(Listen (SOCKFD,5) <0){ -Perror ("Socket Listen error\n"); the     } *  $     intconn = Accept (SOCKFD, (structSOCKADDR *) &un, &len);Panax Notoginseng     if(Conn <0){ -Perror ("Accept error\n"); the         Goto_error; +     } A     Charbuf[ -] = {0}; the     intn = Read (conn, buf, the); +printf"recive-msg:%s", buf); - write (conn, buf, N); $ Close (conn); $  - Close (SOCKFD); - unlink (Un_path); the     return 0; - Wuyi _error: the     if(SOCKFD! =-1){ - Close (SOCKFD); Wu     } -  About     return-1; $}

Client.c

1#include <sys/types.h>2#include <sys/socket.h>3#include <sys/un.h>4#include <stdio.h>5#include <stdlib.h>6#include <unistd.h>7 8 #defineUn_path "/var/tmp/test_domain_socket"9 Ten  One intMainintargcChar*argv[]) A { -     intSOCKFD =-1; -     structsockaddr_un un; the     intLen =0; -  -     if(SOCKFD = socket (Af_unix, Sock_stream,0)) <0){ -Perror ("Create socket error\n"); +         Goto_error; -     } +  Amemset (&un,0,sizeof(un)); atun.sun_family =Af_unix; - strcpy (Un.sun_path, un_path); -  -Len =sizeof(un.sun_family) +sizeof(Un.sun_path); -  -     if(Connect (SOCKFD,structSOCKADDR *) &un, Len) <0){ inPerror ("Socket Connect error\n"); -         Goto_error; to     } +  -     Charbuf[ -] = {0}; the     intn = Read (Stdin_fileno, buf, the); * Write (SOCKFD, buf, n); $printf"send-msg:%s", buf);Panax NotoginsengRead (SOCKFD, buf, the); -buf[ -] =' /'; theprintf"recv-msg:%s", buf); +  A Close (SOCKFD); the     return 0; +  - _error: $     if(SOCKFD! =-1){ $ Close (SOCKFD); -     } -  the     return-1; -}

linux-interprocess communication (quad): Domain sockets

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.