Hello ZeroMQ
安装
-
git clone --depth=1 https://github.com/imatix/zguide.git
-
./configure
-
make
-
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 库
-
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
-
#include <stdio.h>
-
#include <unistd.h>
-
#include <string.h>
-
#include <assert.h>
-
#include <zmq.h>
-
#include <zmq_utils.h>
-
#include "veto_mq_utils.h"
-
-
int main (void)
-
{
-
//
-
print_zmq_version();
-
// 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;
-
}
-
#include <string.h>
-
#include <stdio.h>
-
#include <unistd.h>
-
-
#include <zmq.h>
-
#include <zmq_utils.h>
-
-
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;
-
}
Makefile
-
SHELL = /bin/sh
-
prefix = /usr/local
-
exec_prefix=${prefix}
-
srcdir = .
-
sbindir = ${exec_prefix}/sbin
-
libdir = ${exec_prefix}/lib
-
sysconfdir = ${prefix}/etc
-
SETUPDIR = /home/nms
-
TARGETDIR = $(SETUPDIR)/bin
-
SRCDIR = .
-
CC = gcc
-
CFLAGS = -O2 -Wall
-
CPPFLAGS= -g
-
DEFS =
-
LDFLAGS =
-
LIBS = -lzmq -L/usr/local/lib
-
INCLUDES= -I/usr/local/include
-
-
CURR_DIR = $(shell pwd)
-
CC_TMP = lib
-
OBJS = $(CC_TMP)/veto_mq_server.o $(CC_TMP)/veto_mq_utils.o
-
TARGET = veto_mq_server
-
-
.c.o:
-
$(CC) -I. $(CPPFLAGS) $(DEFS) $(CFLAGS) -c $<
-
-
all: $(TARGET)
-
-
$(TARGET): $(OBJS)
-
$(CC) $(CPPFLAGS) $(OBJS) $(LIBS) $(LDFLAGS) -o $@
-
-cp $(SRCDIR)/$(TARGET) $(TARGETDIR)/$(TARGET)
-
-chmod ugo+s $(TARGETDIR)/$(TARGET)
-
-chmod ugo+s $(SRCDIR)/$(TARGET)
-
-
clean:
-
-rm -f *.o *~ *.core core $(TARGET)
-
-rm -f $(TARGETDIR)/$(TARGET)
-
-
veto_mq_server.o: veto_mq_server.c
-
$(CC) -g -c -o $(CC_TMP)/veto_mq_server.o veto_mq_server.c $(CPPFLAGS) $(INCLUDES)
-
-
veto_mq_utils.o: veto_mq_utils.c
-
$(CC) -g -c -o $(CC_TMP)/veto_mq_utils.o veto_mq_utils.c $(INCLUDES)
Running
-
bash-3.2# ./veto_mq_server
-
-
-
bash-3.2# ps -ef | grep mq | grep -v 'grep'
-
0 3616 1 0 10:00AM ttys000 0:00.01 ./veto_mq_server
-
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
阅读(2154) | 评论(0) | 转发(0) |