I have this code in my server (scattered around the constructors etc. but I've left out the unnecessary parts):
using namespace boost::asio;
io_service ioserv;
ip::tcp::endpoint endpoint(ip::tcp::v4(), 1922);
ip::tcp::acceptor acceptor(ioserv, endpoint);
ip::tcp::socket socket(ioserv);
acceptor.accept(socket);
Now I want to write my IP to the console. Unfortunately both
cout << endpoint.address() << endl;
and
cout << acceptor.local_endpoint().address() << endl;
0.0.0.0
How to get the IP address of my machine?
Where did you get that code?
Try this:
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query(boost::asio::ip::host_name(), "");
tcp::resolver::iterator iter = resolver.resolve(query);
tcp::resolver::iterator end; // End marker.
while (iter != end)
{
tcp::endpoint ep = *iter++;
std::cout << ep << std::endl;
}
And take a look at this discussion.