Page MenuHomePhabricator
Authored By
stwalkerster
Mar 17 2018, 6:43 PM
Size
2 KB
Referenced Files
None
Subscribers
None
namespace foo
{
using System.Threading;
using ZeroMQ;
internal class zmq_sender
{
public static void Main(string[] args)
{
// fancy stuff to queue up three instances of DoStuff on different threads, and wait until they're all done
// most of this is waiting-for-completion garbage, the key bits are ThreadPool.QueueUserWorkItem() which
// takes a method or lambda, and DoStuff().
int instances = 3;
var mre = new WaitHandle[instances];
for (int i = 0; i < instances; i++)
{
mre[i] = new ManualResetEvent(false);
var current = (ManualResetEvent)mre[i];
int c = i;
ThreadPool.QueueUserWorkItem(
x =>
{
try
{
DoStuff(c);
}
finally
{
current.Set();
}
});
}
WaitHandle.WaitAll(mre);
}
// emulates a client. Connects, sends three things, disconnects.
private static void DoStuff(int i)
{
using (var context = new ZContext())
{
using (var sock = new ZSocket(context, ZSocketType.PUB){})
{
sock.Connect("tcp://127.0.0.1:12345");
sock.Send(new ZFrame(i + ": test 1"));
Thread.Sleep(1000);
sock.Send(new ZFrame(i + ": test 2"));
Thread.Sleep(1000);
sock.Send(new ZFrame(i + ": test 3"));
}
}
}
}
internal class zmq_receiver
{
// connects, binds to socket, and prints whatever it receives.
public static void Main(string[] args)
{
var context = new ZContext();
var sock = new ZSocket(context, ZSocketType.SUB);
sock.Bind("tcp://*:12345");
sock.SubscribeAll();
while (true)
{
using (ZFrame request = sock.ReceiveFrame())
{
Console.WriteLine(request.ReadString());
}
}
}
}
}
/*
OUTPUT:
0: test 1
2: test 1
1: test 1
0: test 2
2: test 2
1: test 2
2: test 3
1: test 3
0: test 3

File Metadata

Mime Type
text/plain; charset=utf-8
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
52104
Default Alt Text
(2 KB)

Event Timeline