gpt4 book ai didi

c++ - ZMQ Dealer 向自己发送和接收消息

转载 作者:搜寻专家 更新时间:2023-10-31 00:50:59 26 4
gpt4 key购买 nike

我有一个 zmq 经销商套接字,我正在连接到我机器上没有其他人连接到的端口。我使用 ZMQ_IMMEDIATE 选项来阻止套接字在没有连接的对等方的情况下发送消息。如果我尝试发送消息的次数足够多(大约五分钟),最终我能够发送消息并收到一条消息,消息的大小和数据与我尝试发送的一样。

使用 zmq_socket_monitor 最终表明经销商套接字能够连接并向自身发送/接收消息。谁能解释为什么会这样?

#include <unistd.h>
#include <iostream>
#include <zmq.hpp>
#include <thread>
#include <sstream>
static int read_msg(void* s, zmq_event_t* event, char* ep)
{
int rc ;
zmq_msg_t msg1; // binary part
zmq_msg_init (&msg1);
zmq_msg_t msg2; // address part
zmq_msg_init (&msg2);
rc = zmq_msg_recv (&msg1, s, 0);
if (rc == -1 && zmq_errno() == ETERM)
return 1 ;
assert (rc != -1);
assert (zmq_msg_more(&msg1) != 0);
rc = zmq_msg_recv (&msg2, s, 0);
if (rc == -1 && zmq_errno() == ETERM)
return 1;
assert (rc != -1);
assert (zmq_msg_more(&msg2) == 0);
// copy binary data to event struct
const char* data = (char*)zmq_msg_data(&msg1);
memcpy(&(event->event), data, sizeof(event->event));
memcpy(&(event->value), data+sizeof(event->event), sizeof(event->value));
// copy address part
const size_t len = zmq_msg_size(&msg2) ;
ep = (char*)memcpy(ep, zmq_msg_data(&msg2), len);
*(ep + len) = 0 ;
return 0 ;
}


// dealer socket monitor thread
static void *dealer_socket_monitor (void *ctx)
{
zmq_event_t event;
static char addr[1025] ;
int rc;

printf("starting monitor...\n");

void *s = zmq_socket (ctx, ZMQ_PAIR);
assert (s);
int num = 0;
rc = zmq_connect (s, "inproc://monitor.dealer");
assert (rc == 0);
while (!read_msg(s, &event, addr)) {
switch (event.event) {
case ZMQ_EVENT_CONNECT_RETRIED:
printf (" DEALER connect retried socket descriptor %d %d\n", event.value, num);
printf (" DEALER connect retried socket address %s\n", addr);
break;
case ZMQ_EVENT_CONNECT_DELAYED:
printf (" DEALER connect delayed socket descriptor %d %d\n", event.value, num);
printf (" DEALER connect delayed socket address %s\n", addr);
break;
case ZMQ_EVENT_LISTENING:
printf (" DEALER listening socket descriptor %d %d\n", event.value, num);
printf (" DEALER listening socket address %s\n", addr);
break;
case ZMQ_EVENT_CONNECTED:
printf (" DEALER connected socket descriptor %d %d\n", event.value, num);
printf (" DEALER connected socket address %s\n", addr);
break;
case ZMQ_EVENT_MONITOR_STOPPED:
printf (" DEALER monitor ending socket descriptor %d %d\n", event.value, num);
printf (" DEALER monitor ending socket address %s\n", addr);
break;
case ZMQ_EVENT_ACCEPTED:
printf (" DEALER accepted socket descriptor %d %d\n", event.value, num);
printf (" DEALER accepted socket address %s\n", addr);
break;
case ZMQ_EVENT_CLOSE_FAILED:
printf (" DEALER socket close failure error code %d %d\n", event.value, num);
printf (" DEALER socket address %s\n", addr);
break;
case ZMQ_EVENT_CLOSED:
printf (" DEALER closed socket descriptor %d %d\n", event.value, num);
printf (" DEALER closed socket address %s\n", addr);
break;
case ZMQ_EVENT_DISCONNECTED:
printf (" DEALER disconnected socket descriptor %d %d\n", event.value, num);
printf (" DEALER disconnected socket address %s\n", addr);
break;
default:
printf(" DEALER default case\n");
}
}
zmq_close (s);
return NULL;
}
int main() {
pthread_t thread ;
std::string connect = "tcp://localhost:49150";
zmq::context_t context(1);
zmq::socket_t client (context, ZMQ_DEALER);
int m_linger = 0;
int count = 0;
int m_timeoutMs = 1000;
int imm = 1;
// release the socket / port asa it disconnects
client.setsockopt(ZMQ_LINGER, &m_linger, sizeof(m_linger));
client.setsockopt(ZMQ_RCVTIMEO, &m_timeoutMs, sizeof(m_timeoutMs));
client.setsockopt(ZMQ_SNDTIMEO, &m_timeoutMs, sizeof(m_timeoutMs));
client.setsockopt(ZMQ_IMMEDIATE, &imm, sizeof(imm));

client.connect(connect.c_str());
std::cout << "Connecting client to " << connect << std::endl;

// socket monitoring
int rc = zmq_socket_monitor ((void*)client, "inproc://monitor.dealer", ZMQ_EVENT_ALL);
rc = pthread_create (&thread, NULL, dealer_socket_monitor, (void*)context);
int more = 0;
while(true)
{
zmq::message_t blank{};
zmq::message_t message(sizeof(int));
zmq::message_t response;
int more; // Multipart detection
std::cout << "Client no linger sending message more enabled" << std::endl;
auto ret = client.send(blank, ZMQ_SNDMORE);
if (!ret)
{
std::cout << "timeout on send, trying again" << std::endl;
continue;
}
std::cout << "Client no linger sending message more disabled" << std::endl;
ret = client.send(message);
if (!ret)
{
std::cout << "timeout on send second, trying again";
continue;
}
ret = client.recv(&response);
size_t more_size = sizeof (more);
client.getsockopt(ZMQ_RCVMORE, &more, &more_size);
while (more)
{
std::cout << "Client received message with more coming. Size of message " << response.size() << std::endl;
client.recv(&response);
client.getsockopt(ZMQ_RCVMORE, &more, &more_size);
}
std::cout << "Client received final part of message. Size of message " << response.size() << " message " << *(reinterpret_cast<int*>(message.data())) << std::endl;
exit(0);
}
}

请注意,消息的大小是一个 int 的大小,我用它初始化了 zmq 消息。查看我最初创建此错误的程序,该消息包含尝试发送的相同数据。运行程序得到的输出结束:

 DEALER closed socket descriptor 10 0
DEALER closed socket address tcp://localhost:49150
DEALER connect retried socket descriptor 185 0
DEALER connect retried socket address tcp://localhost:49150
DEALER connect delayed socket descriptor 115 0
DEALER connect delayed socket address tcp://localhost:49150
DEALER connected socket descriptor 10 0
DEALER connected socket address tcp://localhost:49150
Client no linger sending message more disabled
Client received message with more coming. Size of message 0
Client received final part of message. Size of message 4 message 0

编译:g++ -std=c++11 client.cpp -o client -g -lzmq -lpthread by GCC 6.3cppzmq-devel 4.1.4-6 和 zmq 4.1.4-6

最佳答案

我有一个 similar issue使用 NodeJS,套接字最终会连接到自身。这里似乎发生了同样的事情:ZeroMQ 套接字创建了一个本地临时端口,该端口与您要连接的端口 (49150) 相同。

如果是这样,当您连接到没有服务监听的保留端口(< 1024)时,例如端口 7,则不应发生这种情况。绑定(bind)/监听临时端口范围之外的端口(< 32768 在 Linux 上)应该解决问题。

使用诸如 netstattcpdump/wireshark 之类的工具来验证这实际上发生在套接字/网络级别。

关于c++ - ZMQ Dealer 向自己发送和接收消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56387152/

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