Added new ip::host_name functions for getting system host name.

This commit is contained in:
chris_kohlhoff 2006-04-25 01:13:40 +00:00
parent 4726b2890c
commit 59148c5326
10 changed files with 118 additions and 0 deletions

View File

@ -96,6 +96,7 @@ nobase_include_HEADERS = \
asio/ip/basic_resolver_entry.hpp \
asio/ip/basic_resolver_iterator.hpp \
asio/ip/basic_resolver_query.hpp \
asio/ip/host_name.hpp \
asio/ip/multicast.hpp \
asio/ip/resolver_query_base.hpp \
asio/ip/tcp.hpp \

View File

@ -44,6 +44,7 @@
#include "asio/ip/basic_resolver_entry.hpp"
#include "asio/ip/basic_resolver_iterator.hpp"
#include "asio/ip/basic_resolver_query.hpp"
#include "asio/ip/host_name.hpp"
#include "asio/ip/multicast.hpp"
#include "asio/ip/resolver_query_base.hpp"
#include "asio/ip/tcp.hpp"

View File

@ -0,0 +1,59 @@
//
// host_name.hpp
// ~~~~~~~~~~~~~
//
// Copyright (c) 2003-2006 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef ASIO_IP_HOST_NAME_HPP
#define ASIO_IP_HOST_NAME_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include "asio/detail/push_options.hpp"
#include "asio/detail/push_options.hpp"
#include <string>
#include "asio/detail/pop_options.hpp"
#include "asio/error_handler.hpp"
#include "asio/detail/socket_ops.hpp"
namespace asio {
namespace ip {
/// Get the current host name.
std::string host_name();
/// Get the current host name.
template <typename Error_Handler>
std::string host_name(Error_Handler error_handler);
inline std::string host_name()
{
return host_name(asio::throw_error());
}
template <typename Error_Handler>
std::string host_name(Error_Handler error_handler)
{
char name[1024];
if (asio::detail::socket_ops::gethostname(name, sizeof(name)) != 0)
{
error_handler(asio::error(asio::detail::socket_ops::get_error()));
return std::string();
}
return name;
}
} // namespace ip
} // namespace asio
#include "asio/detail/pop_options.hpp"
#endif // ASIO_IP_HOST_NAME_HPP

View File

@ -29,6 +29,7 @@ noinst_PROGRAMS = \
tests/unit/ip/basic_resolver_entry_test \
tests/unit/ip/basic_resolver_iterator_test \
tests/unit/ip/basic_resolver_query_test \
tests/unit/ip/host_name_test \
tests/unit/ip/multicast_test \
tests/unit/ip/resolver_query_base_test \
tests/unit/ip/tcp_test \
@ -116,6 +117,7 @@ TESTS = \
tests/unit/ip/basic_resolver_entry_test \
tests/unit/ip/basic_resolver_iterator_test \
tests/unit/ip/basic_resolver_query_test \
tests/unit/ip/host_name_test \
tests/unit/ip/multicast_test \
tests/unit/ip/resolver_query_base_test \
tests/unit/ip/tcp_test \
@ -184,6 +186,7 @@ tests_unit_ip_basic_endpoint_test_SOURCES = tests/unit/ip/basic_endpoint_test.cp
tests_unit_ip_basic_resolver_entry_test_SOURCES = tests/unit/ip/basic_resolver_entry_test.cpp
tests_unit_ip_basic_resolver_iterator_test_SOURCES = tests/unit/ip/basic_resolver_iterator_test.cpp
tests_unit_ip_basic_resolver_query_test_SOURCES = tests/unit/ip/basic_resolver_query_test.cpp
tests_unit_ip_host_name_test_SOURCES = tests/unit/ip/host_name_test.cpp
tests_unit_ip_multicast_test_SOURCES = tests/unit/ip/multicast_test.cpp
tests_unit_ip_resolver_query_base_test_SOURCES = tests/unit/ip/resolver_query_base_test.cpp
tests_unit_ip_tcp_test_SOURCES = tests/unit/ip/tcp_test.cpp

View File

@ -35,6 +35,7 @@ UNIT_TEST_EXES = \
tests\unit\ip\basic_resolver_entry_test.exe \
tests\unit\ip\basic_resolver_iterator_test.exe \
tests\unit\ip\basic_resolver_query_test.exe \
tests\unit\ip\host_name_test.exe \
tests\unit\ip\multicast_test.exe \
tests\unit\ip\resolver_query_base_test.exe \
tests\unit\ip\tcp_test.exe \

View File

@ -36,6 +36,7 @@ UNIT_TEST_EXES = \
tests/unit/ip/basic_resolver_entry_test.exe \
tests/unit/ip/basic_resolver_iterator_test.exe \
tests/unit/ip/basic_resolver_query_test.exe \
tests/unit/ip/host_name_test.exe \
tests/unit/ip/multicast_test.exe \
tests/unit/ip/resolver_query_base_test.exe \
tests/unit/ip/tcp_test.exe \

View File

@ -66,6 +66,7 @@ UNIT_TEST_EXES = \
tests\unit\ip\basic_resolver_entry_test.exe \
tests\unit\ip\basic_resolver_iterator_test.exe \
tests\unit\ip\basic_resolver_query_test.exe \
tests\unit\ip\host_name_test.exe \
tests\unit\ip\multicast_test.exe \
tests\unit\ip\resolver_query_base_test.exe \
tests\unit\ip\tcp_test.exe \

View File

@ -110,6 +110,7 @@ Standard exception class.
\li \ref async_read
\li \ref write
\li \ref async_write
\li \ref asio::ip::host_name
<H2>Type Traits</H2>

View File

@ -111,6 +111,7 @@ Standard exception class.
\li \ref async_read
\li \ref write
\li \ref async_write
\li \ref asio::ip::host_name
<H2>Type Traits</H2>

View File

@ -0,0 +1,49 @@
//
// host_name_test.cpp
// ~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2006 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Test that header file is self-contained.
#include "asio/ip/host_name.hpp"
#include "asio.hpp"
#include "../unit_test.hpp"
//------------------------------------------------------------------------------
// ip_host_name_compile test
// ~~~~~~~~~~~~~~~~~~~~~~~~~~
// The following test checks that all host_name functions compile and link
// correctly. Runtime failures are ignored.
namespace ip_host_name_compile {
using namespace asio;
void test()
{
try
{
std::string host_name = ip::host_name();
std::string host_name2 = ip::host_name(asio::ignore_error());
}
catch (std::exception&)
{
}
}
} // namespace ip_host_name_compile
//------------------------------------------------------------------------------
test_suite* init_unit_test_suite(int argc, char* argv[])
{
test_suite* test = BOOST_TEST_SUITE("ip/host_name");
test->add(BOOST_TEST_CASE(&ip_host_name_compile::test));
return test;
}