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. 巡风源码阅读与分析---view.py

    巡风xunfeng----巡风源码阅读与分析 巡风是一款适用于企业内网的漏洞快速应急.巡航扫描系统,通过搜索功能可清晰的了解内部网络资产分布情况,并且可指定漏洞插件对搜索结果进行快速漏洞检测并输出结果 ...

  2. Python Matplotlib.pyplot plt 中文显示

    话不多说,上代码 # -*- coding: UTF-8 -*- import matplotlib.pyplot as plt from matplotlib.font_manager import ...

  3. vue组件,可以通过npm引用的组件

    本文章通过实现一个vue-dialog的弹出层组件,然后附加说明如果发布此包到npm,且能被其他项目使用. 功能说明 多层弹出时,只有一个背景层. 弹出层嵌入内部组件. 弹出层按钮支持回调 源码下载 ...

  4. MetaEditor中MQL使用方法

    MT4程序--帮助--MQL5文档,转到网页,切换到中文,点击旁边搜索图标. MetaEditor编辑器,点击相应关键字,按F1键,即可启动MT4对应的MQL4的对应关键字用法帮助.但是英文. 时间序 ...

  5. 使用ML.NET和Azure Function进行机器学习 - 第2部分

    本文是<使用ML.NET和AzureFunction进行机器学习 - 第1部分>的续篇. 像机器一样识别鸢尾花 回顾第1部分,您将使用Visual Studio创建一个新的Azure Fu ...

  6. Python爬虫入门教程 29-100 手机APP数据抓取 pyspider

    1. 手机APP数据----写在前面 继续练习pyspider的使用,最近搜索了一些这个框架的一些使用技巧,发现文档竟然挺难理解的,不过使用起来暂时没有障碍,估摸着,要在写个5篇左右关于这个框架的教程 ...

  7. angr进阶(3) 添加约束

    如果已知字符串长度或部分,可以使用如下的方式进行格式规范csaw_wyvern 1 flag_chars = [claripy.BVS('flag_%d' % i, 8) for i in range ...

  8. 组合模式 合成模式 COMPOSITE 结构型 设计模式(十一)

    组合模式(合成模式 COMPOSITE) 意图 将对象组合成树形结构以表示“部分-整体”的层次结构. Composite使得用户对单个对象和组合对象的使用具有一致性.   树形结构介绍 为了便于理解, ...

  9. spring boot(四) 多数据源

    前言 前一篇中我们使用spring boot+mybatis创建了单一数据源,其中单一数据源不需要我们自己手动创建,spring boot自动配置在程序启动时会替我们创建好数据源. 准备工作 appl ...

  10. tomcat使用详解(week4_day2)--技术流ken

    tomcat简介 Tomcat是Apache软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache.Sun和其他一些公司及个人共同开发 ...