Using Af_unix to implement native data flow communication

Source: Internet
Author: User

Using Af_unix to implement native data flow communication

This article turns from: http://blog.csdn.net/wzw88486969/article/details/42041955

Program Description:
The program contains the server and client two programs, which use Af_unix to implement the local data flow letter. Using the Af_unix domain actually communicates using a local socket file.

Server-side code:
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
int main (int argc, char * argv [])
{
int SERVER_SOCKFD, CLIENT_SOCKFD;
int Server_len, Client_len;
struct Sockaddr_un server_address; /* Declare a UNIX domain socket structure * *
struct Sockaddr_un client_address;
int i, bytes;
Char Ch_send, CH_RECV;
Unlink ("Server_socket"); /* Delete Original Server_socket Object * *
* * Create socket, communication protocol for Af_unix, sck_stream data mode * *
SERVER_SOCKFD = socket (Af_unix, sock_stream, 0);
/* Configure server information (communication protocol) * *
Server_address. sun_family = Af_unix;
/* Configure server information (socket object) * *
strcpy (server_address sun_path, "Server_socket");
/* Configure server information (server address length) * *
Server_len = sizeof (server_address);
/* Bind socket Object/*
Bind (SERVER_SOCKFD, (struct sockaddr *) & server_address, Server_len);
/* Listening Network, queue number is 5*/
Listen (SERVER_SOCKFD, 5);
printf ("Server is waiting for client connect ... \ n");
Client_len = sizeof (client_address);
/* Accept client requests; The 2nd parameter is used to store the client address; The 3rd parameter is used to store the size of the client address * *
/* Establish (return) a file descriptor to the client to read and write to the client.
CLIENT_SOCKFD = Accept (SERVER_SOCKFD, struct sockaddr *) & Server_address, (socklen_t *) & Client_len);
if (CLIENT_SOCKFD = = 1) {
Perror ("accept");
Exit (Exit_failure);
}
printf ("The server is waiting for client data ... \ n");
for (i = 0, ch_send = ' 1 '; i < 5; i + +, Ch_send + +) {
if ((bytes = Read (CLIENT_SOCKFD, & Ch_recv, 1)) = = 1) {
Perror ("read");
Exit (Exit_failure);
}
printf ("The character receiver from client is%c", CH_RECV);
Sleep (1);
if (bytes = Write (CLIENT_SOCKFD, & ch_send, 1) = = 1) {
Perror ("read");
Exit (Exit_failure);
}
}
Close (CLIENT_SOCKFD);
Unlink ("Server Socket");
}



Client code:
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>

int main (int argc, char * argv [])
{
struct Sockaddr_un address;
int sockfd;
int Len;
int i, bytes;
int result;
Char Ch_recv, ch_send;

/* Create Socket,af_unix Communication protocol, SOCK_STREAM data mode * *
if ((SOCKFD = socket (Af_unix, sock_stream, 0)) = = 1) {
Perror ("socket");
Exit (Exit_failure);
}

Address. sun_family = Af_unix;
strcpy (address. Sun_path, "Server_socket");
Len = sizeof (address);

/* Send a connection request to the server * *
result = Connect (SOCKFD, (struct sockaddr *) & address, Len);
if (result = = 1) {
printf ("ensure");
Perror ("Connect");
Exit (Exit_failure);
}

for (i = 0, ch_send = ' A '; i < 5; i + +, Ch_send + +) {

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.