ØMQ API 学习之zmq_bind

服务器节点可以使用zmq_bind在一个套接字上绑定多个端点(协议和地址的组合),允许跨不同传输方式接受连接。例如,服务器可以同时监听tcp://*:5555、tcp://*:9999和inproc://somename。客户端通过zmq_connect发起连接,服务器能同时与多个客户端通信。示例代码展示了服务器和两个客户端的交互,而大多数传输方式不允许对相同端点进行二次绑定,这与UDP不同。
部署运行你感兴趣的模型镜像

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");
服务器可以同时监听多个端口,比如服务器监听端口55556666,那么客户端一调用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

(在以往的套接字编程,我们不能将一个套接字绑定端口两次)

 

您可能感兴趣的与本文相关的镜像

Qwen3-8B

Qwen3-8B

文本生成
Qwen3

Qwen3 是 Qwen 系列中的最新一代大型语言模型,提供了一整套密集型和专家混合(MoE)模型。基于广泛的训练,Qwen3 在推理、指令执行、代理能力和多语言支持方面取得了突破性进展

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值