classes/CSocket.cpp
std::string CSocket::localIPAddress()
    {
        static std::string localIPAddress;
        if (localIPAddress.empty())
        {
            try
            {
                boost::asio::io_service ioService;
                boost::asio::ip::udp::resolver resolver(ioService);
                boost::asio::ip::udp::resolver::query query(boost::asio::ip::udp::v4(), "google.com", "");
                boost::asio::ip::udp::socket socket(ioService);
                socket.connect(*resolver.resolve(query));
                localIPAddress = socket.local_endpoint().address().to_string();
                socket.close();
            }
            catch (std::exception &e)
            {
                return ("127.0.0.1");
            }
        }
        return (localIPAddress);
    }
//
// Work out ip address for local machine. This is quite difficult to achieve but
// this is the best code i have seen for doing it. It just tries to connect to
// google.com with a udp connect to get the local socket endpoint.
// Note: Fall back of localhost on failure.
//
Work out ip address for local machine. This is quite difficult to achieve but
this is the best code i have seen for doing it. It just tries to connect to
google.com with a udp connect to get the local socket endpoint.
Note: Fall back of localhost on failure.
Work out ip address for local machine . This is quite difficult to achieve but this is the best code i have seen for doing it . It just tries to connect to google . com with a udp connect to get the local socket endpoint . Note : Fall back of localhost on failure .
std::string CSocket::localIPAddress()
    {
        static std::string localIPAddress;
        if (localIPAddress.empty())
        {
            try
            {
                boost::asio::io_service ioService;
                boost::asio::ip::udp::resolver resolver(ioService);
                boost::asio::ip::udp::resolver::query query(boost::asio::ip::udp::v4(), "google.com", "");
                boost::asio::ip::udp::socket socket(ioService);
                socket.connect(*resolver.resolve(query));
                localIPAddress = socket.local_endpoint().address().to_string();
                socket.close();
            }
            catch (std::exception &e)
            {
                return ("127.0.0.1");
            }
        }
        return (localIPAddress);
    }
std :: string CSocket :: localIPAddress ( ) { static std :: string localIPAddress ; if ( localIPAddress . empty ( ) ) { try { boost :: asio :: io_service ioService ; boost :: asio :: ip :: udp :: resolver resolver ( ioService ) ; boost :: asio :: ip :: udp :: resolver :: query query ( boost :: asio :: ip :: udp :: v4 ( ) , " " , " " ) ; boost :: asio :: ip :: udp :: socket socket ( ioService ) ; socket . connect ( * resolver . resolve ( query ) ) ; localIPAddress = socket . local_endpoint ( ) . address ( ) . to_string ( ) ; socket . close ( ) ; } catch ( std :: exception & e ) { return ( " " ) ; } } return ( localIPAddress ) ; }
Work out ip address for local machine.
Work out ip address for local machine .