简体   繁体   中英

Get the host IP address in SFML

I'm making a game where a player can host an online session. I'm using SFML network library. But I want to display the IP address of the server host when it is waiting for incoming connections. How can I get this IP address? I'm looking for the internal IP address, but if it's possible to get the external IP address, that would be a bonus.

sf::TcpListener listener;

// bind the listener to a port
if (listener.listen(53000) != sf::Socket::Done)
{
    // error...
}

// accept a new connection
sf::TcpSocket client;
if (listener.accept(client) != sf::Socket::Done)
{
    // error...
}

Unless I've misunderstood your problem, you're looking for those static functions:

sf::IpAddress sf::IpAddress::getLocalAddress();
sf::IpAddress sf::IpAddress::getPublicAddress(Time timeout);

Documentation is here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM