I'm writing a server over TCP using boost::asio. I'm trying to find a way to block connections outside localhost. Only thing I found is this. There is:
boost::asio::ip::host_name()
which returns "tomasz-lenovo-ideapad-Y530" (that's the name of my machine). How to get raw ip ("127.0.0.1" or "localhost") from it?
From the socket object that serve given connection.
std::cout << "Peer IP: " << socket.remote_endpoint().address().to_string() << std::endl;
FYI: If you want to prevent any connections from other machines, its enough to listen 127.0.0.1 and not listen other interfaces. To do this, you should create acceptor like this:
acceptor(io_service, ip::tcp::endpoint(ip::address::from_string("127.0.0.1"), "5555"));
This will listen 127.0.0.1 on port 5555 only
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With