mu5e
mu5e

Reputation: 125

Rust, libzmq, client only send message, when followed by receive

I am testing Rust libzmq client

https://crates.io/crates/libzmq

https://docs.rs/libzmq/0.2.5/libzmq/struct.ClientBuilder.html

and stumbled upon this weird behavior.

This Client would not send the message:

use libzmq::{prelude::*, *, ServerBuilder, ClientBuilder, TcpAddr};

fn main() -> Result<(), String> {
    
    let saddr = format!("{}:{}", "192.168.1.206","5540");
    println!("Strating client for {}", saddr);
    let addr2: TcpAddr = saddr.try_into().unwrap();
    
    let client = ClientBuilder::new()
        .connect(&addr2)
        .build().unwrap();
    client.send("test").unwrap();

    println!("Finished");
    Ok(())
}

this would send:

use libzmq::{prelude::*, *, ServerBuilder, ClientBuilder, TcpAddr};

fn main() -> Result<(), String> {
    
    let saddr = format!("{}:{}", "192.168.1.206","5540");
    println!("Strating client for {}", saddr);
    let addr2: TcpAddr = saddr.try_into().unwrap();
    
    let client = ClientBuilder::new()
        .connect(&addr2)
        .build().unwrap();
    client.send("test").unwrap();

    let mut msg = Msg::new();
    client.recv(&mut msg).unwrap();

    println!("Finished");
    Ok(())
}

For receiving I am using python server (but I tested on Rust as well):

import zmq
if __name__ == '__main__':

    context = zmq.Context()
    socket = context.socket(zmq.SERVER)
    socket.bind("tcp://0.0.0.0:5540")
    print("waiting for hand shake")
    m = socket.recv(copy=False)
    print("got it...")
    socket.send(b'READY', routing_id=m.routing_id)
    print("sent reply")
    socket.close()
    context.term()

Output on server side from code 1:

waiting for hand shake
[blocked]

Output on server side from code 2:

waiting for hand shake
got it...
sent reply

using this version for Rust:

[dependencies]
libzmq = "0.2.5"

Upvotes: 2

Views: 476

Answers (1)

Jmb
Jmb

Reputation: 23463

The libzmq crate is just a wrapper for the libzmq C library. In the documentation for the C library, you will find this note:

NOTE: A successful invocation of zmq_msg_send() does not indicate that the message has been transmitted to the network, only that it has been queued on the 'socket' and 0MQ has assumed responsibility for the message.

In your first example, since the program exits immediately after calling send, the message is still in the libzmq queue and has not been transmitted yet. In your second example, the message gets transmitted while your program is waiting in the recv call.

Upvotes: 2

Related Questions