Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Client:
- import org.zeromq.ZMQ;
- import java.util.concurrent.TimeUnit;
- import static org.zeromq.ZMQ.DEALER;
- import static org.zeromq.ZMQ.SNDMORE;
- public class Client {
- public static void main(String[] args) throws InterruptedException {
- ZMQ.Context ctx = ZMQ.context(1);
- ZMQ.Socket client = ctx.socket(DEALER);
- client.connect("tcp://localhost:222");
- long i = 0;
- while (true) {
- // send().
- assert client.send("", SNDMORE);
- assert client.send(("hello" + i), 0);
- // recv().
- byte[] world = client.recv();
- assert world != null;
- i++;
- TimeUnit.SECONDS.sleep(1);
- }
- }
- }
- // Server:
- package org.zeromq;
- import static org.zeromq.ZMQ.ROUTER;
- import static org.zeromq.ZMQ.SNDMORE;
- public class Server {
- public static void main(String[] args) throws InterruptedException {
- ZMQ.Context ctx = ZMQ.context(1);
- ZMQ.Socket server = ctx.socket(ROUTER);
- server.bind("tcp://*:222");
- while (true) {
- // recv().
- byte[] identity = server.recv();
- assert identity != null;
- byte[] div = server.recv();
- assert div != null;
- byte[] hello = server.recv();
- assert hello != null;
- // send().
- assert server.send(identity, SNDMORE);
- assert server.send("world", 0);
- System.out.println("hello/world complete.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment