host_name.hpp 1.4 KB

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