c/c++ linux 进程间通信系列2,使用UNIX_SOCKET
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;
}
使用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;
}
使用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;
}
使用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;
}
c/c++ 学习互助QQ群:877684253
本人微信:xiaoshitou5854
c/c++ linux 进程间通信系列2,使用UNIX_SOCKET的更多相关文章
- c/c++ linux 进程间通信系列7,使用pthread mutex
linux 进程间通信系列7,使用pthread mutex #include <stdio.h> #include <stdlib.h> #include <unist ...
- c/c++ linux 进程间通信系列6,使用消息队列(message queue)
linux 进程间通信系列6,使用消息队列(message queue) 概念:消息排队,先进先出(FIFO),消息一旦出队,就从队列里消失了. 1,创建消息队列(message queue) 2,写 ...
- c/c++ linux 进程间通信系列5,使用信号量
linux 进程间通信系列5,使用信号量 信号量的工作原理: 由于信号量只能进行两种操作等待和发送信号,即P(sv)和V(sv),他们的行为是这样的: P(sv):如果sv的值大于零,就给它减1:如果 ...
- c/c++ linux 进程间通信系列4,使用共享内存
linux 进程间通信系列4,使用共享内存 1,创建共享内存,用到的函数shmget, shmat, shmdt 函数名 功能描述 shmget 创建共享内存,返回pic key shmat 第一次创 ...
- c/c++ linux 进程间通信系列3,使用socketpair,pipe
linux 进程间通信系列3,使用socketpair,pipe 1,使用socketpair,实现进程间通信,是双向的. 2,使用pipe,实现进程间通信 使用pipe关键点:fd[0]只能用于接收 ...
- c/c++ linux 进程间通信系列1,使用signal,kill
linux 进程间通信系列1,使用signal,kill 信号基本概念: 软中断信号(signal,又简称为信号)用来通知进程发生了异步事件.进程之间可以互相通过系统调用kill发送软中断信号.内核 ...
- Linux 进程间通信系列之 信号
信号(Signal) 信号是比较复杂的通信方式,用于通知接受进程有某种事件发生,除了用于进程间通信外,进程还可以发送信号给进程本身:Linux除了支持Unix早期信号语义函数sigal外,还支持语义符 ...
- 进程间通信系列 之 socket套接字及其实例
进程间通信系列 之 概述与对比 http://blog.csdn.net/younger_china/article/details/15808685 进程间通信系列 之 共享内存及其实例 ...
- 练习--LINUX进程间通信之消息队列MSG
https://www.ibm.com/developerworks/cn/linux/l-ipc/part3/ 继续坚持,或许不能深刻理解,但至少要保证有印象. ~~~~~~~~~~~~~~ 消息队 ...
随机推荐
- GenerationType四中类型
https://blog.csdn.net/u011781521/article/details/72210980 JPA提供的四种标准用法为TABLE,SEQUENCE,IDENTITY,AUTO. ...
- ASP.NET Core 四种方式绑定枚举值
前言 本节我们来讲讲在ASP.NET Core MVC又为我们提供了哪些方便,之前我们探讨过在ASP.NET MVC中下拉框绑定方式,这节我们来再来重点看看枚举绑定的方式,充分实现你所能想到的场景,满 ...
- PYTHON BS 四大对象
BeautifulSoup是灵活又方便的网页解析库,处理搞笑,支持多种解析器利用它不用编写正则表达式即可方便地实现网页信息的提取BS的四大对象:1.TagTag就是HTML中的一个个标签,例如:< ...
- Qt之QComboBox定制(二)
上一篇文章Qt之QComboBox定制讲到了qt实现自定义的下拉框,该篇文章主要实现了列表式的下拉框,这一节我还将继续讲解QComboBox的定制,而这一节我将会讲述更高级的用法,不仅仅是下拉列表框, ...
- ASP.NET Core中如何针对一个使用HttpClient对象的类编写单元测试
原文地址: How to unit test a class that consumes an HttpClient with IHttpClientFactory in ASP.NET Core? ...
- JsChart组件使用
JsChart是什么? JSChart能够在网页上生成图标,常用于统计信息,十分好用的一个JS组件. 使用JsChart 一.导入jscharts.js 二.编写jscharts.jsp测试页面 下载 ...
- linux为什么不可以添加硬链接
假设有个文件夹1 文件夹1里面还有个文件夹2 文件夹2里面还有个文件夹3 然后发现哎呀直接文件夹3放到文件夹1下就行了访问多方便. 也就是文件夹1下有文件夹2和文件夹3,然后问题就来了文件夹1下的文件 ...
- 写在最前面 - 《看懂每一行代码 - kubernetes》
我要写什么 <看懂每一行代码 - kubernetes>会包含k8s整个项目的源码解析,考虑到门槛问题,在开始分析k8s之前我会通过一些更低难度的golang开源项目讲解来帮助大家提升go ...
- Linux常用命令详解(week1_day1_1)--技术流ken
本节内容 基础命令:lsmanpwdcdmkdirechotouchcpmvrmrmdircatmorelessheadtailclearpoweroffreboot进阶命令(下一章节):aliasu ...
- Springboot 系列(二)Spring Boot 配置文件
注意:本 Spring Boot 系列文章基于 Spring Boot 版本 v2.1.1.RELEASE 进行学习分析,版本不同可能会有细微差别. 前言 不管是通过官方提供的方式获取 Spring ...