示例#1
0
  public static void main(String[] args) {
    String bindTo;
    int roundtripCount;
    int messageSize;
    Ctx ctx;
    SocketBase s;
    boolean rc;
    int n;
    int i;
    Msg msg;

    if (args.length != 3) {
      printf("usage: local_lat <bind-to> <message-size> " + "<roundtrip-count>\n");
      return;
    }
    bindTo = args[0];
    messageSize = atoi(args[1]);
    roundtripCount = atoi(args[2]);

    ctx = ZMQ.zmqInit(1);
    if (ctx == null) {
      printf("error in zmqInit: %s\n");
      return;
    }

    s = ZMQ.zmq_socket(ctx, ZMQ.ZMQ_REP);
    if (s == null) {
      printf("error in zmq_socket: %s\n", ZMQ.zmq_strerror(s.errno()));
      return;
    }

    rc = ZMQ.zmq_bind(s, bindTo);
    if (!rc) {
      printf("error in zmq_bind: %s\n", ZMQ.zmq_strerror(s.errno()));
      return;
    }

    for (i = 0; i != roundtripCount; i++) {
      msg = ZMQ.zmq_recvmsg(s, 0);
      if (msg == null) {
        printf("error in zmq_recvmsg: %s\n", ZMQ.zmq_strerror(s.errno()));
        return;
      }
      if (ZMQ.zmq_msg_size(msg) != messageSize) {
        printf("message of incorrect size received\n");
        return;
      }
      n = ZMQ.zmq_sendmsg(s, msg, 0);
      if (n < 0) {
        printf("error in zmq_sendmsg: %s\n", ZMQ.zmq_strerror(s.errno()));
        return;
      }
    }

    ZMQ.zmq_sleep(1);

    ZMQ.zmq_close(s);

    ZMQ.zmq_term(ctx);
  }