1 package org.sim0mq.test;
2
3 import java.util.Arrays;
4
5 import org.sim0mq.Sim0MQException;
6 import org.zeromq.ZContext;
7 import org.zeromq.ZMQ;
8
9
10
11
12
13
14
15
16
17 public final class TicPush
18 {
19
20 private TicPush()
21 {
22
23 }
24
25
26
27
28
29 public static void main(final String[] args) throws Sim0MQException
30 {
31 long time = System.currentTimeMillis();
32 try (ZContext context = new ZContext(1))
33 {
34
35 System.out.println("Connecting to server on port 5556...");
36
37 ZMQ.Socket requester = context.createSocket(ZMQ.REQ);
38 requester.connect("tcp://localhost:5556");
39
40 for (int i = 0; i < 100000; i++)
41 {
42
43 byte[] message = string2byte("TIC");
44 requester.send(message, 0);
45
46
47 byte[] reply = requester.recv(0);
48 String rs = byte2string(reply);
49 if (!rs.equals("TOC"))
50 {
51 System.err.println("Answer was not TOC");
52 }
53 }
54
55
56 byte[] message = string2byte("STOP");
57 requester.send(message, 0);
58
59 requester.close();
60 context.destroy();
61 }
62 System.out.println("RUNTIME = " + (System.currentTimeMillis() - time) + " ms");
63 }
64
65
66
67
68
69
70 public static byte[] string2byte(final String s)
71 {
72 byte[] b = s.getBytes();
73 return Arrays.copyOf(b, b.length + 1);
74 }
75
76
77
78
79
80
81 public static String byte2string(final byte[] b)
82 {
83 return new String(Arrays.copyOf(b, b.length - 1));
84 }
85
86 }