userver: userver/net/blocking/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 blocking DNS resolution via net::blocking::GetAddrInfo.

Definition in file connect_tcp_by_name.hpp.

Go to the source code of this file.

#include <cstdint>
#include <string_view>
#include <userver/engine/deadline.hpp>
#include <userver/engine/io/socket.hpp>

Functions

engine::io::Socket net::blocking::ConnectTcpByName (std::string_view host, std::uint16_t port, engine::Deadline deadline)
 Establishes a TCP connection to the given host and port using blocking DNS resolution via net::blocking::GetAddrInfo.
 

Function Documentation

◆ ConnectTcpByName()

engine::io::Socket net::blocking::ConnectTcpByName ( std::string_view host,
std::uint16_t port,
engine::Deadline deadline )

Establishes a TCP connection to the given host and port using blocking DNS resolution via net::blocking::GetAddrInfo.

Resolves the host name with getaddrinfo, then tries to connect to each resolved address in turn. The first successful connection is returned. TCP_NODELAY is set on the socket. Does not use clients::dns::Resolver; for async resolution use net::ConnectTcpByName.

Parameters
hostDNS name or IP address to connect to (e.g. "localhost", "127.0.0.1").
portTCP port number.
deadlineMaximum time allowed for the whole operation (resolution + connection).
Returns
A connected TCP socket.
Exceptions
std::runtime_errorif getaddrinfo fails (e.g. unknown host).
engine::io::IoExceptionif connection to all resolved addresses fails (e.g. connection refused, timeout).
Note
For async/cached DNS resolution use net::ConnectTcpByName with clients::dns::Resolver.
auto socket = net::blocking::ConnectTcpByName("localhost", listener.Port(), deadline);