NetMQ用作IPC的实例
2024-09-27 06:42:13
- 发送端/接收端
using System;
using System.Threading;
using NetMQ;
using NetMQ.Sockets; namespace NetMQIPCServer
{
class Program
{
private const string topic = "unity3d";
private static bool running = true;
static void Main(string[] args)
{
Console.WriteLine("Main thread id = {0}", Thread.CurrentThread.ManagedThreadId); // 进程内通信
//pub.Bind("inproc://unity3d");
//sub.Connect("inproc://unity3d"); // 跨进程通信
//var pub = new PublisherSocket();
//var sub = new SubscriberSocket();
//pub.Bind("ipc:///unity3d/0");
//sub.Connect("ipc:///unity3d/0");
//sub.Subscribe(topic); // 定向接收[NonBlock]
//var pub = new PublisherSocket();
//var sub = new SubscriberSocket();
//pub.Bind("ipc:///unity3d/0");
//sub.Connect("ipc:///unity3d/0");
//sub.Subscribe(topic);
//var proactor = new NetMQProactor(sub, (socket, message) =>
//{
// //Console.WriteLine(message);
// foreach (var b in message[1].Buffer)
// {
// Console.Write("{0:x2}", b);
// Console.Write(" ");
// }
// Console.WriteLine("Received message in thread {0}", Thread.CurrentThread.ManagedThreadId);
//}); // TCP
var pub = new PublisherSocket();
var sub = new SubscriberSocket();
pub.Bind("tcp://*:2017");
sub.Connect("tcp://localhost:2017");
sub.Subscribe(topic);
var proactor = new NetMQProactor(sub, (socket, message) =>
{
//Console.WriteLine(message);
foreach (var b in message[].Buffer)
{
Console.Write("{0:x2}", b);
Console.Write(" ");
}
Console.WriteLine("Received message in thread {0}", Thread.CurrentThread.ManagedThreadId);
}); // 轮询模式
//var poller = new NetMQPoller();
//poller.Add(sub);
//sub.ReceiveReady += (sender, eventArgs) =>
//{
// bool more = false;
// byte[] bytes = null;
// eventArgs.Socket.ReceiveFrameBytes(out more);
// if (more)
// {
// bytes = eventArgs.Socket.ReceiveFrameBytes();
// foreach (var b in bytes)
// {
// Console.Write("{0:x2}", b);
// Console.Write(" ");
// }
// Console.WriteLine("Received bytes in thread {0}", Thread.CurrentThread.ManagedThreadId);
// }
//}; new Thread(() =>
{
while (running)
{
Console.WriteLine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b, 0xc, 0x1, 0x2, 0x3, 0xa, 0xb, 0xc, 0x1, 0x2, 0x3,
0xa, 0xb, 0xc, 0x1, 0x2, 0x3, 0xa, 0xb, 0xc, 0x1, 0x2, 0x3, 0xa, 0xb, 0xc, 0x1, 0x2, 0x3, 0xa, 0xb, 0xc,
0x1, 0x2, 0x3, 0xa, 0xb, 0xc, 0x1, 0x2, 0x3, 0xa, 0xb, 0xc, 0x1, 0x2, 0x3, 0xa, 0xb, 0xc, 0x1, 0x2, 0x3,
0xa, 0xb, 0xc, 0x1, 0x2, 0x3, 0xa, 0xb, 0xc, 0x1, 0x2, 0x3, 0xa, 0xb, 0xc, 0x1, 0x2, 0x3, 0xa, 0xb, 0xc,
0x1, 0x2, 0x3, 0xa, 0xb, 0xc, 0x1, 0x2, 0x3, 0xa, 0xb, 0xc, 0x1, 0x2, 0x3, 0xa, 0xb, 0xc, 0x1, 0x2, 0x3,
0xa, 0xb, 0xc, 0x1, 0x2, 0x3, 0xa, 0xb, 0xc, 0x1, 0x2, 0x3, 0xa, 0xb, 0xc, 0x1, 0x2, 0x3, 0xa, 0xb, 0xc,
0x1, 0x2, 0x3, 0xa, 0xb, 0xc, 0x1, 0x2, 0x3 });
Thread.Sleep();
}
}).Start(); //new Thread(() =>
//{
// bool more = false;
// byte[] bytes = null;
// while (running)
// {
// if (sub.HasIn)
// {
// sub.ReceiveFrameBytes(out more);
// if (more)
// {
// bytes = sub.ReceiveFrameBytes();
// foreach (var b in bytes)
// {
// Console.Write("{0:x2}", b);
// Console.Write(" ");
// }
// Console.WriteLine("Received bytes in thread {0}", Thread.CurrentThread.ManagedThreadId);
// }
// }
// }
//}).Start();
// 在主线程轮询[Block]
//poller.Run();
// 异步轮询[NonBlock]
//poller.RunAsync(); Console.ReadKey();
running = false;
Thread.Sleep();
//poller.Stop();
//poller.StopAsync();
pub.Close();
sub.Close();
NetMQConfig.Cleanup();
}
}
}
- 运行结果如图
NetMQ官方主页:http://netmq.readthedocs.io/en/latest/
最新文章
- centos7 安装时候检测不到空余硬盘的解决办法
- cross_domain
- SQL Server 连接问题案例解析(1)
- Could not parse mapping document from input stream hibernate配置异常
- Linux crontab 命令详解
- Discuz!X2.5论坛在IIS和Apache环境配置实现伪静态
- 基于visual Studio2013解决C语言竞赛题之0802图书信息查询
- angularJS 系列(一)
- NIO FileChannel
- java之内存分布图
- 找一个数组的最大和的连续子数组(时间复杂度 O(n))
- ajax如何渲染数据
- python 面向对象编程(高级篇)
- mac 环境变量
- 《Java并发编程实战》笔记-synchronized和ReentrantLock
- String args[] 和 String[] args 有什么区别
- windows下进程与线程剖析
- php printf() 输出格式化的字符串
- hibernate学习之一 框架配置
- USACO 5.3 Network of Schools
热门文章
- js阻止事件冒泡(phpcms,浮窗第一次10秒弹出后每30秒弹出,动态更换日期)
- Uva12169 扩展欧几里得模板
- Pikachu-SSRF(服务器端请求伪造)
- git项目分支管理
- Cobalt Strike配置及简单使用
- 在mac下初次使用pygame踩坑纪实(卡死)
- OCM 12c | OCM 12c Update | OCM 11g (Retiring Dec 31, 2019) | OCM 11g考试延期至2020.04.30
- C#接口与抽象类学习笔记
- IDEA 接口调试插件 HTTP Client
- leetcode - 01