How to get the hostname of the node in C++?

In C++, how to get the hostname of the node?

In C++, the C way works too. However, with Boost, you can use the boost::asio::ip::host_name() function to get the hostname as a std::string:

namespace boost {
namespace asio {
namespace ip {

/// Get the current host name.
BOOST_ASIO_DECL std::string host_name();

...

More at http://www.boost.org/doc/libs/1_63_0/boost/asio/ip/host_name.hpp

One example:

The boost-host-name.cpp

#include <iostream>
#include <algorithm>
#include <boost/asio.hpp>

namespace ip = boost::asio::ip;

int main()
{
  boost::asio::io_service io_service;

  std::string h = ip::host_name();
  std::cout << "hostname: " << h << 'n';
}

Build and run it:

$ g++ boost-host-name.cpp -o host-name -lboost_system
$ ./host-name
hostname: host001

Similar Posts

  • How to dd on Windows?

    dd is a handy tool on Linux. But is it possible to run it on Windows? I find the dd in cygwin works very well for me. It provides as almost (if not the same) functions as the dd on Linux. The disks are specified in /dev/ as on Linux. This solution means: you need…

  • MySQL at Facebook

    Facebook uses lots MySQL databases. Any information about how Facebook scales MySQL? Some information on the Web: MySQL at Facebook’s page https://www.facebook.com/MySQLatFacebook?filter=1 A post by Ryan Thiessen, Database Operations at Facebook on Quora: http://www.quora.com/Facebook-Engineering/How-does-Facebook-structure-MySQL-so-that-it-is-robust-and-scalable And more: http://mashable.com/2011/12/15/facebook-timeline-mysql/ http://gigaom.com/2011/12/06/facebook-shares-some-secrets-on-making-mysql-scale/ http://www.wired.com/wiredenterprise/2011/12/facebook-timeline-anatomy “A lot of people are surprised that for this shiny new thing for Facebook, we’re using…

  • mrcc – A Distributed C Compiler System on MapReduce

    The mrcc project’s homepage is here: mrcc project. Abstract mrcc is an open source compilation system that uses MapReduce to distribute C code compilation across the servers of the cloud computing platform. mrcc is built to use Hadoop by default, but it is easy to port it to other could computing platforms, such as MRlite,…

Leave a Reply

Your email address will not be published. Required fields are marked *