不要使用epgm://获取消息,而使用tcp:// do。为什么?
问题描述:
我尝试使用ZeroMQ指南中的天气示例,并使用epgm://
传输级别。起初,我的代码:不要使用epgm://获取消息,而使用tcp:// do。为什么?
// Publisher
#include "zhelpers.h"
int main (void) {
// Prepare our context and publisher
void *context = zmq_ctx_new();
void *publisher = zmq_socket (context, ZMQ_PUB);
// int rc = zmq_bind (publisher, "tcp://*:5556");
int rc = zmq_bind (publisher, "epgm://192.168.1.4;224.0.0.251:5556");
assert (rc == 0);
// Initialize random number generator
srandom ((unsigned) time (NULL));
while (1) {
// Get values that will fool the boss
int zipcode, temperature, relhumidity;
zipcode = randof (100000);
temperature = randof (215) - 80;
relhumidity = randof (50) + 10;
// Send a message to all subscribers
char update [20];
sprintf (update, "%05d %d %d", zipcode, temperature, relhumidity);
s_send (publisher, update);
}
zmq_close (publisher);
zmq_ctx_destroy (context);
return 0;
}
// Subscriber
#include "zhelpers.h"
int main (int argc, char *argv [])
{
// Socket to talk to server
printf ("Collecting updates from weather server…\n");
void *context = zmq_ctx_new();
void *subscriber = zmq_socket (context, ZMQ_SUB);
// int rc = zmq_connect (subscriber, "tcp://192.168.1.4:5556");
int rc = zmq_connect (subscriber, "epgm://192.168.1.9;224.0.0.251:5556");
assert (rc == 0);
// Subscribe to zipcode, default is NYC, 10001
char *filter = (argc > 1)? argv [1]: "10001 ";
rc = zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE,
filter, strlen (filter));
assert (rc == 0);
// Process 100 updates
int update_nbr;
long total_temp = 0;
for (update_nbr = 0; update_nbr < 100; update_nbr++) {
char *string = s_recv (subscriber);
int zipcode, temperature, relhumidity;
sscanf (string, "%d %d %d",
&zipcode, &temperature, &relhumidity);
total_temp += temperature;
free (string);
}
printf ("Average temperature for zipcode '%s' was %dF\n",
filter, (int) (total_temp/update_nbr));
zmq_close (subscriber);
zmq_ctx_destroy (context);
return 0;
}
- 我试过这个代码
tcp://
在同一主机(见注释),它工作正常。 - 我试了这个例子的组播地址Openbook Linux(用我的组播地址),我收到了这些消息。
- 我安装ZeroMQ与
pgm://
(在我得到断言故障之前)。 - 我的主机都是Ubuntu的16.04系统
- 我使用ZeroMQ 4
我的问题是,我不epgm://
得到任何消息。两个程序都在运行,但没有收到任何消息。我错过了什么?我不明白什么是错的。
答
对于后面的问题访问者来说,代码是正常的并且正在运行。要查看它是否可以在您的系统上运行,请将for循环更改为一个循环,并在10001中将发布商的邮政编码设置为,如果您启动没有参数的订户。
您的代码正在为我工作。你是如何确认它没有收到任何消息的? 尝试将zipcode生成更改为如下形式:'zipcode = randof(100)+ 9990;',1/100000命中100次可能需要一段时间才能获得打印的内容。 在循环内添加打印件以查看它是否确实没有收到任何东西。 –
对不起,我的迟到回复。我在旅行。我再次尝试,只有一个邮编,只有一个循环添加和它的工作。令我吃惊的是,因为双方都跑了一段时间,也许我在经过很长一段时间的测试之后开始犯了一个错误。 无论如何谢谢你的回答:-)。 – Runms