Hello ZeroMQ World !
安装
git clone --depth=1 https://github.com/imatix/zguide.git
./configure
make
make install
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)
Hello World Demo
/*
============================================================================
Name : veto_mq_server.c
Author : yanrui
Version :
Copyright : 2015-2050
Description : Hello World in C, Ansi-style
============================================================================
*/
#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;
}
/*
============================================================================
Name : veto_mq_client.c
Author : yanrui
Version :
Copyright : 2015-2050
Description : Hello World in C, Ansi-style
============================================================================
*/
#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;
}