1// 2// ip/impl/host_name.ipp 3// ~~~~~~~~~~~~~~~~~~~~~ 4// 5// Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com) 6// 7// Distributed under the Boost Software License, Version 1.0. (See accompanying 8// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 9// 10 11#ifndef BOOST_ASIO_IP_IMPL_HOST_NAME_IPP 12#define BOOST_ASIO_IP_IMPL_HOST_NAME_IPP 13 14#if defined(_MSC_VER) && (_MSC_VER >= 1200) 15# pragma once 16#endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 17 18#include <boost/asio/detail/config.hpp> 19#include <boost/asio/detail/socket_ops.hpp> 20#include <boost/asio/detail/throw_error.hpp> 21#include <boost/asio/detail/winsock_init.hpp> 22#include <boost/asio/ip/host_name.hpp> 23 24#include <boost/asio/detail/push_options.hpp> 25 26namespace boost { 27namespace asio { 28namespace ip { 29 30std::string host_name() 31{ 32 char name[1024]; 33 boost::system::error_code ec; 34 if (boost::asio::detail::socket_ops::gethostname(name, sizeof(name), ec) != 0) 35 { 36 boost::asio::detail::throw_error(ec); 37 return std::string(); 38 } 39 return std::string(name); 40} 41 42std::string host_name(boost::system::error_code& ec) 43{ 44 char name[1024]; 45 if (boost::asio::detail::socket_ops::gethostname(name, sizeof(name), ec) != 0) 46 return std::string(); 47 return std::string(name); 48} 49 50} // namespace ip 51} // namespace asio 52} // namespace boost 53 54#include <boost/asio/detail/pop_options.hpp> 55 56#endif // BOOST_ASIO_IP_IMPL_HOST_NAME_IPP 57