Linux Learning: UNIX domain protocol

Source: Internet
Author: User
Tags unix domain socket

Preface Introduction:

1. UNIX domain sockets, compared to TCP sockets, transmit four degrees to the same host twice times the latter

2.UNIX domain sockets can pass descriptors between processes on the same host

The difference between a 3.UNIX domain socket and a traditional socket is a description of the protocol family using the pathname.

UNIX Domain Address structure

#define Unix_path_max  108struct  sockaddr_un{    sa_family_t sun_family;        /*   Af_unix* *    Char           Sun_path[unix_path_max];     /* Pathname */ }

Now let's write a simple back-up server to Practice

#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<errno.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/inch.h>#include<arpa/inet.h>#include<sys/un.h>#defineErr_exit (M) Do{perror (M);    Exit (Exit_failure); } while(0)voidEcho_srv (intconn) {    Charrecvbuf[1024x768]; intret;  while(1) {memset (Recvbuf,0,sizeof(RECVBUF)); RET= Read (conn, recvbuf,1024x768); if(Ret ==-1)        {            if(errno = =eintr)Continue; Err_exit ("Read"); }Else if(ret = =0) {close (conn); printf ("Client close\n");        } fputs (Recvbuf, stdout);    Write (conn, recvbuf, ret); }intMainintargcConst Char*argv[]) {    //use UNIX domain sockets, which are different from TCP when requesting sockets//need to use a Pf_unix macro instead of Pf_inet    intLISTENFD = socket (Pf_unix, Sock_stream,0); if(LISTENFD <0) Err_exit ("SoCKET"); structSockaddr_un servaddr; memset (&AMP;SERVADDR,0,sizeof(SERVADDR)); Servaddr.sun_family=Af_unix; strcpy (Servaddr.sun_path,"Test_socket"); if(Bind (LISTENFD, (structsockaddr*) &servaddr,sizeof(SERVADDR)) <0) Err_exit ("Bind"); if(Listen (LISTENFD, Somaxconn) <0) Err_exit ("Listen"); intConn;    pid_t pid;  while(1) {Conn=Accpet (LISTENFD, NULL, NULL); if(conn = =-1)        {            if(errno = =eintr)Continue; Err_exit ("Accept"); } PID=Fork (); if(PID = =-1) Err_exit ("Fork"); if(PID = =0) {close (LISTENFD);            ECHO_SRV (conn);        Exit (exit_success); }Else if(PID >0) {close (conn); }    }    return 0}

Attention:

1.bind success will create a file with permissions of 0777, & umask = 755

2.sun_path better use an absolute path

3.UNIX domain protocol supports streaming sockets and report-style sockets

4.UNIX and streaming sockets connect will immediately return a econnrefused when the listener queue is full, which differs from TCP

If the listening queue is full, the incoming SYN is ignored, which causes the other party to retransmit the SYN;

Linux Learning: UNIX domain protocol

Related Article

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.