When running the ZeroMQ basic PUB / SUB C# samples they are working for me if I start the publisher first, but not if I start the subscriber first. When I do that the subscriber starts, but never receives any data. From what I have read, I thought that I should be able to start the processes in either order.
I am using the ZeroMQ 4.1.0.26 package from nuget in .NET 4.6, x64 apps. These are running on Windows. I am running both apps on the same machine.
Here are is the code I am running (which is a simplified version of the sample from the ZeroMQ tutorial).
Subscriber:
static void Main(string[] args)
{
var endpoint = "tcp://127.0.0.1:5556";
// Socket to talk to server
using (var context = new ZContext())
using (var subscriber = new ZSocket(context, ZSocketType.SUB))
{
Console.WriteLine("I: Connecting to {0}…", endpoint);
subscriber.Connect(endpoint);
// Subscribe to zipcode
string zipCode = "90210 ";
Console.WriteLine("I: Subscribing to zip code {0}…", zipCode);
subscriber.Subscribe(zipCode);
while(true)
{
using (var replyFrame = subscriber.ReceiveFrame())
{
string reply = replyFrame.ReadString();
Console.WriteLine(reply);
}
}
}
}
Publisher:
static void Main(string[] args)
{
using (var context = new ZContext())
using (var publisher = new ZSocket(context, ZSocketType.PUB))
{
var address = "tcp://*:5556";
Console.WriteLine("I: Publisher.Bind'ing on {0}", address);
publisher.Bind(address);
// Initialize random number generator
var rnd = new Random();
while (true)
{
// Get values that will fool the boss
int zipcode = 90210;
int temperature = rnd.Next(-55, +45);
// Send message to all subscribers
var update = string.Format("{0:D5} {1}", zipcode, temperature);
using (var updateFrame = new ZFrame(update))
{
publisher.Send(updateFrame);
}
Thread.Sleep(1000);
}
}
}
Edit
Following suggestions in proposed answers below I tried:
- Using an explicit IP address: this made no difference
- Removing the subject filtering: this made no difference
- Creating new AnyCPU (instead of x64) projects: this made no difference
- Trying other languages: this was interesting!
Using Python equivalent publishers and subscribers:
- The Python subscriber works when started before the publisher, when connecting to either the Python publisher or the C# publisher.
- The C# subscriber does not work when started before the publisher, regardless whether it is connecting to the Python or C# publisher.
So it looks like there is something wrong with the C# subscriber code.
QUESTION:
Is there something wrong with my sample code ( latest version below )?
Or is it a problem with the ZeroMQ .NET library?
Here is the Python subscriber which worked correctly:
import sys
import zmq
# Socket to talk to server
context = zmq.Context()
socket = context.socket( zmq.SUB )
print( "Python: Collecting updates from weather server" )
socket.connect( "tcp://localhost:5556" )
socket.setsockopt_string( zmq.SUBSCRIBE, "" )
while True:
string = socket.recv_string()
zipcode, temperature = string.split()
print( zipcode + " " + temperature )
Here is the latest equivalent ( non-working ) version of the C# subscriber:
static void Main(string[] args)
{
// Socket to talk to server
using (var context = new ZContext())
using (var socket = new ZSocket(context, ZSocketType.SUB))
{
Console.WriteLine("C#: Collecting updates from weather server");
socket.Connect("tcp://localhost:5556");
socket.Subscribe("");
while (true)
{
using (var replyFrame = socket.ReceiveFrame())
{
string reply = replyFrame.ReadString();
Console.WriteLine(reply);
}
}
}
}