userver: userver/net/blocking/connect_tcp_by_name.hpp Source File
Loading...
Searching...
No Matches
connect_tcp_by_name.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/net/blocking/connect_tcp_by_name.hpp
4/// @brief @copybrief net::blocking::ConnectTcpByName
5
6#include <cstdint>
7#include <string_view>
8
9#include <userver/engine/deadline.hpp>
10#include <userver/engine/io/socket.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace net::blocking {
15
16/// @brief Establishes a TCP connection to the given host and port using
17/// blocking DNS resolution via @ref net::blocking::GetAddrInfo.
18///
19/// Resolves the host name with getaddrinfo, then tries to connect to each
20/// resolved address in turn. The first successful connection is returned.
21/// TCP_NODELAY is set on the socket. Does not use @ref clients::dns::Resolver;
22/// for async resolution use @ref net::ConnectTcpByName.
23///
24/// @param host DNS name or IP address to connect to (e.g. "localhost",
25/// "127.0.0.1").
26/// @param port TCP port number.
27/// @param deadline Maximum time allowed for the whole operation (resolution +
28/// connection).
29/// @return A connected TCP socket.
30/// @throws std::runtime_error if getaddrinfo fails (e.g. unknown host).
31/// @throws engine::io::IoException if connection to all resolved
32/// addresses fails (e.g. connection refused, timeout).
33///
34/// @note For async/cached DNS resolution use @ref net::ConnectTcpByName with
35/// @ref clients::dns::Resolver.
36///
37/// @snippet src/net/blocking/connect_tcp_by_name_test.cpp ConnectTcpByName blocking localhost
38engine::io::Socket ConnectTcpByName(std::string_view host, std::uint16_t port, engine::Deadline deadline);
39
40} // namespace net::blocking
41
42USERVER_NAMESPACE_END