gpt4 book ai didi

c++ - 为什么 ZeroMQ PGM 多播没有收到多播消息? ( C++, Windows )

转载 作者:可可西里 更新时间:2023-11-01 09:32:45 26 4
gpt4 key购买 nike

环境设置:
- 多播发送和接收应用程序都在同一台机器上运行

我正在将 ZeroMQ 多播与 OpenPGM 支持集成,但在我的示例代码下方遇到问题。

“未收到多播消息” 在接收器应用程序中。如果我做错了,请纠正我。也无法找到关于 ZeroMQ PGM 多播要求的适当示例。

// ZMQ_pgm_receive.cpp : 
//
//Headers
#include "stdafx.h"
#include "zmq.h"
#include <iostream>
#include <windows.h>

std::string fullurl = "pgm://eth0;239.255.0.1:30001";
static int roundtrip_count = 50;
static size_t message_size = 4;

int _tmain(int argc, _TCHAR* argv[])
{
void *ctx = NULL,
*s = NULL;
int con;
int i;

ctx = zmq_init (1);
if (!ctx) {
printf ("error in zmq_init: %s\n", zmq_strerror (errno));
return -1;
}

s = zmq_socket (ctx, ZMQ_SUB);
if (!s) {
printf ("error in zmq_socket: %s\n", zmq_strerror (errno));
return -1;
}


con = zmq_bind(socket, fullurl.c_str());
if (con == 0) {
printf ("error in zmq_bind: %s\n", zmq_strerror (errno));
return -1;
}
zmq_msg_t msg;

int rc = zmq_msg_init (&msg);
if (rc != 0) {
printf ("error in zmq_msg_init: %s\n", zmq_strerror (errno));
return -1;
}

for (i = 0; i != roundtrip_count; i++) {

rc = zmq_recvmsg (s, &msg, 0);
if (rc < 0) {
printf ("error in zmq_recvmsg: %s\n", zmq_strerror (errno));
return -1;
}
printf("message received\n");

if (zmq_msg_size (&msg) != message_size) {
printf ("message of incorrect size received\n");
return -1;
}
Sleep(1000);

}

rc = zmq_msg_close (&msg);
if (rc != 0) {
printf ("error in zmq_msg_close: %s\n", zmq_strerror (errno));
return -1;
}

rc = zmq_close (s);
if (rc != 0) {
printf ("error in zmq_close: %s\n", zmq_strerror (errno));
return -1;
}
/*rc = zmq_ctx_term (ctx);
if (rc != 0) {
printf ("error in zmq_ctx_term: %s\n", zmq_strerror (errno));
return -1;
}
ctx = NULL;
*/

return 0;
}

// ZMQ_pgm_send.cpp :
//
#include "stdafx.h"
#include "zmq.h"
#include <iostream>
#include <windows.h>

std::string fullurl = "pgm://eth0;239.255.0.1:30001";
static int roundtrip_count = 50;
static size_t message_size = 4;

int _tmain(int argc, _TCHAR* argv[])
{
void *ctx = NULL,
*s = NULL;
int con;
int i;

ctx = zmq_init (1);
if (!ctx) {
printf ("error in zmq_init: %s\n", zmq_strerror (errno));
return -1;
}

s = zmq_socket (ctx, ZMQ_PUB);
if (!s) {
printf ("error in zmq_socket: %s\n", zmq_strerror (errno));
return -1;
}

con = zmq_connect(socket, fullurl.c_str());
if (con == 0) {
printf ("error in zmq_connect: %s\n", zmq_strerror (errno));
return -1;
}
zmq_msg_t msg;

int rc = zmq_msg_init_size (&msg,message_size);
if (rc != 0) {
printf ("error in zmq_msg_init: %s\n", zmq_strerror (errno));
return -1;
}

memset(zmq_msg_data (&msg),'A', message_size );
for (i = 0; i != roundtrip_count; i++) {

rc = zmq_sendmsg (s, &msg, 0);
if (rc < 0) {
printf ("error in zmq_sendmsg: %s\n", zmq_strerror (errno));
return -1;
}
}

rc = zmq_msg_close (&msg);
if (rc != 0) {
printf ("error in zmq_msg_close: %s\n", zmq_strerror (errno));
return -1;
}

rc = zmq_close (s);
if (rc != 0) {
printf ("error in zmq_close: %s\n", zmq_strerror (errno));
return -1;
}
/*rc = zmq_ctx_term (ctx);
if (rc != 0) {
printf ("error in zmq_ctx_term: %s\n", zmq_strerror (errno));
return -1;
}
ctx = NULL;
*/

return 0;
}

如果我做错了,请指正。

最佳答案

解决了上面评论中提出的[Step 0]
一个应该
检测

缺少ZMQ_SUBSCRIBE设置,因此SUB端过滤所有流量

ZMQ_SUBSCRIBE: Establish message filter


The ZMQ_SUBSCRIBE option shall establish a new message filter on a ZMQ_SUB socket. Newly created ZMQ_SUB sockets shall filter out all incoming messages, therefore you should call this option to establish an initial message filter.

An empty option_value of length zero shall subscribe to all incoming messages. A non-empty option_value shall subscribe to all messages beginning with the specified prefix. Multiple filters may be attached to a single ZMQ_SUB socket, in which case a message shall be accepted if it matches at least one filter.


无论如何,欢迎并享受这些用于分布式系统计算的智能工具!

关于c++ - 为什么 ZeroMQ PGM 多播没有收到多播消息? ( C++, Windows ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42665589/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com