Rust
Rust
TCP Socket Send Byte and Receive Byte
Demonstrates the Chilkat Socket ReceiveByte and SendByte method.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let sock = chilkat::Socket::new();
// --------------------------------------------------------------------
// This example uses the public TCP echo service at https://tcpbin.com/
// --------------------------------------------------------------------
let use_tls = false;
let port = 4242;
let max_wait_ms = 5000;
if sock.connect("tcpbin.com", port, use_tls, max_wait_ms).is_err() {
println!("{}", sock.last_error_text());
return;
}
// Wait a max of 2 seconds for a response..
sock.set_max_read_idle_ms(2000);
// Send a byte.
let _ = sock.send_byte(96);
// The tcpbin.com echo server only echoes after receiving an LF (linefeed char)
let _ = sock.send_byte(10);
// The echo server will echo back whatever is sent to it.
// We should be able to read the same byte back..
// After successfully reading, the byte value is available in the ReceivedInt property.
let treat_as_unsigned_int = true;
if sock.receive_byte(treat_as_unsigned_int).is_err() {
println!("{}", sock.last_error_text());
return;
}
// Let's look at the value of the byte received. It should be 96.
println!("{}", sock.received_int());
// The echo server also echoed the LF back.
let _ = sock.receive_byte(treat_as_unsigned_int).is_ok();
// Assuming success..
// Should be decimal 10.
println!("{}", sock.received_int());