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/ 继续坚持,或许不能深刻理解,但至少要保证有印象. ~~~~~~~~~~~~~~ 消息队 ...
随机推荐
- .NET应用程序管理服务AMS设计
AMS全称是Application Management Server即应用程序管理服:由于经常要写些一些应用服务,每次部署和维护都比较麻烦,首先要针对服务编写一个windows服务程序方便系统启动里 ...
- Asp.Net Core中利用Seq组件展示结构化日志功能
在一次.Net Core小项目的开发中,掌握的不够深入,对日志记录并没有好好利用,以至于一出现异常问题,都得跑动服务器上查看,那时一度怀疑自己肯定没学好,不然这一块日志不可能需要自己扒服务器日志来查看 ...
- OCR识别
最近作者项目中用到了身份证识别跟营业执照的OCR识别,就研究了一下百度云跟腾讯云的OCR产品接口. 1.腾讯云OCR 收费:身份证OCR和营业执照OCR接口,每个接口每个月各有1000次的免费调用 接 ...
- mongos-sharding连接池配置
ShardingTaskExecutorPoolMaxSize Maximum number of outbound connections each TaskExecutor connection ...
- 【SpringCloud】HystrixCommand的threadPoolKey默认值及线程池初始化
关于threadPoolKey默认值的疑问 使用SpingCloud必然会用到Hystrix做熔断降级,也必然会用到@HystrixCommand注解,@HystrixCommand注解可以配置的除了 ...
- 一键解决 go get golang.org/x 包失败
问题描述 当我们使用 go get.go install.go mod 等命令时,会自动下载相应的包或依赖包.但由于众所周知的原因,类似于 golang.org/x/... 的包会出现下载失败的情况. ...
- sed从入门到深入的使用心得
本人已经此系列的sed文章整理到pdf中,欢迎下载:玩透sed:探究sed原理 sed系列文章: sed修炼系列(一):花拳绣腿之入门篇sed修炼系列(二):武功心法(info sed翻译+注解)se ...
- formData批量上传的多种实现
前言 最近项目需要批量上传附件,查了下资料,网上很多但看着一脸懵,只贴部分代码,介绍也不详细,这里记录一下自己的采坑与多种实现,以免以后忘记. 这里先介绍下FormData对象,以下内容摘自:http ...
- [C#] 使用 StackExchange.Redis 封装属于自己的 RedisHelper
使用 StackExchange.Redis 封装属于自己的 RedisHelper 目录 核心类 ConnectionMultiplexer 字符串(String) 哈希(Hash) 列表(List ...
- Spring Boot入门-快速搭建web项目
Spring Boot 概述: Spring Boot makes it easy to create stand-alone, production-grade Spring based Appli ...