Rust
Rust
Send a 4-byte Length Prefix over a Socket
See more Socket/SSL/TLS Examples
Demonstrates Socket.SendCount, which sends a four-byte length prefix in network byte order. This is typically used to tell the peer how many bytes follow.
Background. A length-prefixed framing scheme sends the byte count of a message before the message body, so the receiver knows exactly how many bytes to read with a counted receive.
Chilkat Rust Downloads
let socket = chilkat::Socket::new();
// Connect to the server using TLS.
let b_tls = true;
let max_wait_ms = 5000;
if socket.connect("example.com", 5000, b_tls, max_wait_ms).is_err() {
println!("{}", socket.last_error_text());
return;
}
// Send a four-byte length prefix in network byte order. A common framing pattern is to send the
// number of bytes in a message before the message itself, so the peer knows exactly how many bytes
// to read.
let message_length = 512;
if socket.send_count(message_length).is_err() {
println!("{}", socket.last_error_text());
return;
}
println!("Length prefix sent.");