userver: userver/net/connect_tcp_by_name.hpp File Reference
Loading...
Searching...
No Matches
connect_tcp_by_name.hpp File Reference

Detailed Description

Establishes a TCP connection to the given host and port using DNS resolution via the provided clients::dns::Resolver.

Definition in file connect_tcp_by_name.hpp.

Go to the source code of this file.

#include <cstdint>
#include <string>
#include <userver/clients/dns/resolver_fwd.hpp>
#include <userver/engine/deadline.hpp>
#include <userver/engine/io/socket.hpp>

Functions

engine::io::Socket net::ConnectTcpByName (const std::string &host, std::uint16_t port, clients::dns::Resolver &resolver, engine::Deadline deadline)
 Establishes a TCP connection to the given host and port using DNS resolution via the provided clients::dns::Resolver.
 

Function Documentation

◆ ConnectTcpByName()

engine::io::Socket net::ConnectTcpByName ( const std::string & host,
std::uint16_t port,
clients::dns::Resolver & resolver,
engine::Deadline deadline )

Establishes a TCP connection to the given host and port using DNS resolution via the provided clients::dns::Resolver.

Resolves the host name using resolver (which may use cache, /etc/hosts, and network DNS), then tries to connect to each resolved address in turn. The first successful connection is returned. TCP_NODELAY is set on the socket.

Parameters
hostDNS name or IP address to connect to (e.g. "localhost", "example.com", "127.0.0.1").
portTCP port number.
resolverDNS resolver instance (e.g. from clients::dns::Component).
deadlineMaximum time allowed for the whole operation (resolution + connection). Same deadline is used for both resolve and connect steps.
Returns
A connected TCP socket.
Exceptions
clients::dns::ResolverExceptionor clients::dns::NotResolvedException if the host name cannot be resolved.
engine::io::IoExceptionif connection to all resolved addresses fails (e.g. connection refused, timeout).
Note
Without a Resolver (e.g. in non-component code) use net::blocking::ConnectTcpByName.
auto socket = net::ConnectTcpByName("localhost", listener.Port(), resolver, deadline);