1

I'd like to use nanomsg as bus-system. So I tried to code a performance test and testing it by using two PCs.

At first I wrote an server, which connects to the other server:

#include <nanomsg/nn.h>
#include <nanomsg/bus.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>

int main(int argc, char *argv[]) {
  if (argc == 2) {
    int socket = nn_socket (AF_SP_RAW, NN_BUS);
    assert(nn_bind (socket, "tcp://*:27384") != -1);
    assert(nn_connect (socket, argv[1]) != -1);

    assert(nn_device(socket, -1) != -1);

    nn_close(socket);
  }
}

In my case I run these commands as:

On the first PC:
./server tcp://192.168.1.11:27384

On the second PC:
./server tcp://192.168.1.241:27384

They are connected, to prove it, I used nanocat and connected it locally to the server:

On the first PC:
nanocat --bus --connect tcp://127.0.0.1:27384 --data foo --interval 1 --ascii

On the second PC:
nanocat --bus --connect tcp://127.0.0.1:27384 --data bar --interval 1 --ascii

On the first PC, I received a 'bar' every second, on the second PC a 'foo', also every second.

So I wrote now the receiver.

#include <nanomsg/nn.h>
#include <nanomsg/bus.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>

int main(int argc, char *argv[]) {
  int socket = nn_socket (AF_SP, NN_BUS);
  assert(nn_connect (socket, "tcp://127.0.0.1:27384") != -1);

  sleep(1);

  unsigned char buffer[4096];

  while(1) {
    int n = nn_recv(socket, buffer, 4096, 0);

    if (n > 0) {
      nn_send(socket, buffer, strlen(buffer), 0);
    }
  }

  nn_close(socket);
}

It receives a message and sends it back.

Then I wrote the sender:

#include <nanomsg/nn.h>
#include <nanomsg/bus.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <string.h>

#define NANO_PER_SEC 1000000000.0

int main(int argc, char *argv[]) {
  int socket = nn_socket (AF_SP, NN_BUS);
  nn_connect (socket, "tcp://127.0.0.1:27384");

  sleep(1);

  unsigned char buffer[4096];

  int i = 0;
  for (i = 0; i < 1024; i++) {
    buffer[i] = 'a';
  }
  buffer[i] = '\0';

  struct timespec start, end;
  double start_sec, end_sec, elapsed_sec;
  double average;

  double m[4096];

  for (i = 0; i < 4096; i++) {
    clock_gettime(CLOCK_REALTIME, &start);

    int ns = nn_send(socket, buffer, strlen(buffer), 0);
    int nr = nn_recv(socket, buffer, 4096, 0);

    clock_gettime(CLOCK_REALTIME, &end);
    start_sec = start.tv_sec + start.tv_nsec/NANO_PER_SEC;
    end_sec = end.tv_sec + end.tv_nsec/NANO_PER_SEC;
    m[i] = end_sec - start_sec;
  }

  elapsed_sec = 0.0;
  for (i = 0; i < 4096; i++) {
    elapsed_sec = elapsed_sec + m[i];
  }
  average = (elapsed_sec / 4096.0) * 1000000.0;
  printf("Average: %.3f micros\nWhole: %.12f seconds\n", average, elapsed_sec);

  nn_close(socket);
}

The sender transmits 4096 times 1kbyte to the receiver and measures the time, so I get the whole time and the average time.

At first I test it at only one PC locally, in three opened bash-terminals.

First terminal:
./server tcp://192.168.1.11:27384
Second terminal:
./receiver

Third terminal:
./sender

So, I got from the "sender" programme this output:

Average: 60.386 micros
Whole: 0.247341632843 seconds

Then I tried to run this:

On first PC:
./server tcp://192.168.1.11:27384
./receiver

On second PC:
./server tcp://192.168.1.241:27384
./sender

But it stucks, the first PC running the "receiver" doesn't receive any message from the second PC, which runs the "sender". I don't get it whats wrong, because with nanocat it works fine.

Can somebody please help me?

user3666197
  • 1
  • 6
  • 50
  • 92
clausismus
  • 165
  • 1
  • 9
  • 3
    `nn_send(socket, buffer, strlen(buffer), 0);` -->> `nn_send(socket, buffer, n, 0);` dont use strlen() on a not-null-terminated buffer. – wildplasser Feb 04 '17 at 13:59
  • 1
    But I terminated the buffer, see: `buffer[i] = '\0';` – clausismus Feb 04 '17 at 14:04
  • 1
    In the sender, not in the receiver. – wildplasser Feb 04 '17 at 14:16
  • 1
    Thx! It works. A really poor mistake. But I don't get it, why it works on my first PC locally. (i5 processor, Ubuntu 16.04, gcc). – clausismus Feb 04 '17 at 14:31
  • 1
    After `int n = nn_recv(socket, buffer, 4096, 0);`, the unused buffer is indeterminate because `buffer` is just a local variables, it can be 0 or anything. If it's 0 on your pc, then it works. – jfly Feb 04 '17 at 15:41

0 Answers0