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