pub struct Client { /* private fields */ }
Expand description
A Client
socket is used for advanced request-reply messaging.
Client
sockets are threadsafe and can be used from multiple threads at the
same time. Note that replies from a Server
socket will go to the first
client thread that calls recv
. If you need to get replies back to the
originating thread, use one Client
socket per thread.
When a Client
socket is connected to multiple sockets, outgoing
messages are distributed between connected peers on a round-robin basis.
Likewise, the Client
socket receives messages fairly from each connected peer.
§Mute State
When Client
socket enters the mute state due to having reached the high water
mark, or if there are no peers at all, then any send operations on the
socket shall block until the mute state ends or at least one peer becomes
available for sending; messages are not discarded.
§Summary of Characteristics
Characteristic | Value |
---|---|
Compatible peer sockets | Server |
Direction | Bidirectional |
Send/receive pattern | Unrestricted |
Outgoing routing strategy | Round-robin |
Incoming routing strategy | Fair-queued |
Action in mute state | Block |
§Example
use libzmq::{prelude::*, *};
// Use a system assigned port.
let addr: TcpAddr = "127.0.0.1:*".try_into()?;
let server = ServerBuilder::new()
.bind(addr)
.build()?;
// Retrieve the addr that was assigned.
let bound = server.last_endpoint()?;
let client = ClientBuilder::new()
.connect(bound)
.build()?;
// Send a string request.
client.send("tell me something")?;
// Receive the client request.
let msg = server.recv_msg()?;
let id = msg.routing_id().unwrap();
// Reply to the client.
server.route("it takes 224 bits to store a i32 in java", id)?;
// We send as much replies as we want.
server.route("also don't talk to me", id)?;
// Retreive the first reply.
let mut msg = client.recv_msg()?;
// And the second.
client.recv(&mut msg)?;