Socket IPC)

Source: Internet
Author: User

The socket IPC is consistent with the general network communication. There are two types of sock_stream and sock_daram, which are different from the following:

1. the network communication server uses IP/port as the service name, And the IPC uses the socket file path name as the service name. The socket file is different from the general file, and the socket file is created when the program is running, but cannot be created manually.

2. Different domain names: af_inet; af_unix; sockaddr_in and sockaddr_un. struct sockaddr_un {int sun_family; char sun_path [108];}

3. the client and the server can bind a file path. After binding, a socket file is generated. This file does not have any effect. You can delete it directly without affecting communication.

// Server end

# Include <stdio. h>

# Include <stdlib. h>
# Include <sys/socket. h>
# Include <sys/UN. h>
# Include <stddef. h>
# Include <unistd. h>
Int bindsocket (char * servname)
{
Struct sockaddr_un usock;
Int UFD;
Memset (& usock, 0, sizeof (sockaddr_un ));
Usock. sun_family = af_unix;
Strcpy (usock. sun_path, servname );
If (UFD = socket (af_unix, sock_stream, 0) <0)
{
Perror ("socket () error! ");
Exit (-1 );
}
Int Len = offsetof (sockaddr_un, sun_path) + strlen (usock. sun_path );
If (BIND (UFD, (sockaddr *) & usock, Len) <0)
{
Perror ("BIND () error! ");
Exit (-1 );
}

Return UFD;
}
Int acceptsocket (INT ssock, sockaddr_un * UN)
{

Int csock, Len;

Len = sizeof (un); // This statement must exist, because Len is an outgoing parameter. If no size is specified during input, it is possible to pass in the parameter value of the last outgoing parameter.

If (csock = accept (ssock, (sockaddr *) UN, (socklen_t *) & Len) <0)
{
Perror ("accept () error! ");
Exit (-1 );
}
Len-= offsetof (sockaddr_un, sun_path );
Un-> sun_path [Len] = 0;
Printf ("client file: % s \ n", un-> sun_path );
Return csock;

}

Int main (void)
{
Char servname [20];
Sprintf (servname, "server ");
Int SFD = bindsocket (servname );
If (Listen (SFD, 5) <0)
{
Perror ("Listen () error! ");
Exit (-1 );
}
Struct sockaddr_un;
Int CFD = acceptsocket (SFD, & UN );
Char Buf [100];
Int S = read (CFD, Buf, sizeof (BUF ));
Write (stdout_fileno, Buf, S );

}

// Client

# Include <stdio. h>
# Include <sys/socket. h>
# Include <stdlib. h>
# Include <unistd. h>
# Include <string. h>
# Include <sys/UN. h>
# Include <stddef. h>
Int cli_connect (const char * name)
{
Int Len;
Struct sockaddr_un;
Memset (& UN, 0, sizeof (un ));
Un. sun_family = af_unix;
Sprintf (UN. sun_path, "client % 05d", getpid ());
Int CFD;
If (CFD = socket (af_unix, sock_stream, 0) <0)
{
Perror ("socket () error ");
Exit (-1 );
}
Len = offsetof (sockaddr_un, sun_path) + strlen (UN. sun_path );
If (BIND (CFD, (sockaddr *) & UN, Len) <0)
{
Perror ("BIND () error ");
Exit (-1 );
}
Memset (& UN, 0, sizeof (un ));
Un. sun_family = af_unix;
Sprintf (UN. sun_path, name );
Len = offsetof (sockaddr_un, sun_path );
Len + = strlen (UN. sun_path );
Printf ("% s \ n", UN. sun_path );
If (connect (CFD, (sockaddr *) & UN, Len) <0)
{
Perror ("Connect () error ");
Exit (-1 );
}
Return CFD;
}
Int main (void)
{
Int CFD = cli_connect ("QW/Server ");
Char * Buf = "Hello world! ";
If (write (CFD, Buf, strlen (BUF) <0)
Perror ("error ");
Return CFD;
}

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.