host_name.hpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // host_name.hpp
  3. // ~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2008 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. #ifndef BOOST_ASIO_IP_HOST_NAME_HPP
  11. #define BOOST_ASIO_IP_HOST_NAME_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/push_options.hpp>
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <string>
  18. #include <boost/asio/detail/pop_options.hpp>
  19. #include <boost/asio/error.hpp>
  20. #include <boost/asio/detail/socket_ops.hpp>
  21. #include <boost/asio/detail/throw_error.hpp>
  22. namespace boost {
  23. namespace asio {
  24. namespace ip {
  25. /// Get the current host name.
  26. std::string host_name();
  27. /// Get the current host name.
  28. std::string host_name(boost::system::error_code& ec);
  29. inline std::string host_name()
  30. {
  31. char name[1024];
  32. boost::system::error_code ec;
  33. if (boost::asio::detail::socket_ops::gethostname(name, sizeof(name), ec) != 0)
  34. {
  35. boost::asio::detail::throw_error(ec);
  36. return std::string();
  37. }
  38. return std::string(name);
  39. }
  40. inline std::string host_name(boost::system::error_code& ec)
  41. {
  42. char name[1024];
  43. if (boost::asio::detail::socket_ops::gethostname(name, sizeof(name), ec) != 0)
  44. return std::string();
  45. return std::string(name);
  46. }
  47. } // namespace ip
  48. } // namespace asio
  49. } // namespace boost
  50. #include <boost/asio/detail/pop_options.hpp>
  51. #endif // BOOST_ASIO_IP_HOST_NAME_HPP