pub fn connect_timeout<A: ToSocketAddrs>(
addr: A,
timeout: Duration,
) -> Result<TcpStream>
Expand description
Opens a TCP connection to a remote host.
addr
is an address of the remote host. Anything which implements
ToSocketAddrs
trait can be supplied for the address; see this trait
documentation for concrete examples.
If addr
yields multiple addresses, connect
will be attempted with
each of the addresses until a connection is successful. If none of
the addresses result in a successful connection, the error returned from
the last connection attempt (the last address) is returned.
ยงExamples
Open a TCP connection to 127.0.0.1:8080
:
if let Ok(stream) = open_coroutine::connect_timeout("127.0.0.1:8080", std::time::Duration::from_secs(3)) {
println!("Connected to the server!");
} else {
println!("Couldn't connect to server...");
}
Open a TCP connection to 127.0.0.1:8080
. If the connection fails, open
a TCP connection to 127.0.0.1:8081
:
use std::net::SocketAddr;
let addrs = [
SocketAddr::from(([127, 0, 0, 1], 8080)),
SocketAddr::from(([127, 0, 0, 1], 8081)),
];
if let Ok(stream) = open_coroutine::connect_timeout(&addrs[..], std::time::Duration::from_secs(3)) {
println!("Connected to the server!");
} else {
println!("Couldn't connect to server...");
}