<p>您的<strong><code>socket.recv()</code></strong>-ed数据与ZeroMQ规范完全匹配,尽管它们不必让您感到高兴,而且您怀疑为什么会得到这些数据,而不是准确地传递所发送消息的精确副本。在</p>
<p>所以,耐心点,继续阅读。在</p>
<h2>ZeroMQ最近添加了<code>STREAM</code>套接字原型相当具体</h2>
<p>任何对ZeroMQ信令/消息传递工具有几年经验的人都会告诉您,最近(v4.x)添加的<code>STREAM</code>原型并不是ZeroMQ进程与ZeroMQ进程相互通信需求的最佳选择。在</p>
<p>为什么?ZeroMQ工具所拥有的所有gem都是并且必须是<code>STREAM</code>中的快捷方式,以便允许ZeroMQ套接字访问点能够与另一个套接字端点进程“对话”,后者对ZeroMQ智能套接字高级协议一无所知。在</p>
<blockquote>
<p><strong>Native pattern</strong></p>
<p>The native pattern is used for communicating with TCP peers and allows asynchronous requests and replies in either direction.
<strong><code>ZMQ_STREAM</code></strong></p>
<p>A socket of type <code>ZMQ_STREAM</code> is used to send and receive TCP data from a non-ØMQ peer, when using the <strong><code>tcp://</code></strong> transport. A <code>ZMQ_STREAM</code> socket can act as client and/or server, sending and/or receiving TCP data asynchronously.</p>
<p>When receiving TCP data, a <code>ZMQ_STREAM</code> socket shall prepend a message part containing the identity of the originating peer to the message before passing it to the application. Messages received are fair-queued from among all connected peers.</p>
<p>When sending TCP data, a <code>ZMQ_STREAM</code> socket shall remove the first part of the message and use it to determine the identity of the peer the message shall be routed to, and unroutable messages shall cause an <code>EHOSTUNREACH</code> or <code>EAGAIN</code> error.</p>
<p>To open a connection to a server, use the <strong><code>zmq_connect()</code></strong> call, and then fetch the socket identity using the <code>ZMQ_IDENTITY</code> <strong><code>zmq_getsockopt()</code></strong> call.</p>
<p>To close a specific connection, send the identity frame followed by a zero-length message (see EXAMPLE section).</p>
<p>When a connection is made, a zero-length message will be received by the application. Similarly, when the peer disconnects (or the connection is lost), a zero-length message will be received by the application.</p>
<p>You must send one identity frame followed by one data frame. The <code>ZMQ_SNDMORE</code> flag is required for identity frames but is ignored on data frames.</p>
<h2>EXAMPLE</h2>
</blockquote>
<pre><code>void *ctx = zmq_ctx_new ();
assert ( ctx );
/* Create ZMQ_STREAM socket */
void *socket = zmq_socket ( ctx, ZMQ_STREAM );
assert ( socket );
int rc = zmq_bind ( socket, "tcp://*:8080" );
assert ( rc == 0 );
/* Data structure to hold the ZMQ_STREAM ID */
uint8_t id [256];
size_t id_size = 256;
/* Data structure to hold the ZMQ_STREAM received data */
uint8_t raw [256];
size_t raw_size = 256;
while ( 1 ) {
/* Get HTTP request; ID frame and then request */
id_size = zmq_recv ( socket, id, 256, 0 );
assert ( id_size > 0 );
do {
raw_size = zmq_recv ( socket, raw, 256, 0 );
assert ( raw_size >= 0 );
} while ( raw_size == 256 );
/* Prepares the response */
char http_response [] =
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/plain\r\n"
"\r\n"
"Hello, World!";
/* Sends the ID frame followed by the response */
zmq_send ( socket, id, id_size, ZMQ_SNDMORE );
zmq_send ( socket, http_response, strlen ( http_response ), 0 );
/* Closes the connection by sending the ID frame followed by a zero response */
zmq_send ( socket, id, id_size, ZMQ_SNDMORE );
zmq_send ( socket, 0, 0, 0 );
}
zmq_close ( socket );
zmq_ctx_destroy ( ctx );
</code></pre>
<hr/>
<p>如果您遵循多连接套接字情况下<code>STREAM</code>行为的描述,那么发送方将碰巧在<strong><code>socket</code></strong>实例上接收一个公平队列循环读取,该实例连接到多个端点(1x通过<code>.connect()</code>+Nx,通过<code>.bind()</code>,<code>N = < 0, +INF )</code>)连接到多个端点,到目前为止还没有对计数进行任何控制或者/以及通信对等点的性质,但是在<code>socket.recv()</code>-s上有一个公平的排队循环机制,这绝对不是一个安全的设计实践。在</p>
^{pr2}$