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

Accept Connection on Socket

See more Socket/SSL/TLS Examples

Demonstrates how to create a TCP/IP socket, listen on a port, accept an incoming connection, and send a "Hello World" message to the client.

Chilkat Rust Downloads

Rust

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

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

// Bind to a port and listen for incoming connections:
// This example will listen at port 5555 and allows for a backlog
// of 25 pending connection requests.
if listen_socket.bind_and_listen(5555, 25).is_err() {
    println!("{}", listen_socket.last_error_text());
    return;
}

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

// Set maximum timeouts for reading an writing (in millisec)
connected_socket.set_max_read_idle_ms(10000);
connected_socket.set_max_send_idle_ms(10000);

// Send a "Hello World!" message to the client:
if connected_socket.send_string("Hello World!").is_err() {
    println!("{}", connected_socket.last_error_text());
    return;
}

// Close the connection with the client.
// Wait a max of 20 seconds (20000 millsec)
let _ = connected_socket.close(20000).is_ok();

println!("success!");