Sample code for 30+ languages & platforms
Rust

Receive a 4-byte Length Prefix from a Socket

See more Socket/SSL/TLS Examples

Demonstrates Socket.ReceiveCount, which reads a four-byte length prefix as a signed 32-bit integer. A return value of -1 indicates failure.

Background. Reading the length prefix first tells the application exactly how many bytes to read for the message body, which is a reliable way to know when a full message has arrived.

Chilkat Rust Downloads

Rust

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;
}

// Read a four-byte length prefix as a signed 32-bit integer (network byte order by default).  A
// return value of -1 indicates failure.
let message_length = socket.receive_count();
if message_length < 0 {
    println!("{}", socket.last_error_text());
    return;
}

println!("The peer will send {} bytes.", message_length);