Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.0.0+

Transfer a File using Sockets (TLS or non-TLS)

See more Socket/SSL/TLS Examples

Demonstrates how to two programs, one a socket writer and the other a socket reader, can transfer a file. The connection can be TLS or a regular non-encrypted TCP connection.

Chilkat Rust Downloads

Rust

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// On the sending side, we'll load the file into a BinData object and send.
// On the receiving side, we'll read from the socket connection into a BinData, and save to a file.
// This example assumes the file is not crazy-large, and that the entire contents
// can fit into memory.  
// (If the file is too large for memory, there are other ways to send. It just involves streaming or 
// sending the file chunk-by-chunk..)

// This section of code is for the sender.
let bd_to_send = chilkat::BinData::new();
let _ = bd_to_send.load_file("somePath/someFile.dat").is_ok();
// Assume success for the example...

let snd_sock = chilkat::Socket::new();
let b_use_tls = true;
let port = 5555;
let max_wait_ms = 5000;
let _ = snd_sock.connect("some_domain_or_ip.com", port, b_use_tls, max_wait_ms).is_ok();
// Assume success for the example...

// Tell the receiver how many bytes are coming.
let num_bytes = bd_to_send.num_bytes();
let b_big_endian = true;
let _ = snd_sock.send_int32(num_bytes, b_big_endian).is_ok();

// Send the file data (sends the entire contents of bdToSend).
let _ = snd_sock.send_bd(&bd_to_send, 0, 0).is_ok();

// Get an acknowledgement.
if snd_sock.receive_int32(b_big_endian).is_err() {
    println!("{}", snd_sock.last_error_text());
    return;
}

// Did the receiver get the correct number of bytes?
if snd_sock.received_int() != num_bytes {
    println!("The receiver did not acknowledge with the correct number of bytes.");
    return;
}

println!("File sent!");

// ------------------------------------------------------------------------------------
// The code below is for the receiving side (running on some other computer..)

let listen_sock = chilkat::Socket::new();

if listen_sock.bind_and_listen(5555, 25).is_err() {
    println!("{}", listen_sock.last_error_text());
    return;
}

// Get the next incoming connection
// Wait a maximum of 20 seconds (20000 millisec)
let rcv_sock = chilkat::Socket::new();
if listen_sock.accept_next(20000, &rcv_sock).is_err() {
    println!("{}", listen_sock.last_error_text());
    return;
}

// The sender will first send the big-endian integer for the number of bytes
// that are forthcoming..
if rcv_sock.receive_int32(b_big_endian).is_err() {
    println!("{}", rcv_sock.last_error_text());
    return;
}

let num_bytes_coming = rcv_sock.received_int();

// Receive that many bytes..
let bd_received = chilkat::BinData::new();
if rcv_sock.receive_bd_n(num_bytes_coming as u32, &bd_received).is_err() {
    println!("{}", rcv_sock.last_error_text());
    return;
}

// Acknowledge the sender by sending back the number of bytes we received.
let _ = rcv_sock.send_int32(bd_received.num_bytes(), b_big_endian).is_ok();

// Close the connection.
let max_wait_ms = 20;
let _ = rcv_sock.close(max_wait_ms);

// Save the received data to a file.
let _ = bd_received.write_file("somePath/someFile.dat").is_ok();
// Assume success for the example...

println!("File received!");