• 发送端/接收端
 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("PublisherSocket:Send Bytes in thread {0}.", Thread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, 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/

最新文章

  1. centos7 安装时候检测不到空余硬盘的解决办法
  2. cross_domain
  3. SQL Server 连接问题案例解析(1)
  4. Could not parse mapping document from input stream hibernate配置异常
  5. Linux crontab 命令详解
  6. Discuz!X2.5论坛在IIS和Apache环境配置实现伪静态
  7. 基于visual Studio2013解决C语言竞赛题之0802图书信息查询
  8. angularJS 系列(一)
  9. NIO FileChannel
  10. java之内存分布图
  11. 找一个数组的最大和的连续子数组(时间复杂度 O(n))
  12. ajax如何渲染数据
  13. python 面向对象编程(高级篇)
  14. mac 环境变量
  15. 《Java并发编程实战》笔记-synchronized和ReentrantLock
  16. String args[] 和 String[] args 有什么区别
  17. windows下进程与线程剖析
  18. php printf() 输出格式化的字符串
  19. hibernate学习之一 框架配置
  20. USACO 5.3 Network of Schools

热门文章

  1. js阻止事件冒泡(phpcms,浮窗第一次10秒弹出后每30秒弹出,动态更换日期)
  2. Uva12169 扩展欧几里得模板
  3. Pikachu-SSRF(服务器端请求伪造)
  4. git项目分支管理
  5. Cobalt Strike配置及简单使用
  6. 在mac下初次使用pygame踩坑纪实(卡死)
  7. OCM 12c | OCM 12c Update | OCM 11g (Retiring Dec 31, 2019) | OCM 11g考试延期至2020.04.30
  8. C#接口与抽象类学习笔记
  9. IDEA 接口调试插件 HTTP Client
  10. leetcode - 01