erickt / rust-zmq

Rust zeromq bindings.
Apache License 2.0
886 stars 189 forks source link

Received empty string instead of sent string when using zmq::STREAM #323

Open Riemannstein opened 3 years ago

Riemannstein commented 3 years ago

I understand this might be an elementary question. But I kind of got stuck here. Any help is greatly appreciated.

I use the following examples to send (send.rs) and receive (receive.rs, which is the same as the official example logserver.rs) messages:

Sender:


fn main() {
    let ctx = zmq::Context::new();
    let socket = ctx.socket(zmq::STREAM).unwrap();

    loop {

        socket.connect("tcp://127.0.0.1:1234").unwrap();
        let msg = "abc\n";
        socket.send(zmq::Message::from(&msg), 0).unwrap();
        println!("Message sent: {:?}",msg);
        let ten_millis = time::Duration::from_millis(1000);
        thread::sleep(ten_millis);
    }

}

examples.zip

The sender sends messages: image

But the receiver does not receive the desired "abc\n" supposedly sent by the sender:

image

Thanks in advance for your time.

birkenfeld commented 3 years ago

IIRC there is an empty frame in the multipart message between identity and the actual message frames?

Riemannstein commented 3 years ago

IIRC there is an empty frame in the multipart message between identity and the actual message frames?

Thanks for the hint. Does that mean I am simply not printing the message correctly? This is currently how I print it. What changes should I make ?

        let data = socket.recv_multipart(0).unwrap();
        println!(
            "Identity: {:?} Message : {}",
            data[0],
            str::from_utf8(&data[1]).unwrap()
        );
birkenfeld commented 3 years ago

Try data[2].

Riemannstein commented 3 years ago

Try data[2].

Seems the length is 2. The exception returned:

thread 'main' panicked at 'index out of bounds: the len is 2 but the index is 2', examples/zmq/receive.rs:15:29

This is the data received using python script receiving data: image

birkenfeld commented 3 years ago

OK, then that's not the case. However, if you can reproduce in Python, it's not a rust-zmq problem...

Riemannstein commented 3 years ago

OK, then that's not the case. However, if you can reproduce in Python, it's not a rust-zmq problem...

I would say it is not the problem on the receiving end. The sending end is still through rust-zmq. But again, it could be an elementary question. Any help is greatly appreciated.

birkenfeld commented 3 years ago

So wird the sender in Python it works?

abdelmonem17 commented 3 years ago

@birkenfeld unfortunately I faced the same bug which the receiver receives empty string. can help?