ZeroMQ实例

使用库版本
zeromq-4.0.3

 接收端代码

#include <zmq.h>
#include "stdio.h"

int main(int argc, char * argv[])
{
    void * pCtx = NULL;
    void * pSock = NULL;
    const char * pAddr = "tcp://*:7766";

    //创建context,zmq的socket 需要在context上进行创建 
    if((pCtx = zmq_ctx_new()) == NULL)
    {
        return 0;
    }
    //创建zmq socket ,socket目前有6中属性 ,这里使用dealer方式具体使用方式请参考zmq官方文档 
    if((pSock = zmq_socket(pCtx, ZMQ_DEALER)) == NULL)
    {
        zmq_ctx_destroy(pCtx);
        return 0;
    }
    int iRcvTimeout = 5000;// millsecond
    //设置zmq的接收超时时间为5秒 
    if(zmq_setsockopt(pSock, ZMQ_RCVTIMEO, &iRcvTimeout, sizeof(iRcvTimeout)) < 0)
    {
        zmq_close(pSock);
        zmq_ctx_destroy(pCtx);
        return 0;
    }
    //绑定地址 tcp://*:7766 也就是使用tcp协议进行通信,使用网络端口 7766
    if(zmq_bind(pSock, pAddr) < 0)
    {
        zmq_close(pSock);
        zmq_ctx_destroy(pCtx);
        return 0;
    }
    printf("bind at : %s\n", pAddr);
    while(1)
    {
        char szMsg[1024] = {0};
        printf("waitting...\n");
        errno = 0;
        //循环等待接收到来的消息,当超过5秒没有接到消息时,zmq_recv函数返回错误信息
        if(zmq_recv(pSock, szMsg, sizeof(szMsg), 0) < 0)
        {
            printf("error = %s\n", zmq_strerror(errno));
            continue;
        }
        printf("received message : %s\n", szMsg);
    }

    return 0;
}

发送端代码

#include <zmq.h>
#include "stdio.h"

int main(int argc, char * argv[])
{
    void * pCtx = NULL;
    void * pSock = NULL;
    //使用tcp协议进行通信,需要连接的目标机器IP地址为192.168.1.2通信使用的网络端口 为7766 
    const char * pAddr = "tcp://192.168.1.2:7766";

    //创建context 
    if((pCtx = zmq_ctx_new()) == NULL)
    {
        return 0;
    }
    //创建socket 
    if((pSock = zmq_socket(pCtx, ZMQ_DEALER)) == NULL)
    {
        zmq_ctx_destroy(pCtx);
        return 0;
    }
    int iSndTimeout = 5000;// millsecond
    //设置接收超时 
    if(zmq_setsockopt(pSock, ZMQ_RCVTIMEO, &iSndTimeout, sizeof(iSndTimeout)) < 0)
    {
        zmq_close(pSock);
        zmq_ctx_destroy(pCtx);
        return 0;
    }
    //连接目标IP192.168.1.2,端口7766 
    if(zmq_connect(pSock, pAddr) < 0)
    {
        zmq_close(pSock);
        zmq_ctx_destroy(pCtx);
        return 0;
    }
    //循环发送消息 
    while(1)
    {
        static int i = 0;
        char szMsg[1024] = {0};
        snprintf(szMsg, sizeof(szMsg), "hello world : %3d", i++);
        printf("Enter to send...\n");
        if(zmq_send(pSock, szMsg, sizeof(szMsg), 0) < 0)
        {
            fprintf(stderr, "send message faild\n");
            continue;
        }
        printf("send message : [%s] succeed\n", szMsg);
        getchar();
    }

    return 0;
}
编译
gcc -o recv recv.c -lzmq
gcc -o send send.c -lzmq

运行

export LD_LIBRARY_PATH=/usr/local/lib/

接收端

$ ./recv 
bind at : tcp://*:7766
waitting...
received message : hello world :   0
waitting...
received message : hello world :   1
waitting...
received message : hello world :   2
waitting...
received message : hello world :   3
waitting...
received message : hello world :   4
waitting...
received message : hello world :   5
waitting...

发送端

$ ./send 
Enter to send...
send message : [hello world :   0] succeed

Enter to send...
send message : [hello world :   1] succeed

Enter to send...
send message : [hello world :   2] succeed

Enter to send...
send message : [hello world :   3] succeed

Enter to send...
send message : [hello world :   4] succeed

Enter to send...
send message : [hello world :   5] succeed

原创文章 96 获赞 48 访问量 6万+

猜你喜欢

转载自blog.csdn.net/wteruiycbqqvwt/article/details/103782806
今日推荐