1、应用手册

https://github.com/nanomsg/nanomsg

  1. % mkdir build
  2. % cd build
  3. % cmake ..
  4. % cmake --build .
  5. % ctest .
  6. % sudo cmake --build . --target install
  7. % sudo ldconfig (if on Linux)

2、性能测试

dong@ubuntu:~/nanomsg-1.1.4/build$ ctest .
Test project /home/dong/nanomsg-1.1.4/build
      Start  1: inproc
 1/43 Test  #1: inproc ...........................   Passed    0.51 sec
      Start  2: inproc_shutdown
 2/43 Test  #2: inproc_shutdown ..................   Passed    0.26 sec
      Start  3: ipc
 3/43 Test  #3: ipc ..............................   Passed    0.71 sec
      Start  4: ipc_shutdown
 4/43 Test  #4: ipc_shutdown .....................   Passed    1.07 sec
      Start  5: ipc_stress
 5/43 Test  #5: ipc_stress .......................   Passed    1.52 sec
      Start  6: tcp
 6/43 Test  #6: tcp ..............................   Passed    0.65 sec
      Start  7: tcp_shutdown
 7/43 Test  #7: tcp_shutdown .....................   Passed    2.72 sec
      Start  8: ws
 8/43 Test  #8: ws ...............................   Passed    1.73 sec
      Start  9: pair
 9/43 Test  #9: pair .............................   Passed    0.00 sec
      Start 10: pubsub
10/43 Test #10: pubsub ...........................   Passed    0.12 sec
      Start 11: reqrep
11/43 Test #11: reqrep ...........................   Passed    0.11 sec
      Start 12: pipeline
12/43 Test #12: pipeline .........................   Passed    0.02 sec
      Start 13: survey
13/43 Test #13: survey ...........................   Passed    1.01 sec
      Start 14: bus
14/43 Test #14: bus ..............................   Passed    0.01 sec
      Start 15: async_shutdown
15/43 Test #15: async_shutdown ...................   Passed    2.02 sec
      Start 16: block
16/43 Test #16: block ............................   Passed    0.21 sec
      Start 17: term
17/43 Test #17: term .............................   Passed    0.11 sec
      Start 18: timeo
18/43 Test #18: timeo ............................   Passed    0.21 sec
      Start 19: iovec
19/43 Test #19: iovec ............................   Passed    0.00 sec
      Start 20: msg
20/43 Test #20: msg ..............................   Passed    0.03 sec
      Start 21: prio
21/43 Test #21: prio .............................   Passed    0.11 sec
      Start 22: poll
22/43 Test #22: poll .............................   Passed    0.15 sec
      Start 23: device
23/43 Test #23: device ...........................   Passed    0.21 sec
      Start 24: device4
24/43 Test #24: device4 ..........................   Passed    0.11 sec
      Start 25: device5
25/43 Test #25: device5 ..........................   Passed    0.11 sec
      Start 26: device6
26/43 Test #26: device6 ..........................   Passed    1.01 sec
      Start 27: device7
27/43 Test #27: device7 ..........................   Passed    1.01 sec
      Start 28: emfile
28/43 Test #28: emfile ...........................   Passed    0.05 sec
      Start 29: domain
29/43 Test #29: domain ...........................   Passed    0.00 sec
      Start 30: trie
30/43 Test #30: trie .............................   Passed    0.00 sec
      Start 31: list
31/43 Test #31: list .............................   Passed    0.00 sec
      Start 32: hash
32/43 Test #32: hash .............................   Passed    0.02 sec
      Start 33: stats
33/43 Test #33: stats ............................   Passed    0.51 sec
      Start 34: symbol
34/43 Test #34: symbol ...........................   Passed    0.00 sec
      Start 35: separation
35/43 Test #35: separation .......................   Passed    0.41 sec
      Start 36: zerocopy
36/43 Test #36: zerocopy .........................   Passed    0.00 sec
      Start 37: shutdown
37/43 Test #37: shutdown .........................   Passed    0.01 sec
      Start 38: cmsg
38/43 Test #38: cmsg .............................   Passed    0.01 sec
      Start 39: bug328
39/43 Test #39: bug328 ...........................   Passed    0.41 sec
      Start 40: bug777
40/43 Test #40: bug777 ...........................   Passed    0.00 sec
      Start 41: ws_async_shutdown
41/43 Test #41: ws_async_shutdown ................   Passed    1.08 sec
      Start 42: reqttl
42/43 Test #42: reqttl ...........................   Passed    0.21 sec
      Start 43: surveyttl
43/43 Test #43: surveyttl ........................   Passed    0.21 sec

100% tests passed, 0 tests failed out of 43

Total Test time (real) =  18.70 sec
dong@ubuntu:~/nanomsg-1.1.4/build$

3、demo

recv.c

#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include <assert.h>
#include <nanomsg/nn.h>
#include <nanomsg/reqrep.h>
#include <nanomsg/pubsub.h>
#include <nanomsg/pipeline.h> const char *url = "ipc:///tmp/pipeline.ipc"; typedef struct{
int type;
char text[];
}buf_t; int main ()
{
buf_t *buf = NULL;
buf = (buf_t *)malloc(sizeof(buf_t)); int sock = nn_socket (AF_SP, NN_PULL);
assert (sock >= );
assert (nn_bind (sock, url) >= );
while ()
{
int bytes = nn_recv (sock, &buf, NN_MSG, );
assert (bytes >= );
printf ("NODE0: RECEIVED %d \"%s\"\n", buf->type, buf->text);
nn_freemsg (buf);
}
return ;
}

send.c

#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include <assert.h>
#include <nanomsg/nn.h>
#include <nanomsg/reqrep.h>
#include <nanomsg/pubsub.h>
#include <nanomsg/pipeline.h> const char *url = "ipc:///tmp/pipeline.ipc"; typedef struct{
int type;
char text[];
}buf_t; int main ()
{
buf_t *buf = NULL;
buf = (buf_t *)malloc(sizeof(buf_t));
buf->type = ;
memset(buf->text,,);
memcpy(buf->text,"hello,world !",strlen("hello,world !")); int sz_msg = sizeof(buf_t);
int sock = nn_socket (AF_SP, NN_PUSH);
assert (sock >= );
assert (nn_connect (sock, url) >= );
printf ("NODE1: SENDING %d \"%s\"\n", buf->type,buf->text);
int bytes = nn_send (sock, buf, sz_msg, );
assert (bytes == sz_msg);
return nn_shutdown (sock, );
}
编译运行

dong@ubuntu:~/nanomsg_demo$ gcc -o recv recv.c -lnanomsg
dong@ubuntu:~/nanomsg_demo$ ./recv
NODE0: RECEIVED 1 "hello,world !"
NODE0: RECEIVED 1 "hello,world !"
NODE0: RECEIVED 1 "hello,world !"

dong@ubuntu:~/nanomsg_demo$ gcc -o send send.c -lnanomsg
dong@ubuntu:~/nanomsg_demo$ ./send
NODE1: SENDING 1 "hello,world !"
dong@ubuntu:~/nanomsg_demo$ ./send
NODE1: SENDING 1 "hello,world !"
dong@ubuntu:~/nanomsg_demo$ ./send
NODE1: SENDING 1 "hello,world !"
dong@ubuntu:~/nanomsg_demo$

* 进程内通信(inproc):url格式为inproc://test
* 进程间同in想(ipc):url格式为ipc:///tmp/test.ipc
* tcp通信:url格式为tcp://*:5555

4、python 与 c 进程通信

server.c

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <nanomsg/pair.h>
#include <nanomsg/bus.h>
#include <nanomsg/nn.h> char *url = "ipc://test"; int server_sock_init(int *sock)
{
*sock = nn_socket (AF_SP, NN_PAIR);
if (*sock < ) {
printf("create server sock failed\r\n");
return ;
}
if (nn_bind(*sock, url) < ) {
printf("bind server sock failed\r\n");
return ;
}
printf("server socket init success...\r\n");
return ;
} int main()
{
int s_sock;
char *tx_msg = "Hi, client !";
char *rx_msg = NULL; if ( != server_sock_init(&s_sock)) {
return;
}
while ()
{
int result = nn_recv(s_sock, &rx_msg, NN_MSG,NN_DONTWAIT);
if (result > ) {
printf("server Recieve: %s\r\n", rx_msg);
nn_freemsg (rx_msg);
} size_t len = strlen (tx_msg) + ;
if (nn_send(s_sock, tx_msg, len, ) < ) {
printf("server Send Msg Failed\r\n");
nn_freemsg (tx_msg);
} sleep();
} return ;
}

client.py

# -*- coding:utf-8 -*-
from __future__ import print_function
from nanomsg import Socket, PAIR, PUB
import time
s1 = Socket(PAIR)
s1.bind('ipc://test')
while True:
s1.send(b'hi, server !')
print(s1.recv())
time.sleep(1)
#s1.close()

编译

gcc -o server server.c -lnanomsg
gcc -o client client.c -lnanomsg
export LD_LIBRARY_PATH=$(pwd)/nanomsg-1.1.5/build:$LD_LIBRARY_PATH
https://files.cnblogs.com/files/dong1/nanomsg_demo.tar.gz

参考设计

nanomsg通信库的pubsub及survey

https://yq.aliyun.com/ziliao/829

https://yq.aliyun.com/articles/8694

https://www.oschina.net/code/snippet_1444806_49921

https://nanomsg.org/v0.1/nn_recv.3.html

1)Getting Started with nanomsg

https://blog.csdn.net/zsy19881226/article/details/56486176

2)This is a sample for p2p network based nanomsg

https://github.com/pch957/nanomsg_p2pnode

nanomsg(ZeroMQ with C)的更多相关文章

  1. GitHub 如何基於 Node.js 和 Chromium 開發 Atom?

    看到回答里, 多数都没有回答到点子上, 还有些给了非常主观的意见而没有给出实际结论和分析过程. 题主的问题有四个: 1. Github 如何基于 Node.js 和 Chromium 开发 Atom? ...

  2. linux ipc/its

    linux进程间双向消息队列 server.c #include <stdio.h> #include <stdlib.h> #include <string.h> ...

  3. c++消息中间件

    ZeroMQ ActiveMQ-CPP 另外 ZeroMQ 的作者用 C 重构了一套.改名叫:nanomsg ZeroMQ:https://www.cnblogs.com/rainbowzc/p/33 ...

  4. 消息队列性能对比——ActiveMQ、RabbitMQ与ZeroMQ(译文)

    Dissecting Message Queues 概述: 我花了一些时间解剖各种库执行分布式消息.在这个分析中,我看了几个不同的方面,包括API特性,易于部署和维护,以及性能质量..消息队列已经被分 ...

  5. Writing custom protocol for nanomsg

    http://vitiy.info/writing-custom-protocol-for-nanomsg/ nanomsg is next version of ZeroMQ lib, provid ...

  6. ubuntu 下安装nanomsg和nnpy

    nanomsg nanomsg是ZeroMQ作者用C语言重写的一个Socket库,其用法和模式和ZeroMQ差不多,但是具有更好的性能和更完善的接口. 首先下载源码 wget https://gith ...

  7. nanomsg安装和测试

    最近在构建一个中间层的通信架构,本来想用dbus,在实验过程中发现dbus对于国产系统支持版本比较低,安装比较麻烦,今天无意中看中了nanomsg,尽管没有dbus那么强悍的生态,但基本能满足需求. ...

  8. ZeroMQ:云时代极速消息通信库

    ZeroMQ:云时代极速消息通信库(大规模|可扩展|低成本|高效率解决之道,大规模分布式|多线程应用程序|消息传递架构构建利器) [美]Pieter Hintjens(皮特.亨特金斯)著   卢涛 李 ...

  9. 以ZeroMQ谈消息中间件的设计【译文】

    本文主要是探究学习比较流行的一款消息层是如何设计与实现的 ØMQ是一种消息传递系统,或者乐意的话可以称它为"面向消息的中间件".它在金融服务,游戏开发,嵌入式系统,学术研究和航空航 ...

随机推荐

  1. CentOS 7 上systemctl 的用法

    我们对service和chkconfig两个命令都不陌生,systemctl 是管制服务的主要工具, 它整合了chkconfig 与 service功能于一体. systemctl is-enable ...

  2. spring boot的@RequestParam和@RequestBody的区别

    一.问题描述 由于项目是前后端分离,因此后台使用的是spring boot,做成微服务,只暴露接口.接口设计风格为restful的风格,在get请求下,后台接收参数的注解为RequestBody时会报 ...

  3. 抹掉Scala的糖衣(14) -- Update Method

    欢迎关注我的新博客地址:http://cuipengfei.me/ 在Scala中,名字叫做update的方法是有特殊作用的. 比如: 1 2 3 val scores = new scala.col ...

  4. Unity3D 新版粒子系统 (Shuriken)

    Shuriken粒子系统是继Unity3.5版本之后推出的新版粒子系统,它采用了模块化管理,个性化的粒子模块配合粒子曲线编辑器使用户更容易创作出各种兵分复杂的粒子效果. 创建一个粒子系统的方式有两种: ...

  5. 多媒体开发之rtsp 实现rtsp over tcp/http/udp---rtsp发送

    (1) (2) (3) http://itindex.net/detail/51966-海康-rtsp-客户端 http://bbs.csdn.net/topics/390488547?page=1# ...

  6. PHP cURL库函数抓取页面内容

    目录 1 为什么要用cURL? 2 启用cURL 3 基本结构 4 检查错误 5 获取信息 6 基于浏览器的重定向 7 用POST方法发送数据 8 文件上传 9 cURL批处理(multi cURL) ...

  7. SpringCloud服务发现(Eureka)简介

    Eureka是Netflix开发的服务发现框架,SpringCloud将它集成在自己的子项目spring-cloud-netflix中,实现SpringCloud的服务发现功能. 为什么要使用Eure ...

  8. ORACLE之常用FAQ V1.0

    [B]第一部分.SQL&PL/SQL[/B][Q]怎么样查询特殊字符,如通配符%与_[A]select * from table where name like 'A\_%' escape ' ...

  9. django定义app名称

    1.apps.py class AccountConfig(AppConfig): name = 'account' verbose_name = u'用户信息' # app显示中文 2. __ini ...

  10. Redis分布式锁,基于StringRedisTemplate和基于Lettuce实现setNx

    使用redis分布式锁,来确保多个服务对共享数据操作的唯一性一般来说有StringRedisTemplate和RedisTemplate两种redis操作模板. 根据key-value的类型决定使用哪 ...