Linuxipc--unix Domain Socket __linux

Source: Internet
Author: User
Tags unix domain socket
Turn from: http://blog.csdn.net/todd911/article/details/20285711
1. Non-named UNIX domain sockets

UNIX sockets are used to communicate between processes running on a single machine. UNIX sockets are more efficient than Internet domain sockets. UNIX and sockets provide and

Datagram two kinds of interface, UNIX domain datagram service is reliable, will not lose the message and will not pass error. A UNIX domain socket is a mixture of sockets and pipes.

To create a pair of unnamed, interconnected Unxi domain sockets, the user can use the Socketopair function.

#include <sys/socket.h>
int socketpari (int domain, int type, int protocol, int sockfd[2]);//Joschengong returns 0, error returns-1.

Practice:

 #include <stdio.h> #include <stdlib.h> #include <sys/socket.h> #include <
        string.h> int main (void) {int fd[2];
        int pid;
        Char wbuf[16] = "1234567890";
        Char rbuf[16] = "n";
                if (Socketpair (Af_unix, sock_stream, 0, FD) < 0) {perror ("Socketpair");
        return-1;
                The IF (PID = fork ()) <0) {perror ("fork");
        return-1;
                }else if (pid = = 0) {//child close (fd[0));
                        if (Write (Fd[1],wbuf,strlen (WBUF)) < 0) {perror ("write");
                Exit (-1);
                }}else{//parent Close (fd[1]);
                        if (read (fd[0],rbuf,16) < 0) {perror ("read");
                Exit (-1);
        printf ("%s\n", rbuf);
return 0; }

Execution results:

yan@yan-vm:~$./a.out
1234567890
2. Name UNIX domain sockets

Although the Socketpair function creates a pair of sockets that are interconnected, each socket has no name. This means that unrelated processes cannot use them.

We can name the UNIX domain socket and use it for the notice service. Note, however, that Unxi and sockets use different addresses than Internet domain sockets.

The address of a UNIX domain socket is represented by the SOCKADDR_UN structure.

In linux2.4.22, the SOCKADDR_UN structure is defined in file <sys/un.h> in the following form.

struct sockaddr_un{

sa_family_t sun_family; Af_unix

Char sun_path[108]; Pathname

};

Sun_path members contain all the names, and when we bind an address to a UNIX domain socket, the system uses the pathname to create a s_ifsock file.

This file is used only to inform the client process of the socket name. The file cannot be opened, nor can it be used by the application for communication.

If the file already exists when we attempt to bind the address, the bind request fails. When the socket is closed, the file is not automatically deleted, so we must

Make sure that you perform a unlink operation on the file before the application terminates.

The server process can use the standard bind, listen, and accept functions to schedule a unique UNIX domain connection for the client process. Client process using Connect and server

Process connection; Once the server process has accepted the Connect request, a unique connection exists between the server process and the client process. This style and Internet sockets

The operation is very similar.

Practice:

Server.c

#include <stdio.h> #include <sys/socket.h> #include <sys/un.h> #include <string.h> int main (
        void) {int fd,clientfd;
        int client_addr_len = sizeof (struct sockaddr_un);
        struct Sockaddr_un Un,clientun;
        Char path[32]= "Serversocketfile";
        int addr_len = sizeof (struct sockaddr_un);
        Char rbuf[32];

        Char wbuf[32] = "I am server."; Create a UNIX domain stream socket if (FD = socket (af_unix,sock_stream,0)) < 0) {perror ("sock
                ET ");
        return-1;

        //in case it already exists unlink (path);
        Fill in socket address structure memset (&un, 0, sizeof (un));
        un.sun_family = Af_unix;

        strncpy (un.sun_path,path,32); Bind the name to the descriptor if (Bind (FD, (struct sockaddr*) &un, Addr_len) < 0) {perror
                ("bind");
        return-1; if (Listen (FD) < 0) {perror ("listen");
        return-1;
                } if ((Clientfd = Accept (FD, (struct sockaddr*) &clientun, (socklen_t*) &client_addr_len)) < 0) {
                Perror ("accept");
        return-1;

        printf ("Client is:%s\n", Clientun.sun_path);
                if (read (clientfd,rbuf,32) < 0) {perror ("read");
        return-1;

        printf ("Receive msg:%s\n", rbuf);
                if (Write (Clientfd,wbuf,strlen (WBUF) +1) < 0) {perror ("write");
        return-1;
        } unlink (path);
return 0; }

Client.c
#include <stdio.h> #include <sys/un.h> #include <sys/socket.h> #include <string.h> int main (
        void) {int fd;
        struct Sockaddr_un un;
        Char path[32] = "Clientsocketfile";
        Char serverpath[32] = "Serversocketfile";
        int addr_len = sizeof (struct sockaddr_un);
        Char wbuf[32] = "I am client.";

        Char rbuf[32];
                if (FD = socket (af_unix,sock_stream,0)) <0) {perror ("socket");
        return-1;
        } memset (un); &un,0,sizeof
        un.sun_family = Af_unix;
        strncpy (un.sun_path,path,32);

        unlink (path);
                if (Bind (FD, (struct sockaddr*) &un,addr_len) <0) {perror ("bind");
        return-1;
        //fill socket adress structure with server's address memset (&un,0,sizeof (un));
        un.sun_family = Af_unix;

        strncpy (un.sun_path,serverpath,32); if (Connect (fd, (struct sockaddr*) &un,addr_len) < 0) {perror ("connect");
        return-1;
                } if (Write (Fd,wbuf,strlen (WBUF) +1) <0) {perror ("write");
        return-1;
                } if (read (fd,rbuf,32) < 0) {perror ("write");
        return-1;
        printf ("Receive msg:%s\n", rbuf);
        unlink (path);
return-1; }

Run server first, then run client. Run Result:

SERVER.C:

yan@yan-vm:~/apue$./unixserver
Client Is:clientsocketfile
Receive msg:i AM client.

CLIENT.C:

yan@yan-vm:~/apue$./unixclient
Receive MSG:I AM server.

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.