View Javadoc
1   package org.sim0mq.demo;
2   
3   import org.djutils.serialization.SerializationException;
4   import org.sim0mq.Sim0MQException;
5   import org.sim0mq.message.MessageStatus;
6   import org.sim0mq.message.SimulationMessage;
7   import org.zeromq.ZContext;
8   import org.zeromq.ZMQ;
9   
10  /**
11   * Client example for JeroMQ / ZeroMQ.
12   * <p>
13   * (c) copyright 2002-2016 <a href="http://www.simulation.tudelft.nl">Delft University of Technology</a>. <br>
14   * BSD-style license. See <a href="http://www.simulation.tudelft.nl/dsol/3.0/license.html">DSOL License</a>. <br>
15   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
16   * @version Oct 21, 2016
17   */
18  public final class Client
19  {
20      /** */
21      private Client()
22      {
23          // Utility clss
24      }
25      
26      /**
27       * @param args command line arguments
28       * @throws Sim0MQException on error
29       * @throws SerializationException on serialization problem
30       */
31      public static void main(final String[] args) throws Sim0MQException, SerializationException
32      {
33          ZContext context = new ZContext(1);
34  
35          // Socket to talk to server
36          System.out.println("Connecting to server...");
37  
38          ZMQ.Socket requester = context.createSocket(ZMQ.REQ);
39          requester.connect("tcp://localhost:5556");
40          // requester.connect("tcp://131.180.98.169:5556");
41          // requester.connect("tcp://130.161.3.179:5556");
42  
43          // send a reply
44          Object[] request = new Object[] { "test message", new Double(14.2), new Float(-28.4), new Short((short) 10) };
45          requester.send(SimulationMessage.encodeUTF8("IDVV14.2", "MC.1", "MM1.4", "TEST.2", 1201L, MessageStatus.NEW, request),
46                  0);
47  
48          byte[] reply = requester.recv(0);
49          Object[] replyMessage = SimulationMessage.decode(reply);
50          System.out.println("Received\n" + SimulationMessage.print(replyMessage));
51  
52          requester.close();
53          context.destroy();
54          context.close();
55      }
56  
57  }