Af_unix domain communication (socket and pipe based communication, only suitable for UNIX system s&c on one host for process communication)

Source: Internet
Author: User
Tags sprintf

Server-side:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stddef.h>
Char buf[100];
void Main (void)
{int sersock,cltsock;
int Len,clen;
int ret=0;
struct Sockaddr_un seraddr,cltaddr;
Sersock=socket (af_unix,sock_stream,0);
if (sersock<0) {return;}
memset (&seraddr,0,sizeof (SERADDR));
Seraddr.sun_family=af_unix;
sprintf (Seraddr.sun_path, "Ser");
Unlink (Seraddr.sun_path);
Len = offsetof (struct Sockaddr_un, Sun_path) + strlen (Seraddr.sun_path);
if (0 > Bind (sersock, (struct sockaddr *) &seraddr, sizeof (struct sockaddr)) | | //
0 > Listen (sersock, 10))
{printf ("fail to bind and Lisen \ n");}
printf ("Start to listen connect request from UNIX socket name: \"%s\ "\ n", Seraddr.sun_path);
while (1)
{clen=sizeof (CLTADDR);
Cltsock=accept (Sersock, (struct sockaddr*) &cltaddr,&clen);
if (0>cltsock)
{printf ("ACCEPT error\n");}
Clen-= offsetof (struct sockaddr_un, sun_path);
Cltaddr.sun_path[clen] = 0;
while (1)
{
ret = recv (cltsock,buf,100, 0);
buf[ret]=0;
printf (BUF);
if (ret! = Send (Cltsock, Buf,ret, 0))
{printf ("Agentsrv Send Message Fail\n");}
}
}
Close (Cltsock);
Close (Sersock);
}

Client:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stddef.h>
Char buf[100];
void Main (void)
{
int Cltsock, Len, err;
struct Sockaddr_un cltaddr;
struct Sockaddr_un seraddr;
int ret;
Cltsock = socket (Af_unix, sock_stream, 0);
if (Cltsock < 0)
{
printf ("Socket creat error!!! \ n ");
Return
}
memset (&cltaddr, 0, sizeof (CLTADDR));
cltaddr.sun_family = Af_unix;
sprintf (Cltaddr.sun_path, "CLT");
Len = offsetof (struct Sockaddr_un, Sun_path) + strlen (Cltaddr.sun_path);
Unlink (Cltaddr.sun_path); If it already exists, delete it first
if (Bind (Cltsock, (struct sockaddr *) &cltaddr, sizeof (struct sockaddr)) < 0)//bind to create file path strength
{
printf ("Bind socket to addr fail\n");
Close (Cltsock);
Return
}

/* IF (chmod (Cltaddr.sun_path, S_irwxu) < 0)//Modify file permissions: Can not be required
{
printf ("Set%s rwx for user only fail\n", Cltaddr.sun_path);
Return
}
*/
Set the server-side address
memset ((char*) &seraddr, 0, sizeof (SERADDR));
seraddr.sun_family = Af_unix;
strcpy (Seraddr.sun_path, "Ser");
Len = offsetof (struct Sockaddr_un, Sun_path) + strlen ("Ser");
Connect the server end address with the client socket
if (Connect (cltsock, (struct sockaddr *) &seraddr, Len) < 0)
{
printf ("Connect Agent sock \"%s\ "fail\n", "Ser");
Return
}
while (1)
{printf ("Input a Line:");
Gets (BUF);
Len=strlen (BUF);
printf ("\nsend cnt:%d\n", Len);
if (len! = Send (Cltsock, buf, Len, 0))
{
printf ("Send msg fail\n");
return;
}
ret = recv (cltsock,buf,100, 0);
buf[ret]=0;
printf ("\nreceived:%s\n", buf);
}
Close (Cltsock);
}

This article from "Tech record" blog, declined reprint!

Af_unix domain communication (socket and pipe based communication, only suitable for UNIX system s&c on one host for process communication)

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.