linux 进程间通信系列2,使用UNIX_SOCKET

1,使用stream,实现进程间通信

2,使用DGRAM,实现进程间通信

关键点:使用一个临时的文件,进行信息的互传。

  s_un.sun_family = AF_UNIX;
strcpy(s_un.sun_path, "/tmp/afunix_text");

使用stream,server端:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h> #define FILEPATH "/tmp/afunix_text" int main(){
int s0, sock;
sockaddr_un s_un;
sockaddr_un s_un_accept;
socklen_t addrlen; s0 = socket(AF_UNIX, SOCK_STREAM, 0);
if(s0 < 0){
perror("socket");
return 1;
} s_un.sun_family = AF_UNIX;
strcpy(s_un.sun_path, FILEPATH); if(bind(s0, (sockaddr*)&s_un, sizeof(s_un)) != 0){
perror("bind");
return 1;
} if(listen(s0, 5) != 0){
perror("listen");
return 1;
} addrlen = sizeof(s_un_accept);
sock = accept(s0, (sockaddr*)&s_un_accept, &addrlen);
if(sock < 0){
perror("accept");
return 1;
} printf("after accept\n"); write(sock, "the msg is send from server", 27);
close(sock);
close(s0); unlink(FILEPATH); return 0;
}

github源代码

使用stream,client端:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h> int main(){
int sock;
sockaddr_un s_un;
int n;
char buf[128]; sock = socket(AF_UNIX, SOCK_STREAM, 0);
if(sock < 0){
perror("socket");
return 1;
} s_un.sun_family = AF_UNIX;
strcpy(s_un.sun_path, "/tmp/afunix_text"); if(connect(sock, (sockaddr*)&s_un, sizeof(s_un)) != 0){
perror("connect");
return 1;
}
printf("after connect\n");
memset(buf, 0, sizeof(buf));
n = read(sock, buf, sizeof(buf));
if(n < 0){
perror("read");
return 1;
} printf("%s\n", buf);
close(sock);
return 0;
}

github源代码

使用dgram,发送端:

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h> int main(){
int sock;
sockaddr_un addr;
socklen_t addrlen; sock = socket(AF_UNIX, SOCK_DGRAM, 0);
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, "/tmp/afu_dgram"); int n = sendto(sock, "HELLO\n", 6, 0, (sockaddr*)&addr, sizeof(addr));
printf("send data\n");
close(sock);
return 0;
}

github源代码

使用dgram,接收端:

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h> int main(){
int sock;
sockaddr_un addr;
socklen_t addrlen;
char buf[1024];
int n; sock = socket(AF_UNIX, SOCK_DGRAM, 0); addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, "/tmp/afu_dgram"); bind(sock, (sockaddr*)&addr, sizeof(addr)); while(1){
memset(buf, 0, sizeof(buf));
n = recv(sock, buf, sizeof(buf) - 1, 0);
printf("recv:%s\n", buf);
} close(sock);
return 0;
}

github源代码

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

c/c++ linux 进程间通信系列2,使用UNIX_SOCKET的更多相关文章

  1. c/c++ linux 进程间通信系列7,使用pthread mutex

    linux 进程间通信系列7,使用pthread mutex #include <stdio.h> #include <stdlib.h> #include <unist ...

  2. c/c++ linux 进程间通信系列6,使用消息队列(message queue)

    linux 进程间通信系列6,使用消息队列(message queue) 概念:消息排队,先进先出(FIFO),消息一旦出队,就从队列里消失了. 1,创建消息队列(message queue) 2,写 ...

  3. c/c++ linux 进程间通信系列5,使用信号量

    linux 进程间通信系列5,使用信号量 信号量的工作原理: 由于信号量只能进行两种操作等待和发送信号,即P(sv)和V(sv),他们的行为是这样的: P(sv):如果sv的值大于零,就给它减1:如果 ...

  4. c/c++ linux 进程间通信系列4,使用共享内存

    linux 进程间通信系列4,使用共享内存 1,创建共享内存,用到的函数shmget, shmat, shmdt 函数名 功能描述 shmget 创建共享内存,返回pic key shmat 第一次创 ...

  5. c/c++ linux 进程间通信系列3,使用socketpair,pipe

    linux 进程间通信系列3,使用socketpair,pipe 1,使用socketpair,实现进程间通信,是双向的. 2,使用pipe,实现进程间通信 使用pipe关键点:fd[0]只能用于接收 ...

  6. c/c++ linux 进程间通信系列1,使用signal,kill

    linux 进程间通信系列1,使用signal,kill 信号基本概念:  软中断信号(signal,又简称为信号)用来通知进程发生了异步事件.进程之间可以互相通过系统调用kill发送软中断信号.内核 ...

  7. Linux 进程间通信系列之 信号

    信号(Signal) 信号是比较复杂的通信方式,用于通知接受进程有某种事件发生,除了用于进程间通信外,进程还可以发送信号给进程本身:Linux除了支持Unix早期信号语义函数sigal外,还支持语义符 ...

  8. 进程间通信系列 之 socket套接字及其实例

    进程间通信系列 之 概述与对比   http://blog.csdn.net/younger_china/article/details/15808685  进程间通信系列 之 共享内存及其实例   ...

  9. 练习--LINUX进程间通信之消息队列MSG

    https://www.ibm.com/developerworks/cn/linux/l-ipc/part3/ 继续坚持,或许不能深刻理解,但至少要保证有印象. ~~~~~~~~~~~~~~ 消息队 ...

随机推荐

  1. RabbitMQ面试题

    1.为什么要引入MQ系统,直接读写数据库不行吗?其实就是问问你消息队列都有哪些使用场景,然后你项目里具体是什么场景,说说你在这个场景里用消息队列是什么? 面试官问你这个问题,期望的一个回答是说,你们公 ...

  2. python网络-Socket之udp编程(24)

    一.udp简介 udp --- 用户数据报协议,是一个无连接的简单的面向数据报的运输层协议. udp不提供可靠性,它只是把应用程序传给IP层的数据报发送出去,但是并不能保证它们能到达目的地. udp在 ...

  3. vue组件的基本使用,以及组件之间的基本传值方式

    组件(页面上的每一个部分都是组件) 1.三部分:结构(template),样式(style),逻辑(script) 2.组件的作用:复用 3.模块包含组件 4.组件创建:     1.全局组件:Vue ...

  4. Javaweb编程中的乱码问题

    程序中的乱码问题,主要出现在我们处理中文数据的过程中出现.从浏览器向服务器请求数据,服务器返回的数据在浏览器中显示为乱码.或者是服务器中的java文件用到中文,也有可能会出现乱码.数据库在处理数据的时 ...

  5. 如何写好css系列之button

    现代前端行业的发展,如果你在css的时候,还没有利用一些预编译工具,是否觉得自己太low了.但你是否考虑过搭建一套自己前端框架.可能你会想这是否有必要,因为基础有boostrap,组件库有:easyu ...

  6. 程序员如何面试才能拿到offer

    一.概述 面试,难还是不难?取决于面试者的底蕴(气场+技能).心态和认知及沟通技巧.面试其实可以理解为一场聊天和谈判,在这过程中有心理.思想上的碰撞和博弈.其实你只需要搞清楚一个逻辑:“面试官为什么会 ...

  7. 【Java基础】【14正则表达式&常用工具类】

    14.01_常见对象(正则表达式的概述和简单使用) A:正则表达式 是指一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串.其实就是一种规则.有自己特殊的应用. 作用:比如注册邮箱,邮箱有 ...

  8. scrapy pipelines导出各种格式

    scrapy在使用pipelines的时候,我们经常导出csv,json.jsonlines等等格式.每次都需要写一个类去导出,很麻烦. 这里我整理一个pipeline文件,支持多种格式的. # -* ...

  9. Nacos 发布 v0.8.0 Pre-GA版本,安全稳定上生产?

    服务注册和服务配置开源项目 Nacos 本周发布了 v0.8.0 Pre-GA 版本,作为开源项目生命周期中的里程碑版本之一,v0.8.0 Pre-GA版本支持登录.命名空间.Metrics监控(对接 ...

  10. 【野草】SQL Server之索引解析(二)

    1.堆表 堆表通过IAM连接一起,查询时全表扫描. 1.1 非聚集索引 结构 叶子节点数据结构:行数据结构+Rid(8字节) 中间节点数据结构: (非聚集非唯一索引)行数据结构+Page(4)+2+ ...