xtd 0.2.0
ip_address.cpp

Shows how to use xtd::net::dns class.

#include <xtd/net/sockets/socket>
#include <xtd/net/dns>
#include <xtd/net/ip_address>
#include <xtd/text/string_builder>
#include <xtd/console>
#include <xtd/startup>
#include <regex>
using namespace xtd;
using namespace xtd::collections::generic;
using namespace xtd::net;
using namespace xtd::net::sockets;
using namespace xtd::text;
class test_ip_address {
public:
static auto main(const argument_collection& args) {
auto server = string_builder {};
// Define a regular expression to parse user's input.
// This is a security check. It allows only
// alphanumeric input string between 2 to 40 character long.
auto rex = std::regex {R"(^[a-zA-Z]\w{1,39}$)"};
if (args.size() < 1) {
// If no server name is passed as an argument to this program, use the current
// server name as default.
server = dns::get_host_name();
console::write_line("Using current host: {}", server);
} else {
server = args[0];
if (!regex_match(server.chars(), rex)) {
console::write_line("Input string format not allowed.");
return;
}
}
// Get the list of the addresses associated with the requested server.
ip_addresses(server.to_string());
// Get additional address information.
ip_address_additional_info();
}
private:
static void ip_addresses(const string& server) {
try {
// Get server related information.
auto heserver = dns::get_host_entry(server);
// Loop on the AddressList
for (auto cur_add : heserver.address_list()) {
// Display the type of address family supported by the server. If the
// server is IPv6-enabled this value is: InterNetworkV6. If the server
// is also IPv4-enabled there will be an additional value of InterNetwork.
console::write_line("address_family: {}", cur_add.address_family());
// Display the ScopeId property in case of IPV6 addresses.
if (cur_add.address_family() == address_family::inter_network_v6)
console::write_line("scope id: {}", cur_add.scope_id());
// Display the server IP address in the standard format. In
// IPv4 the format will be dotted-quad notation, in IPv6 it will be
// in in colon-hexadecimal notation.
console::write_line("address: {}", cur_add);
// Display the server IP address in byte format.
console::write("address_bytes: ");
auto bytes = cur_add.get_address_bytes();
for (size_t i = 0; i < bytes.size(); i++)
console::write(bytes[i]);
}
} catch (const system_exception& e) {
console::write_line("[do_resolve] exception: {}", e);
}
}
// This IPAddressAdditionalInfo displays additional server address information.
static void ip_address_additional_info() {
try {
// Display the flags that show if the server supports IPv4 or IPv6
// address schemas.
console::write_line("\r\nos_supports_ip_v4: {}", socket::os_supports_ip_v4());
console::write_line("os_supports_ip_v6: {}", socket::os_supports_ip_v6());
// Display the server Any address. This IP address indicates that the server
// should listen for client activity on all network interfaces.
// Display the server loopback address.
// Used during autoconfiguration first phase.
}
} catch (const system_exception& e) {
console::write_line("[ip_addresses] exception: {}", e);
}
}
};
startup_(test_ip_address::main);
// This code produces the following output if no args:
//
// Using current host: !---OMITTED---!.local
// address_family: inter_network
// address: 192.168.1.37
// address_bytes: 192168137
//
//
// os_supports_ip_v4: true
// os_supports_ip_v6: true
//
// ip_v6_any: ::
// ip_v6_loopback: ::1
// ip_v6_none: ::
// is_loopback(ip_v6_loopback): true
// is_loopback(loopback):true
virtual size_type size() const noexcept
Returns the number of elements in the container, i.e. std::distance(xtd::array::begin(),...
Definition basic_array.hpp:246
static void write(arg_t &&value)
Writes the text representation of the specified value to the standard output stream.
Definition console.hpp:462
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
static xtd::string get_host_name()
Gets the host name of the local computer.
static xtd::net::ip_host_entry get_host_entry(const ip_address &address)
Resolves an IP address to an xtd::net::ip_host_entry instance.
static const ip_address ip_v6_none
Provides an IP address that indicates that no network interface should be used. This field is constan...
Definition ip_address.hpp:62
static const ip_address loopback
Provides the IP loopback address. This field is constant.
Definition ip_address.hpp:65
static const ip_address ip_v6_loopback
Provides the IP loopback address. This field is constant.
Definition ip_address.hpp:58
static const ip_address ip_v6_any
The Socket::Bind method uses the cIPv6Any field to indicate that a Socket must listen for client acti...
Definition ip_address.hpp:55
static bool is_loopback(const ip_address &address)
Indicates whether the specified IP address is the loopback address.
static bool os_supports_ip_v6() noexcept
Indicates whether the underlying operating system and network adaptors support Internet Protocol vers...
static bool os_supports_ip_v4() noexcept
Indicates whether the underlying operating system and network adaptors support Internet Protocol vers...
The exception that is thrown when a method call is invalid for the object's current state.
Definition system_exception.hpp:18
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:168
xtd::text::basic_string_builder< char > string_builder
Represents a mutable string of characters of UTF-8 code units. This class cannot be inherited.
Definition string_builder.hpp:19
xtd::array< xtd::string > argument_collection
Represents the collection of arguments passed to the main entry point method.
Definition argument_collection.hpp:19
@ inter_network_v6
Address for IP version 6.
Definition address_family.hpp:82
@ i
The I key.
Definition console_key.hpp:104
@ e
The E key.
Definition console_key.hpp:96
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:16
The xtd::net::sockets namespace provides a managed implementation of the Berkeley Sockets interface f...
Definition address_family.hpp:16
The xtd::net namespace provides a simple programming interface for many of the protocols used on netw...
Definition cookie_exception.hpp:10
Contains classes that represent ASCII and Unicode character encodings; abstract base classes for conv...
Definition basic_string_builder.hpp:17
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8