A server node can bind to many endpoints (that is, acombination of protocol and address) and it can do this using a single socket.This means it will accept connections across different transports:
zmq_bind(socket,
"tcp://*:5555");
zmq_bind (socket, "tcp://*:9999");
zmq_bind (socket, "inproc://somename");
服务器可以同时监听多个端口,比如服务器监听端口5555和6666,那么客户端一调用zmq_connect(req,"tcp://127.0.0.1:6666")建立连接,客户端二调用zmq_connect(req,"tcp://127.0.0.1:55555")建立连接.服务器可以同时和这两个客户端进行通信
下面给出一个例子:
server.cpp代码:
#include "zmq.h"
#include <stdio.h>
#include <string.h>
#include<windows.h>
int main (void)
{
void *context = zmq_ctx_new ();
// Socket to talk to clients
void *responder = zmq_socket (context, ZMQ_REP);
//绑定多个端口
zmq_bind (responder, "tcp://127.0.0.1:5555");
zmq_bind (responder, "tcp://127.0.0.1:6666");
while (1) {
// Wait for next request from client
zmq_msg_t request;
zmq_msg_init (&request);
int ret = zmq_msg_recv (&request, responder, 0);
request._[ret] = '\0';
// Do some 'work'
Sleep (1);
// Send reply back to client
zmq_msg_send (&request, responder, 0);
zmq_msg_close (&request);
}
// We never get here but if we did, this would be how we end
zmq_close (responder);
zmq_ctx_destroy (context);
return 0;
}
clent1.cpp代码:
#include "zmq.h"
#include <stdio.h>
#include <string.h>
#include<windows.h>
int main (void)
{
void *context = zmq_ctx_new ();
// Socket to talk to clients
void *req = zmq_socket (context, ZMQ_REQ);
//ZMQ_EXPORT int zmq_connect (void *s, const char *addr);
zmq_connect(req,"tcp://127.0.0.1:5555");
while (1) {
// Send reply back to client
zmq_msg_t reply;
zmq_msg_init_size (&reply, 5);
memcpy (zmq_msg_data (&reply), "World", 5);
int ret = zmq_msg_send (&reply, req, 0);
reply._[ret] = '\0';
printf("Send %s\n", &reply);
zmq_msg_close (&reply);
// Do some 'work'
Sleep (1000);
// Wait for next request from client
zmq_msg_t request;
zmq_msg_init (&request);
int rec = zmq_msg_recv (&request, req, 0);
request._[rec] = '\0';
printf ("Received %s\n",&request);
zmq_msg_close (&request);
}
// We never get here but if we did, this would be how we end
zmq_close (req);
zmq_ctx_destroy (context);
return 0;
}
client 2.cpp代码:
#include "zmq.h"
#include <stdio.h>
#include <string.h>
#include<windows.h>
int main (void)
{
void *context = zmq_ctx_new ();
// Socket to talk to clients
void *req = zmq_socket (context, ZMQ_REQ);
//ZMQ_EXPORT int zmq_connect (void *s, const char *addr);
zmq_connect(req,"tcp://127.0.0.1:6666");
while (1) {
// Send reply back to client
zmq_msg_t reply;
zmq_msg_init_size (&reply, 7);
memcpy (zmq_msg_data (&reply), "HuZhong", 7);
int ret = zmq_msg_send (&reply, req, 0);
reply._[ret] = '\0';
printf("Send %s\n", &reply);
zmq_msg_close (&reply);
// Do some 'work'
Sleep (1000);
// Wait for next request from client
zmq_msg_t request;
zmq_msg_init (&request);
int rec = zmq_msg_recv (&request, req, 0);
request._[rec] = '\0';
printf ("Received %s\n",&request);
zmq_msg_close (&request);
}
// We never get here but if we did, this would be how we end
zmq_close (req);
zmq_ctx_destroy (context);
return 0;
}
运行效果如下:

With most transports you cannot bind to the same endpoint twice, unlike for example in UDP
(在以往的套接字编程,我们不能将一个套接字绑定端口两次)
服务器节点可以使用zmq_bind在一个套接字上绑定多个端点(协议和地址的组合),允许跨不同传输方式接受连接。例如,服务器可以同时监听tcp://*:5555、tcp://*:9999和inproc://somename。客户端通过zmq_connect发起连接,服务器能同时与多个客户端通信。示例代码展示了服务器和两个客户端的交互,而大多数传输方式不允许对相同端点进行二次绑定,这与UDP不同。
598

被折叠的 条评论
为什么被折叠?



