2 #include <nanomsg/nn.h> 3 #include <nanomsg/pair.h> 4 #include <nanomsg/pubsub.h> 5 #include <nanomsg/inproc.h> 10 #define SOCKET_ADDRESS "inproc://test" 16 sock = nn_socket (family, protocol);
18 std::cout <<
"Error opening socket" << std::endl;
26 rc = nn_connect (sock, address);
28 std::cout <<
"Error on connect"<< std::endl;
36 rc = nn_bind (sock, address);
38 std::cout <<
"Error on bind"<< std::endl;
47 data_len = strlen(data);
49 std::cout <<
"Send: " << data << std::endl;
50 rc = nn_send (sock, data, data_len, 0);
52 std::cout <<
"Error on send"<< std::endl;
54 if (rc != (
int)data_len) {
55 std::cout <<
"error on data to send"<< std::endl;
64 data_len = strlen(data);
67 buf = (
char*)malloc(data_len+1);
70 rc = nn_recv (sock, buf, data_len+1, 0);
72 std::cout <<
"Error on receive"<< std::endl;
74 if (rc != (
int)data_len) {
75 std::cout <<
"Error on data wrong length"<< std::endl;
77 if (memcmp (data, buf, data_len) != 0) {
78 std::cout <<
"Error on received data is wrong"<< std::endl;
80 std::cout <<
"Receive: " << buf<< std::endl;
91 std::cout <<
"Error on close"<< std::endl;
114 for (i = 0; i != 100; ++i) {
123 for (i = 0; i != 100; ++i) {
126 for (i = 0; i != 100; ++i) {
136 rc = nn_setsockopt (sb, NN_SOL_SOCKET, NN_RCVBUF, &val,
sizeof (val));
143 rc = nn_setsockopt (sc, NN_SOL_SOCKET, NN_SNDTIMEO, &val,
sizeof (val));
147 rc = nn_send (sc,
"0123456789", 10, 0);
148 if (rc < 0 && nn_errno () == EAGAIN)
157 rc = nn_send (sc,
"0123456789", 10, 0);
159 for (i = 0; i != 20; ++i) {
165 for (i = 0; i !=
sizeof (buf); ++i)
167 rc = nn_send (sc, buf, 256, 0);
170 rc = nn_recv (sb, buf,
sizeof (buf), 0);
190 rc = nn_setsockopt (sub1, NN_SUB, NN_SUB_SUBSCRIBE,
"", 0);
193 rc = nn_getsockopt (sub1, NN_SUB, NN_SUB_SUBSCRIBE, buf, &sz);
197 rc = nn_setsockopt (sub2, NN_SUB, NN_SUB_SUBSCRIBE,
"", 0);
204 test_send (pub1,
"0123456789012345678901234567890123456789");
205 test_recv (sub1,
"0123456789012345678901234567890123456789");
206 test_recv (sub2,
"0123456789012345678901234567890123456789");
215 rc = nn_setsockopt (sub1, NN_SUB, NN_SUB_SUBSCRIBE,
"", 0);
224 test_send (pub1,
"0123456789012345678901234567890123456789");
225 test_send (pub2,
"0123456789012345678901234567890123456789");
226 test_recv (sub1,
"0123456789012345678901234567890123456789");
227 test_recv (sub1,
"0123456789012345678901234567890123456789");
static void test_send(int sock, const char *data)
static int test_bind(int sock, const char *address)
static void test_recv(int sock, const char *data)
static int test_connect(int sock, const char *address)
static int test_socket(int family, int protocol)
static void test_close(int sock)