[nanomsg] Re: Messages get lost using the pub/sub model

  • From: 李捷 <lijielile@xxxxxxxxx>
  • To: nanomsg@xxxxxxxxxxxxx
  • Date: Sun, 5 Jun 2016 14:59:03 -0400

Still not work. I tried to make the server sleep for 4 seconds after
sending the first message. The client still blocks for ever.  Below is the
code.

int receiveMSG() {
std::cout << "Start the thread to receive message..." << std::endl;
char *buf = NULL;
int sock = nn_socket(AF_SP, NN_SUB);
nn_setsockopt (sock, NN_SUB, NN_SUB_SUBSCRIBE, "", 0);
std::string addr = "tcp://localhost:7777";
int eid = nn_connect(sock, addr.c_str());
assert (eid >= 0);
int nbytes = 0;
nbytes = nn_recv(sock, &buf, NN_MSG, 0);
assert (nbytes >= 0);
std::cout << "Received msg is: " << buf << std::endl;
nn_freemsg(buf);
return nn_shutdown (sock, 0);
}

int sendMSG() {
std::cout << "Start the thread to send message..." << std::endl;
int sock =  nn_socket(AF_SP, NN_PUB);
std::string addr = "tcp://localhost:7777";
int eid = nn_bind(sock, addr.c_str());
assert (eid >= 0);
size_t nbytes = 0;
const char * msg = "first message";
nbytes = nn_send(sock, msg, strlen(msg) + 1, 0);
std::cout << nbytes << std::endl;
assert (nbytes == strlen(msg) + 1);
sleep(4);
// If I send a second message, the second one will be received immediately.
/* const char* msg2 = "second message";
nbytes = nn_send(sock, msg2, strlen(msg2) + 1, 0);*/
return nn_shutdown (sock, 0);
}

int main() {

std::thread t1(receiveMSG);
sleep(1);
std::thread t2(sendMSG);
t1.join();
t2.join();
return 0;
}

On Sun, Jun 5, 2016 at 2:50 PM, 李捷 <lijielile@xxxxxxxxx> wrote:

Thanks for the reply. Spent a lot of time to debug this and tried to make
the server sleep for 1 second. Did't try to make it sleep for 2
seconds......

On Sun, Jun 5, 2016 at 2:48 PM, Garrett D'Amore <garrett@xxxxxxxxxx>
wrote:

Put another way, when the PUB server “sends” it does not block — it sends
to all pipes that are *connected at that time*, and are not experiencing
flow control.  Any clients not connected won’t see messages that might have
been published earlier.

On Sun, Jun 5, 2016 at 11:47 AM, Garrett D'Amore <garrett@xxxxxxxxxx>
wrote:

The first message the server sends is lost, because the clients haven’t
connected yet when the send takes place.

If you ask the server to wait for > 1 sec (perhaps 2 seconds) before
sending the first message, then that will give the clients a chance to
connect, and it will be fine.

 - Garrett


On Sun, Jun 5, 2016 at 11:40 AM, 李捷 <lijielile@xxxxxxxxx> wrote:

I am planning to use the pub/sub model to broadcast messages from one
thread to multiple threads.  However, after playing around with it I found
when broadcasting messages from the server node to the client nodes, the
first messages can't be received. If I sent two messages from the server,
only the second one is received and if I send three, the last two got
received. If I only send one message, the receiving node block for ever
waiting for the messages. Did I miss anything? Also, by looking at the
sample from the tutorial, it is obvious that messages got lost too.  Below
is copied from the tutorial page. Obviously 6 messages were sent and only 4
were received?

gcc pubsub.c /usr/local/lib/libnanomsg.a -o pubsub./pubsub server 
ipc:///tmp/pubsub.ipc & server=$! && sleep 1./pubsub client 
ipc:///tmp/pubsub.ipc client0 & client0=$!./pubsub client 
ipc:///tmp/pubsub.ipc client1 & client1=$!./pubsub client 
ipc:///tmp/pubsub.ipc client2 & client2=$!sleep 5kill $server $client0 
$client1 $client2

SERVER: PUBLISHING DATE Sat Sep  7 17:40:11 2013
SERVER: PUBLISHING DATE Sat Sep  7 17:40:12 2013
SERVER: PUBLISHING DATE Sat Sep  7 17:40:13 2013
CLIENT (client2): RECEIVED Sat Sep  7 17:40:13 2013
CLIENT (client0): RECEIVED Sat Sep  7 17:40:13 2013
CLIENT (client1): RECEIVED Sat Sep  7 17:40:13 2013
SERVER: PUBLISHING DATE Sat Sep  7 17:40:14 2013
CLIENT (client2): RECEIVED Sat Sep  7 17:40:14 2013
CLIENT (client1): RECEIVED Sat Sep  7 17:40:14 2013
CLIENT (client0): RECEIVED Sat Sep  7 17:40:14 2013
SERVER: PUBLISHING DATE Sat Sep  7 17:40:15 2013
CLIENT (client1): RECEIVED Sat Sep  7 17:40:15 2013
CLIENT (client2): RECEIVED Sat Sep  7 17:40:15 2013
CLIENT (client0): RECEIVED Sat Sep  7 17:40:15 2013
SERVER: PUBLISHING DATE Sat Sep  7 17:40:16 2013
CLIENT (client1): RECEIVED Sat Sep  7 17:40:16 2013
CLIENT (client2): RECEIVED Sat Sep  7 17:40:16 2013
CLIENT (client0): RECEIVED Sat Sep  7 17:40:16 2013





Other related posts: