• (转)zeromq 安装


    今天在安装zeromq的时候费了好大的力气才算装好

    下面来回顾一下在linux安装zeromq的过程

    首先

    http://download.zeromq.org/

    1.下载最新版的zeromq

        http://download.zeromq.org/zeromq-3.1.0-beta.tar.gz

    2 解压

        tar -xvf zeromq-3.1.0-beta.tar.gz

    3 运行configure

        ./configure --prefix=/data/zeromq     (prefix  指定安装目录)

    4. make

    5. make install

    6. 设置环境变量

        export CPPFLAGS=-I/home/mine/0mq/include/

        export LDFLAGS=-L/home/mine/0mq/lib/

    7.测试代码

    server.c

    #######################################

    #include </data/zeromq/include/zmq.h>

    #include <stdio.h>

    #include <unistd.h>

    #include <string.h>

    #include <stdlib.h>

    int main (void)

    {

        void *context = zmq_init (1);

    // Socket to talk to clients

        void *responder = zmq_socket (context, ZMQ_REP);

        zmq_bind (responder, "tcp://192.168.0.185:5555");

        printf("binding on port 5555. waiting client send message... ");

        while (1) {

            // Wait for next request from client

            zmq_msg_t request;

            zmq_msg_init (&request);

            char buf[32];

            zmq_recv (responder,buf, &request, 0);

            int size = zmq_msg_size (&request);

            char *string = malloc (size + 1);

            memset(string,0,size+1);

            memcpy (string, zmq_msg_data (&request), size);

            printf ("Received Hello string=[%s] ",string);

            free(string);

            zmq_msg_close (&request);

            // Do some 'work'

            sleep (1);

            // Send reply back to client

            zmq_msg_t reply;

            char res[128]={0};

            snprintf(res,127,"reply:%d",random());

            zmq_msg_init_size (&reply, strlen(res));

            memcpy (zmq_msg_data (&reply), res, strlen(res));

            char buf2[32];

            zmq_send (responder, buf2, &reply, 0);

            zmq_msg_close (&reply);

        }

        // We never get here but if we did, this would be how we end

        zmq_close (responder);

        zmq_term (context);

        return 0;

    }

    ########################################################

    #client.c

    #include <zmq.h> 

    #include <string.h> 

    #include <stdio.h> 

    #include <unistd.h>  

    int main () 

    {     

     void *context = zmq_init (1); //  Socket to talk to server     

     printf ("Connecting to hello world server... ");     

     void *requester = zmq_socket (context, ZMQ_REQ);     

     zmq_connect (requester, "tcp://192.168.0.185:5555");       

     int request_nbr;     

     for (request_nbr = 0; request_nbr != 10; request_nbr++) 

     {         

      zmq_msg_t request;         

      zmq_msg_init_data (&request, "Hello", 6, NULL, NULL);         

      printf ("Sending request %d... ", request_nbr);         

      zmq_send (requester, &request, 0,0); 

      printf("send over")      ; 

      zmq_msg_close (&request);           

      zmq_msg_t reply;         

      zmq_msg_init (&reply);         

      zmq_recv (requester, &reply, 0,0);         

      printf ("Received reply %d: [%s] ", request_nbr, (char *) zmq_msg_data (&reply));         

      zmq_msg_close (&reply);     

     }     

     zmq_close (requester);     

     zmq_term (context);     

     return 0; 

    }

    gcc server.c -o server -lzmq   -L/data/zeromq/lib -I/data/zeromq/include 

    gcc client.c -o client -lzmq   -L/data/zeromq/lib -I/data/zeromq/include 

    ./server

    ./client

    jzmq 是zeromq 的java客户端

    下载地址

    https://github.com/zeromq/jzmq/downloads

    1. tar xzf zeromq-jzmq-semver-90-g58c6108.tar.gz

    2. ./configure

       这个时候需要上面环境变量的配置,

       还需要

       随便  

        vi /data/zeromq/.bashrc

        export PATH=$PATH:/data/zeromq/include

        export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/data/zeromq/lib:/data/zeromq/include

        只要configure 执行完毕就会生成makefile文件

    3.make

    4.make install

    测试

    hwserver.java

    //

    //  Hello World server in Java

    //  Binds REP socket to tcp://*:5555

    //  Expects "Hello" from client, replies with "World"

    //

    //  Naveen Chawla <naveen.chwl@gmail.com>

    //

    import org.zeromq.ZMQ;

    public class hwserver {

        public static void main(String[] args) {

            //  Prepare our context and socket

            ZMQ.Context context = ZMQ.context(1);

            ZMQ.Socket socket = context.socket(ZMQ.REP);

            socket.bind ("tcp://*:5555");

            while (true) {

                byte[] request;

                //  Wait for next request from client

                //  We will wait for a 0-terminated string (C string) from the client,

                //  so that this server also works with The Guide's C and C++ "Hello World" clients

                request = socket.recv (0);

                //  In order to display the 0-terminated string as a String,

                //  we omit the last byte from request

                System.out.println ("Received request: [" +

                new String(request,0,request.length-1)  //  Creates a String from request, minus the last byte

                + "]");

                //  Do some 'work'

                try {

                    Thread.sleep (1000);

                }

                catch(InterruptedException e){

                    e.printStackTrace();

                }

                //  Send reply back to client

                //  We will send a 0-terminated string (C string) back to the client,

                //  so that this server also works with The Guide's C and C++ "Hello World" clients

                String replyString = "World" + " ";

                byte[] reply = replyString.getBytes();

                reply[reply.length-1]=0; //Sets the last byte of the reply to 0

                socket.send(reply, 0);

            }

        }

    }

    hwclient.java

    //

    //  Hello World client in Java

    //  Connects REQ socket to tcp://localhost:5555

    //  Sends "Hello" to server, expects "World" back

    //

    //  Naveen Chawla <naveen.chwl@gmail.com>

    //

    import org.zeromq.ZMQ;

    public class hwclient{

        public static void main(String[] args){

            //  Prepare our context and socket

            ZMQ.Context context = ZMQ.context(1);

            ZMQ.Socket socket = context.socket(ZMQ.REQ);

            System.out.println("Connecting to hello world server...");

            socket.connect ("tcp://localhost:5555");

            //  Do 10 requests, waiting each time for a response

            for(int request_nbr = 0; request_nbr != 10; request_nbr++) {

                //  Create a "Hello" message.

                //  Ensure that the last byte of our "Hello" message is 0 because

                //  our "Hello World" server is expecting a 0-terminated string:

                String requestString = "Hello" + " ";

                byte[] request = requestString.getBytes();

                request[request.length-1]=0; //Sets the last byte to 0

                // Send the message

                System.out.println("Sending request " + request_nbr + "...");

                socket.send(request, 0);

                //  Get the reply.

                byte[] reply = socket.recv(0);

                //  When displaying reply as a String, omit the last byte because

                //  our "Hello World" server has sent us a 0-terminated string:

                System.out.println("Received reply " + request_nbr + ": [" + new String(reply,0,reply.length-1) + "]");

            }

        }

    }

    javac -classpath /data/jzmq/share/java/zmq.jar -d . h*.java

    java -Djava.library.path=/data/jzmq/lib -cp /data/jzmq/share/java/zmq.jar:. hwserver

    java -Djava.library.path=/data/jzmq/lib -cp /data/jzmq/share/java/zmq.jar:. hwclient

    显示内容:

    server端

    Received request: [Hello]

    Received request: [Hello]

    Received request: [Hello]

    Received request: [Hello]

    Received request: [Hello]

    Received request: [Hello]

     
    client端
    Connecting to hello world server...
    Sending request 0...
    Received reply 0: [World]
    Sending request 1...
    Received reply 1: [World]
    Sending request 2...
    Received reply 2: [World]
    Sending request 3...
    Received reply 3: [World]
    Sending request 4...
    Received reply 4: [World]
    Sending request 5...
  • 相关阅读:
    AtCoder Beginner Contest 162 C~F
    题解 | 【CF896B】 Ithea Plays With Chtholly
    C# | C#快速入门
    Codeforces Round #618 (Div. 2) A~E
    Educational Codeforces Round 92 (Rated for Div. 2) A~C
    使用 Python 参与算法竞赛
    【网络爬虫学习】实战,爬取网页以及贴吧数据
    国内pip源提示“not a trusted or secure host”解决方案
    【网络爬虫学习】第一个Python爬虫程序 & 编码与解码详解 & Pythonの实现
    【网络爬虫学习】网页的基本构成
  • 原文地址:https://www.cnblogs.com/fkissx/p/5595194.html
Copyright © 2020-2023  润新知