Http://youzifei.iteye.com/blog/1698237zeromq
It took a great effort to install ZEROMQ today.
Here's a look at the process of installing ZEROMQ on Linux
First of all
http://download.zeromq.org/
1. Download the latest version of ZEROMQ
Http://download.zeromq.org/zeromq-3.1.0-beta.tar.gz
2 Decompression
TAR-XVF zeromq-3.1.0-beta.tar.gz
3 Running Configure
./configure--prefix=/data/zeromq (prefix specified installation directory)
4. Make
5. Make install
6. Setting Environment variables
Export cppflags=-i/home/mine/0mq/include/
Export ldflags=-l/home/mine/0mq/lib/
7. Test code
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.\nwaiting client send message...\n");
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]\n", 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 if we do, this would is 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...\n");
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! =); request_nbr++)
{
zmq_msg_t request;
Zmq_msg_init_data (&request, "Hello", 6, NULL, NULL);
printf ("Sending Request%d...\n", 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]\n", 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 is the Java Client for ZEROMQ
Https://github.com/zeromq/jzmq/downloads
1. Tar xzf zeromq-jzmq-semver-90-g58c6108.tar.gz
2../configure
This time requires the configuration of the above environment variables,
also need
Just
Vi/data/zeromq/.bashrc
Export path= $PATH:/data/zeromq/include
Export Ld_library_path= $LD _library_path:/data/zeromq/lib:/data/zeromq/include
The makefile file will be generated as soon as the Configure is finished executing
3.make
4.make Install
Test
Hwserver.java
//
Hello World Server in Java
Binds REP socket to tcp://*:5555
Expects "Hello" from the client, replies with "World"
//
Naveen Chawla <[email protected]>
//
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'll wait for a 0-terminated string (C string) from the client,
So, 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'll send a 0-terminated string (C string) back to the client,
So, 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 <[email protected]>
//
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 ten requests, waiting each time for a response
for (int request_nbr = 0; Request_nbr! =; request_nbr++) {
Create a "Hello" message.
Ensure 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
Display content:
Server Side
Received request: [Hello]
Received request: [Hello]
Received request: [Hello]
Received request: [Hello]
Received request: [Hello]
Received request: [Hello]
Client side connecting to Hello World server ... Sending request 0...Received reply 0: [world]sending request 1...Received reply 1: [world]sending request 2...Received Rep Ly 2: [world]sending request 3...Received reply 3: [world]sending request 4...Received reply 4: [world]sending request 5]. .
(Turn) ZEROMQ installation