SHOW:
|
|
- or go back to the newest paste.
| 1 | // Client: | |
| 2 | ||
| 3 | import org.zeromq.ZMQ; | |
| 4 | ||
| 5 | import java.util.concurrent.TimeUnit; | |
| 6 | ||
| 7 | import static org.zeromq.ZMQ.DEALER; | |
| 8 | import static org.zeromq.ZMQ.SNDMORE; | |
| 9 | ||
| 10 | public class Client {
| |
| 11 | - | ZMQ.Socket client = ctx.socket(ZMQ.REQ); |
| 11 | + | |
| 12 | public static void main(String[] args) throws InterruptedException {
| |
| 13 | ZMQ.Context ctx = ZMQ.context(1); | |
| 14 | ||
| 15 | ZMQ.Socket client = ctx.socket(DEALER); | |
| 16 | client.connect("tcp://localhost:222");
| |
| 17 | ||
| 18 | long i = 0; | |
| 19 | while (true) {
| |
| 20 | // send(). | |
| 21 | assert client.send("", SNDMORE);
| |
| 22 | assert client.send(("hello" + i), 0);
| |
| 23 | ||
| 24 | // recv(). | |
| 25 | byte[] world = client.recv(); | |
| 26 | assert world != null; | |
| 27 | ||
| 28 | i++; | |
| 29 | TimeUnit.SECONDS.sleep(1); | |
| 30 | } | |
| 31 | } | |
| 32 | } | |
| 33 | ||
| 34 | // Server: | |
| 35 | - | ZMQ.Socket server = ctx.socket(ZMQ.REP); |
| 35 | + | |
| 36 | package org.zeromq; | |
| 37 | ||
| 38 | import static org.zeromq.ZMQ.ROUTER; | |
| 39 | import static org.zeromq.ZMQ.SNDMORE; | |
| 40 | ||
| 41 | public class Server {
| |
| 42 | ||
| 43 | public static void main(String[] args) throws InterruptedException {
| |
| 44 | ZMQ.Context ctx = ZMQ.context(1); | |
| 45 | ||
| 46 | ZMQ.Socket server = ctx.socket(ROUTER); | |
| 47 | server.bind("tcp://*:222");
| |
| 48 | ||
| 49 | while (true) {
| |
| 50 | // recv(). | |
| 51 | byte[] identity = server.recv(); | |
| 52 | assert identity != null; | |
| 53 | ||
| 54 | byte[] div = server.recv(); | |
| 55 | assert div != null; | |
| 56 | ||
| 57 | byte[] hello = server.recv(); | |
| 58 | assert hello != null; | |
| 59 | ||
| 60 | // send(). | |
| 61 | assert server.send(identity, SNDMORE); | |
| 62 | assert server.send("world", 0);
| |
| 63 | ||
| 64 | System.out.println("hello/world complete.");
| |
| 65 | } | |
| 66 | } | |
| 67 | } |