安装zeromq

亲测有效系列

下载zmq:下载地址
解压:tar -zxvf zeromq-4.1.6.tar.gz
编译安装
A.执行configure文件:./configure
出现错误:
configure: error: Package requirements (libsodium) were not met:
No package ‘libsodium’ found
解决方案:忽略这个库
./configure --prefix=/home/ygy/zmq --without-libsodium(prefix中的路径是zmq存放的目录)
B.编译:make
C.安装:make install

三、样例程序
(1)server端代码:server.cpp

#include                                                                                                                                                           
#include                                                                                     
#include                                                                                     
#include                                                                                     
#include                                                                                      
                                                                                                       
int main (void)                                                                                        
{                                                                                                      
    //  Socket to talk to clients                                                                      
    void *context = zmq_ctx_new ();                                                                    
    void *responder = zmq_socket (context, ZMQ_REP);                                                   
    int rc = zmq_bind (responder, "tcp://*:5555");                                                     
    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;                                                                                          
}

(2)client端代码:client.cpp

#include                                                                                        
#include                                                                                     
#include                                                                                      
#include                                                                                     
                                                                                                       
int main (void)                                                                                        
{                                                                                                      
    printf ("Connecting to hello world server…\n");                                                   
                                                                                                                                                                       
    void *context = zmq_ctx_new ();                                                                    
    void *requester = zmq_socket (context, ZMQ_REQ);                                                                                                       
    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;                                                                                          
}

3)编译
g++ server.cpp -o server -lzmq -I./include -L./lib
g++ client.cpp -o client -lzmq -I./include -L./lib
(4)运行程序
先运行server端:./server
出现错误:
./server: error while loading shared libraries: libzmq.so.5: cannot open shared object file: No such file or directory
解决方案:
找出libzmq.so所在路径:sudo find ./ -depth -name “libzmq.so” -print
方案一:需要root权限,永久有效
将找到的路径写入ld.so.conf文件中:echo “填入所找到的路径” >> /etc/ld.so.conf
重新加载配置文件:sudo ldconfig

参考资料

https://blog.csdn.net/xiaoyezi_1834/article/details/51298408

你可能感兴趣的:(其他)