Chinaunix首页 | 论坛 | 博客
  • 博客访问: 46667
  • 博文数量: 13
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 140
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-25 18:24
个人简介

公众号:RiboseYim 个人站点:http://riboseyim.github.io?source=chinaunix

文章分类

全部博文(13)

分类: C/C++

2016-03-20 00:15:45

Hello ZeroMQ 

安装

点击(此处)折叠或打开

  1. git clone --depth=1 https://github.com/imatix/zguide.git
  2. ./configure
  3. make
  4. make install

编译依赖

libsodium-1.0.0 (zeromq-4.1.2) 


Github Issues: 
[zmq-4.1.2 make failed as libsodium-1.0.8 sodium_init #1854]()
~~~
cc1plus: warnings being treated as errors
src/curve_client.cpp: In constructor 
'zmq::curve_client_t::curve_client_t(const zmq::options_t&)':
src/curve_client.cpp:61: warning: ignoring return value of
 'int sodium_init()', 
declared with attribute warn_unused_result
~~~

C Project 库

点击(此处)折叠或打开

  1. gcc -L/usr/local/lib -o "veto_mq_server" ./src/veto_mq_server.o -lzmq

可能的异常:库路径、名称错误

Undefined symbols for architecture x86_64:
"_zmq_bind", referenced from:
_main in veto_mq_server.o
"_zmq_ctx_new", referenced from:
_main in veto_mq_server.o
"_zmq_recv", referenced from:
_main in veto_mq_server.o
"_zmq_send", referenced from:
_main in veto_mq_server.o
"_zmq_socket", referenced from:
_main in veto_mq_server.o
** ld: symbol(s) not found for architecture x86_64  **
**clang: error: linker command failed with exit code 1 (use -v to see invocation)**

Client-Server Model


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <zmq.h>
  6. #include <zmq_utils.h>
  7. #include "veto_mq_utils.h"

  8. int main (void)
  9. {
  10.  //
  11.  print_zmq_version();
  12.     // Socket to talk to clients
  13.     void *context = zmq_ctx_new ();
  14.     void *responder = zmq_socket (context, ZMQ_REP);
  15.     int rc = zmq_bind (responder, "tcp://*:5555");
  16.     assert (rc == 0);


  17.     while (1) {
  18.         char buffer [10];
  19.         zmq_recv (responder, buffer, 10, 0);


  20.         printf ("Received Hello \n");
  21.         sleep (1); // Do some 'work'
  22.         zmq_send (responder, "World", 5, 0);
  23.     }
  24.     return 0;
  25. }

点击(此处)折叠或打开

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <unistd.h>

  4. #include <zmq.h>
  5. #include <zmq_utils.h>

  6. int main(void) {

  7.          printf ("Connecting to hello world server...\n");

  8.      void *context = zmq_ctx_new ();
  9.      void *requester = zmq_socket (context, ZMQ_REQ);

  10.      zmq_connect (requester, "tcp://localhost:5555");

  11.      int request_nbr;
  12.      for (request_nbr = 0; request_nbr != 10; request_nbr++) {
  13.      char buffer [10];
  14.      printf ("Sending Hello %d...\n", request_nbr);
  15.      zmq_send (requester, "Hello", 5, 0);
  16.      zmq_recv (requester, buffer, 10, 0);
  17.      printf ("Received World %d\n", request_nbr);
  18.      }

  19.      zmq_close (requester);
  20.      zmq_ctx_destroy (context);

  21.         return 0;
  22. }

Makefile


点击(此处)折叠或打开

  1. SHELL = /bin/sh
  2. prefix = /usr/local
  3. exec_prefix=${prefix}
  4. srcdir = .
  5. sbindir = ${exec_prefix}/sbin
  6. libdir = ${exec_prefix}/lib
  7. sysconfdir = ${prefix}/etc
  8. SETUPDIR = /home/nms
  9. TARGETDIR = $(SETUPDIR)/bin
  10. SRCDIR = .
  11. CC = gcc
  12. CFLAGS = -O2 -Wall
  13. CPPFLAGS= -g
  14. DEFS =
  15. LDFLAGS =
  16. LIBS = -lzmq -L/usr/local/lib
  17. INCLUDES= -I/usr/local/include

  18. CURR_DIR = $(shell pwd)
  19. CC_TMP = lib
  20. OBJS = $(CC_TMP)/veto_mq_server.o $(CC_TMP)/veto_mq_utils.o
  21. TARGET = veto_mq_server

  22. .c.o:
  23.  $(CC) -I. $(CPPFLAGS) $(DEFS) $(CFLAGS) -c $<

  24. all: $(TARGET)

  25. $(TARGET): $(OBJS)
  26.  $(CC) $(CPPFLAGS) $(OBJS) $(LIBS) $(LDFLAGS) -o $@
  27.  -cp $(SRCDIR)/$(TARGET) $(TARGETDIR)/$(TARGET)
  28.  -chmod ugo+s $(TARGETDIR)/$(TARGET)
  29.  -chmod ugo+s $(SRCDIR)/$(TARGET)

  30. clean:
  31.  -rm -f *.o *~ *.core core $(TARGET)
  32.  -rm -f $(TARGETDIR)/$(TARGET)

  33. veto_mq_server.o: veto_mq_server.c
  34.  $(CC) -g -c -o $(CC_TMP)/veto_mq_server.o veto_mq_server.c $(CPPFLAGS) $(INCLUDES)

  35. veto_mq_utils.o: veto_mq_utils.c
  36.  $(CC) -g -c -o $(CC_TMP)/veto_mq_utils.o veto_mq_utils.c $(INCLUDES)



 Running

点击(此处)折叠或打开

  1. bash-3.2# ./veto_mq_server


  2. bash-3.2# ps -ef | grep mq | grep -v 'grep'
  3.     0 3616 1 0 10:00AM ttys000 0:00.01 ./veto_mq_server



点击(此处)折叠或打开

  1. bash-3.2# ./veto_mq_client


Connecting to hello world server...
Sending Hello 0...
Received World 0
Sending Hello 1...
Received World 1
Sending Hello 2...
Received World 2
Sending Hello 3...
Received World 3
Sending Hello 4...
Received World 4
Sending Hello 5...
Received World 5
Sending Hello 6...
Received World 6
Sending Hello 7...
Received World 7
Sending Hello 8...
Received World 8
Sending Hello 9...
Received World 9





阅读(2094) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~