Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'master' of github.com:zeromq/issues

  • Loading branch information...
commit ba01930a1e0bd6f6c6d1414e5829dfce3116530d 2 parents 231d935 + 4cc3826
Pieter Hintjens hintjens authored

Showing 2 changed files with 125 additions and 0 deletions. Show diff stats Hide diff stats

  1. +54 0 235/2-1/client.c
  2. +71 0 235/2-1/server.c
54 235/2-1/client.c
... ... @@ -0,0 +1,54 @@
  1 +//
  2 +// PUSH fails to load balance on windows
  3 +// Binds PUSH socket to tcp://localhost:5557
  4 +//
  5 +// gcc -lzmq testcase.c
  6 +// chmod +x a.out
  7 +// ./a.out
  8 +//
  9 +#include <zmq.h>
  10 +
  11 +// several of these are probably unnecessary...
  12 +#include <stdint.h>
  13 +#include <stdio.h>
  14 +#include <stdlib.h>
  15 +#include <stdarg.h>
  16 +#include <string.h>
  17 +#include <sys/time.h>
  18 +#include <time.h>
  19 +#include <unistd.h>
  20 +#include <assert.h>
  21 +#include <inttypes.h>
  22 +
  23 +
  24 +
  25 +int main (void)
  26 +{
  27 + void *context = zmq_init (1);
  28 + void *worker = zmq_socket (context, ZMQ_PULL);
  29 + int linger = 0;
  30 + zmq_setsockopt (worker, ZMQ_LINGER, &linger, sizeof (linger));
  31 + zmq_connect (worker, "tcp://127.0.0.1:5557");
  32 + printf("worker has started\n");
  33 +
  34 + int total = 0;
  35 + int rc = 0;
  36 + while (1) {
  37 + zmq_msg_t message;
  38 + zmq_msg_init (&message);
  39 +
  40 + // wait for a message
  41 + //if ((rc = zmq_recv (worker, &message, ZMQ_NOBLOCK)) == 0) {
  42 + if ((rc = zmq_recv (worker, &message, 0)) == 0) {
  43 + // print message
  44 + //printf("worker got message\n");
  45 + }
  46 + // else {
  47 + // printf("worker, rc != 0, [%d]\n", rc);
  48 + // }
  49 + zmq_msg_close (&message);
  50 + }
  51 + zmq_close (worker);
  52 + zmq_term (context);
  53 + return (0);
  54 +}
71 235/2-1/server.c
... ... @@ -0,0 +1,71 @@
  1 +//
  2 +// PUSH fails to load balance on windows
  3 +// Binds PUSH socket to tcp://localhost:5557
  4 +//
  5 +// gcc -lzmq server.c -o server
  6 +// chmod +x server
  7 +// ./server
  8 +//
  9 +// on Windows, compile with:
  10 +// gcc -I<path to include files> -L<path to libzmq.dll> -lzmq server.c -o winserver
  11 +//
  12 +#include <zmq.h>
  13 +
  14 +// several of these are probably unnecessary...
  15 +#include <stdint.h>
  16 +#include <stdio.h>
  17 +#include <stdlib.h>
  18 +#include <stdarg.h>
  19 +#include <string.h>
  20 +#include <sys/time.h>
  21 +#include <time.h>
  22 +#include <unistd.h>
  23 +#include <assert.h>
  24 +#include <inttypes.h>
  25 +
  26 +
  27 +int main (void)
  28 +{
  29 + void *context = zmq_init (1);
  30 +
  31 + // Socket to send messages on
  32 + void *sender = zmq_socket (context, ZMQ_PUSH);
  33 + uint64_t hwm = 1;
  34 + //zmq_setsockopt (sender, ZMQ_HWM, &hwm, sizeof (hwm));
  35 + int linger = 0;
  36 + zmq_setsockopt (sender, ZMQ_LINGER, &linger, sizeof (linger));
  37 + zmq_bind (sender, "tcp://127.0.0.1:5557");
  38 +
  39 + // Wait for threads to connect, since otherwise the messages
  40 + // we send will be dropped
  41 + printf ("Press Enter when the workers are ready: ");
  42 + getchar ();
  43 + printf ("Sending tasks to workers...\n");
  44 +
  45 +
  46 + int rc = 0;
  47 + int i;
  48 + int successful = 0;
  49 + int eagain = 0;
  50 +
  51 + for(i = 0; i < 1000000; i++) {
  52 + zmq_msg_t task;
  53 + zmq_msg_init_data (&task, "Hello world", 11, NULL, NULL);
  54 + rc = zmq_send (sender, &task, ZMQ_NOBLOCK);
  55 +
  56 + if (rc == 0) {
  57 + successful++;
  58 + }
  59 + else {
  60 + eagain++;
  61 + }
  62 +
  63 + zmq_msg_close (&task);
  64 + }
  65 +
  66 + printf("Successfully sent [%d] messages\n", successful);
  67 + printf("EAGAIN received for [%d] messages\n", eagain);
  68 + zmq_close (sender);
  69 + zmq_term (context);
  70 + return 0;
  71 +}

0 comments on commit ba01930

Please sign in to comment.
Something went wrong with that request. Please try again.