ZeroMQ 示例的网络诊断

Network diagnostics for ZeroMQ Example

我正在尝试实施 ZeroMQ 以在 Raspberry Pi 3(Raspbian Stretch)上获取应用程序以与单独机器上的应用程序通信(在本例中为 Windows 7 64 位 OS) link 通过有线或 WLAN 连接。

我已经在两台机器上使用 C 库接口编译了 ZeroMQ(在 Windows 上使用 Cygwin)和 Hello World 示例(我稍微修改以打印指针值以确保函数是 'working').两台机器都已连接(在本例中通过有线以太网 link 和路由器)并且连接良好(我 link 通过 Xrdp 或 SSH 从 PC 连接到 RPi OK)。

我遇到的问题是 client/server ZeroMQ 程序似乎彼此不 'seeing' 即使它们看起来确实有效,我的问题是:我的第一步是什么应该调查为什么会这样?是否有任何命令行或 GUI 工具可以帮助我找出导致阻塞的原因? (比如端口 activity 监视器之类的?)。

我对网络知之甚少,所以在您的回复中将我视为所有方面的新手sockety/servicey。 RPi(服务器)上的源代码是:

// ZeroMQ Test Server
// Compile with
// gcc -o zserver zserver.c -lzmq

#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>

int main (void)
{
    void *context=NULL,*responder=NULL;
    int rc=1;

    //  Socket to talk to clients
    context = zmq_ctx_new ();
printf("Context pointer = %p\n",context);
    responder = zmq_socket (context, ZMQ_REP);
printf("Responder pointer = %p\n",responder);
    rc = zmq_bind (responder, "tcp://*:5555");
printf("rc = %d\n",rc);

    assert (rc == 0);

    while (1) {
        char buffer [10];
        zmq_recv (responder, buffer, 10, 0);
        printf ("Received Hello\n");
        sleep (1);          //  Do some 'work'
        zmq_send (responder, "World", 5, 0);
    }
    return 0;
}

PC端(Cygwin)客户端源码为:

// ZeroMQ Test Client
// Compile with:
// gcc -o zclient zclient.c -L/usr/local/lib -lzmq

#include <zmq.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

int main (void)
{
  void *context=NULL,*requester=NULL;

    printf ("Connecting to hello world server\n");
    context = zmq_ctx_new ();
printf("Context pointer = %p\n",context);
    requester = zmq_socket (context, ZMQ_REQ);
printf("Requester pointer = %p\n",requester);
    zmq_connect (requester, "tcp://localhost:5555");

    int request_nbr;
    for (request_nbr = 0; request_nbr != 10; request_nbr++) {
        char buffer [10];
        printf ("Sending Hello %d\n", request_nbr);
        zmq_send (requester, "Hello", 5, 0);
        zmq_recv (requester, buffer, 10, 0);
        printf ("Received World %d\n", request_nbr);
    }
    zmq_close (requester);
    zmq_ctx_destroy (context);
    return 0;
}

在 RPi LXTerminal 我 运行 服务器上得到这个:

Context pointer = 0xefe308
Responder pointer = 0xf00e08
rc = 0

在 Cygwin 上 Bash shell 我 运行 客户端得到这个:

Connecting to hello world server
Context pointer = 0x60005ab90
Requester pointer = 0x60005f890
Sending Hello 0

...他们都挂在那里 - 一个在听,另一个在发送,但彼此都没有回应。 任何线索如何开始调查将不胜感激。

+1 使用显式 zmq_close()zmq_ctx_term() 释放资源 ...
如果这是第一次使用 ZeroMQ,
在深入了解更多细节之前,人们可能会喜欢先看一下“

Q : What are the first steps I should take to investigate why this is happening?

作为步零的视线测试在这里没有意义。 所有 localhost 放置的接口都很难彼此 "see"。

接下来,作为 第一步 调用 { .bind() | .connect() } 方法进行测试,使用像 tcp://127.0.0.1:56789 这样的显式地址(以避免扩展*-通配符和localhost-符号名称翻译)

始终准备好 read/evaluate API-提供 errno ZeroMQ 不断报告关于最后一个 ZeroMQ API-操作导致错误状态。

最好阅读 ZeroMQ 本机 API 文档,该文档在各个版本之间都得到了很好的维护,以充分理解 API 设计的 signaling/messaging 元平面的舒适性。


Mea Culpa:LoS 肯定不是由 O/P 代码建立的:

  • RPi .bind()-s 在本地 I/F(否则不能)
  • PC .connect()-s 不是到RPi的那个,而是PC的本地I/F
  • PC .connect( "tcp://<address_of_RPi>:5555" ) 会成功(使用您在 Xrdp 或 SSH 中使用的相同 IP 地址连接到 RPi,或者可以从 RPi CLI 显式读取一个-在 ~$ ip address 之后的终端,并将那个用于 PC 端客户端代码)

两个不相交的 ZeroMQ AccessPoint-s 的通信方式为零,一旦没有传输-"wire" 从 A 到 B

// Zero MQ Test Server
// Compile with
// gcc -o zserver zserver.c -lzmq

#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>

int main (void)
{
    void *context=NULL,*responder=NULL;
    int rc=1;

    //  Socket to talk to clients
    context = zmq_ctx_new ();                  printf("Context pointer = %p\n",context);
    responder = zmq_socket (context, ZMQ_REP); printf("Responder pointer = %p\n",responder);
    rc = zmq_bind (responder, "tcp://*:5555"); printf("rc = %d\n",rc);
/* ----------------------------------^^^^^^------------RPi interface-----------*/
    assert (rc == 0);

    while (1) {
        char buffer [10];
        zmq_recv (responder, buffer, 10, 0);   printf("Received Hello\n");
        sleep (1);                         //  Do some 'work'
        zmq_send (responder, "World", 5, 0);
    }
    return 0;
}

PC端(Cygwin)客户端源码为:

// ZeroMQ Test Client
// Compile with:
// gcc -o zclient zclient.c -L/usr/local/lib -lzmq

#include <zmq.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

int main (void)
{
    void *context=NULL,*requester=NULL;
                                               printf("Connecting to hello world server\n");
    context = zmq_ctx_new ();                  printf("Context pointer = %p\n",context);
    requester = zmq_socket (context, ZMQ_REQ); printf("Requester pointer = %p\n",requester);
    zmq_connect (requester, "tcp://localhost:5555");
/*---------------------------------^^^^^^^^^^^^^^---------PC-local-interface------*/
    int request_nbr;
    for (request_nbr = 0; request_nbr != 10; request_nbr++) {
        char buffer [10];                      printf("Sending Hello %d\n", request_nbr);
        zmq_send (requester, "Hello", 5, 0);
        zmq_recv (requester, buffer, 10, 0);   printf("Received World %d\n", request_nbr);
    }
    zmq_close (requester);
    zmq_ctx_destroy (context);
    return 0;
}

可能还想阅读更多与 ZeroMQ 相关的主题 here


结语:

O/P 中报告的问题实际上被掩盖了,并且仍然隐藏起来,无法被 API 检测到。 ZeroMQ 允许一个 AccessPoint 同时拥有 0+ transport-class-connections,给定一个正确的语法和满足其他条件。


zmq_connect( reguester, "tcp://<address-not-intended-but-correct>:<legal-port>" )的调用将导致合法的公平状态和none定义和记录的可能错误案例-状态会被报告,因为 none 所有此类情况确实发生了:

EINVAL
The endpoint supplied is invalid.

EPROTONOSUPPORT
The requested transport protocol is not supported.

ENOCOMPATPROTO
The requested transport protocol is not compatible with the socket type.

ETERM
The ØMQ context associated with the specified socket was terminated.

ENOTSOCK
The provided socket was invalid.

EMTHREAD
No I/O thread is available to accomplish the task.


有一些机会 至少在某种程度上-"detect"麻烦的是强制执行另一种 exception/error,但推迟到 { zmq_recv() | zmq_recv() } 的非阻塞形式的调用中,这些可能会变成报告 EAGAIN 或者可能是 EFSM 因为没有完成端到端重新确认的 ZMTP 协议握手(在具有远程 RPi 服务器端的 PC 本地主机端口上没有也永远不会遇到对手方)。这还需要预先设置 zmq_setsockopt( responder, ZMQ_IMMEDIATE, 1 ) 和其他配置细节。

下一个,在 ZeroMQ v4.+ 中,有机会检查 AccessPoint 内部报告事件的子集,使用 "inspection-socket" 通过相当复杂的实例化策略
int zmq_socket_monitor (void *socket, char *endpoint, int events); 通过 inproc:// transport-class 附加到 AccessPoint 的内部结构 ~ 这里 "inproc://myPCsocketAccessPOINT_monitor" 像这样:

rc = zmq_socket_monitor( responder,                               // AccessPoint to monitor
                        "inproc://myPCsocketAccessPOINT_monitor", // symbolinc name
                         ZMQ_ALL_EVENTS                           // scope of Events
                         );

这样创建的内部监控 "inspection-socket" 接下来可能会 zmq_connect()-ed 喜欢:

void             *my_end_of_monitor_socket = zmq_socket ( context, ZMQ_PAIR );
rc = zmq_connect( my_end_of_monitor_socket,               // local-end PAIR-socket AccessPoint
                 "inproc://myPCsocketAccessPOINT_monitor" // symbolic name
                  );

最后,我们可以使用它来读取一系列事件(并据此采取行动):

int event = get_monitor_event( my_end_of_monitor_socket, NULL, NULL );
if (event == ZMQ_EVENT_CONNECT_DELAYED) { ...; }
if (event == ... ) { ...; }

像这样使用一个简单的 get_monitor_event() 作为工具,它处理一些阅读和解释从实例化的 "internal"-monitor 中订购的多部分消息的内部规则接入点:

// Read one event off the monitor socket; return value and address
// by reference, if not null, and event number by value. Returns -1
// in case of error.

static int
get_monitor_event ( void *monitor, int *value, char **address )
{
// First frame in message contains event number and value
   zmq_msg_t msg;
   zmq_msg_init (&msg);
   if (zmq_msg_recv (&msg, monitor, 0) == -1) return -1; // Interrupted, presumably
   assert (zmq_msg_more (&msg));

   uint8_t *data = (uint8_t *) zmq_msg_data (&msg);
   uint16_t event = *(uint16_t *) (data);

   if (value) *value = *(uint32_t *) (data + 2);

// Second frame in message contains event address
   zmq_msg_init (&msg);
   if (zmq_msg_recv (&msg, monitor, 0) == -1) return -1; // Interrupted, presumably
   assert (!zmq_msg_more (&msg));

   if (address) {
      uint8_t *data = (uint8_t *) zmq_msg_data (&msg);
      size_t size = zmq_msg_size (&msg);
      *address = (char *) malloc (size + 1);
      memcpy (*address, data, size);
      (*address)[size] = 0;
   }
   return event;
}

可以监视哪些内部-API-事件?

截至 v4.2 API 的状态,有这组 "internal"-monitor(able) internal-API-events:

ZMQ_EVENT_CONNECTED
The socket has successfully connected to a remote peer. The event value is the file descriptor (FD) of the underlying network socket. Warning: there is no guarantee that the FD is still valid by the time your code receives this event.
ZMQ_EVENT_CONNECT_DELAYED
A connect request on the socket is pending. The event value is unspecified.
ZMQ_EVENT_CONNECT_RETRIED
A connect request failed, and is now being retried. The event value is the reconnect interval in milliseconds. Note that the reconnect interval is recalculated at each retry.
ZMQ_EVENT_LISTENING
The socket was successfully bound to a network interface. The event value is the FD of the underlying network socket. Warning: there is no guarantee that the FD is still valid by the time your code receives this event.
ZMQ_EVENT_BIND_FAILED
The socket could not bind to a given interface. The event value is the errno generated by the system bind call.
ZMQ_EVENT_ACCEPTED
The socket has accepted a connection from a remote peer. The event value is the FD of the underlying network socket. Warning: there is no guarantee that the FD is still valid by the time your code receives this event.
ZMQ_EVENT_ACCEPT_FAILED
The socket has rejected a connection from a remote peer. The event value is the errno generated by the accept call.
ZMQ_EVENT_CLOSED
The socket was closed. The event value is the FD of the (now closed) network socket.
ZMQ_EVENT_CLOSE_FAILED
The socket close failed. The event value is the errno returned by the system call. Note that this event occurs only on IPC transports.
ZMQ_EVENT_DISCONNECTED
The socket was disconnected unexpectedly. The event value is the FD of the underlying network socket. Warning: this socket will be closed.
ZMQ_EVENT_MONITOR_STOPPED
Monitoring on this socket ended.
ZMQ_EVENT_HANDSHAKE_FAILED
The ZMTP security mechanism handshake failed. The event value is unspecified.

NOTE: in DRAFT state, not yet available in stable releases.
ZMQ_EVENT_HANDSHAKE_SUCCEED


NOTE: as new events are added, the catch-all value will start returning them. An application that relies on a strict and fixed sequence of events must not use ZMQ_EVENT_ALL in order to guarantee compatibility with future versions.
Each event is sent as two frames. The first frame contains an event number (16 bits), and an event value (32 bits) that provides additional data according to the event number. The second frame contains a string that specifies the affected TCP or IPC endpoint.

zmq_connect中必须注明树莓派的IP地址(已执行zmq_bind:

应该是:

// on PC, remote ip is the raspberry one, the one you use for ssh for instance
rc = zmq_connect(requester, "tcp://<remote ip>:5555");