Advertisement
Guest User

Untitled

a guest
May 23rd, 2011
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.14 KB | None | 0 0
  1. import org.zeromq.ZMQ;
  2.  
  3. public class SubTest
  4. {
  5.   public static void main(String[] args)
  6.   {
  7.     ZMQ.Context context = ZMQ.context(1);
  8.  
  9.     ZMQ.Socket subSocket = context.socket(ZMQ.SUB);
  10.     subSocket.subscribe("".getBytes());
  11.     subSocket.setHWM(10);
  12.     subSocket.connect("tcp://127.0.0.1:5555");
  13.  
  14.     int expectedMsgId = 1;
  15.  
  16.     while (true)
  17.     {
  18.       String msg = new String(subSocket.recv(0));
  19.  
  20.       int rxMsgId = Integer.parseInt(msg);
  21.  
  22.       if (rxMsgId != expectedMsgId)
  23.       {
  24.         System.out.printf("Expected %d, but received %d\n", expectedMsgId, rxMsgId);
  25.       }
  26.       else
  27.       {
  28.         System.out.printf("Received expected message: %d\n", rxMsgId);
  29.       }
  30.  
  31.       expectedMsgId = rxMsgId + 1;
  32.     }
  33.   }
  34. }
  35.  
  36.  
  37. import org.zeromq.ZMQ;
  38.  
  39. public class PubTest
  40. {
  41.   public static void main(String[] args)
  42.   {
  43.     ZMQ.Context context = ZMQ.context(1);
  44.     ZMQ.Socket pubSocket = context.socket(ZMQ.PUB);
  45.     pubSocket.setHWM(10);
  46.     pubSocket.bind("tcp://*:5555");
  47.  
  48.     int msgId = 0;
  49.  
  50.     while (true)
  51.     {
  52.       String msg = (++msgId) + "";
  53.  
  54.       pubSocket.send(msg.getBytes(), 0);
  55.     }
  56.   }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement