xtd 0.2.0
Loading...
Searching...
No Matches
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 list<string>& 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]);
console::write_line("\r\n");
}
} 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());
if (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.
console::write_line("\r\nip_v6_any: {}", ip_address::ip_v6_any);
// Display the server loopback address.
console::write_line("ip_v6_loopback: {}", ip_address::ip_v6_loopback);
// Used during autoconfiguration first phase.
console::write_line("ip_v6_none: {}", ip_address::ip_v6_none);
console::write_line("is_loopback(ip_v6_loopback): {}", ip_address::is_loopback(ip_address::ip_v6_loopback));
}
console::write_line("is_loopback(loopback):{}", ip_address::is_loopback(ip_address::loopback));
} 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
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.h:71
virtual size_type size() const noexcept
Returns the number of elements in the container, i.e. std::distance(xtd::collections::generic::list::...
Definition list.h:364
The exception that is thrown when a method call is invalid for the object's current state.
Definition system_exception.h:18
Represents a mutable string of characters. This class cannot be inherited.
Definition basic_string_builder.h:35
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.h:175
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.h:15
The xtd::net::sockets namespace provides a managed implementation of the Berkeley Sockets interface f...
Definition address_family.h:16
The xtd::net namespace provides a simple programming interface for many of the protocols used on netw...
Definition cookie_exception.h:11
Contains classes that represent ASCII and Unicode character encodings; abstract base classes for conv...
Definition basic_string_builder.h:16
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10