13 #include <nanomsg/nn.h> 14 #include <nanomsg/pubsub.h> 17 #define SERVER "server" 18 #define CLIENT "client" 23 time_t raw = time (&raw);
24 struct tm *info = localtime (&raw);
25 char *text = asctime (info);
26 text[strlen(text)-1] =
'\0';
32 int sock = nn_socket (AF_SP, NN_PUB);
35 result = nn_bind (sock,
URL);
37 std::cout <<
"Asserted bind on " <<
URL << std::endl;
41 int sz_d = strlen(d) + 1;
42 printf (
"SERVER: PUBLISHING DATE %s\n", d);
43 int bytes = nn_send (sock, d, sz_d, 0);
44 assert (bytes == sz_d);
47 return nn_shutdown (sock, 0);
52 int sock = nn_socket (AF_SP, NN_SUB);
53 std::cout <<
"Socket: " << sock << std::endl;
57 result = nn_setsockopt (sock, NN_SUB, NN_SUB_SUBSCRIBE,
"", 0);
59 std::cout <<
"Asserted nn_setsockopt" << std::endl;
60 result = nn_connect (sock,
URL);
62 std::cout <<
"Asserted nn_connect on " <<
URL << std::endl;
63 std::cout <<
"Receiving"<< std::endl;
67 int bytes = nn_recv (sock, &buf, NN_MSG, 0);
69 printf (
"CLIENT: RECEIVED %s\n", buf);
72 return nn_shutdown (sock, 0);
75 int main (
const int argc,
const char **argv)
78 fprintf (stderr,
"Usage: pubsub %s|%s \n",
SERVER,
CLIENT);
83 else if (strncmp (
CLIENT, argv[1], strlen (
CLIENT)) == 0)
int main(const int argc, const char **argv)