Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::asio get client ip

Tags:

c++

ip

boost-asio

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?

like image 589
ducin Avatar asked Dec 17 '12 20:12

ducin


1 Answers

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

like image 148
PSIAlt Avatar answered Sep 29 '22 11:09

PSIAlt