SFML community forums
General => SFML wiki => Topic started by: ActionBoy on February 26, 2011, 05:35:27 pm
-
This small code might help someone with a little php/sfml coding. Would it fit in the wiki?
main.cpp
#include <SFML/Network.hpp>
#include <iostream>
using namespace std;
sf::IPAddress mMyIP;
string mStringDomain = "http://www.yourdomain.com";
string mStringURI = "/ip.php";
int getMyIP()
{
sf::Http Http;
Http.SetHost(mStringDomain);
// Prepare a request to retrieve the index page
sf::Http::Request Request;
Request.SetMethod(sf::Http::Request::Get);
Request.SetURI(mStringURI);
sf::Http::Response Page = Http.SendRequest(Request);
if(Page.GetStatus()==200)
{
mMyIP = Page.GetBody();
if(mMyIP.IsValid() == true)
{
cout<<"My IP: "<<mMyIP.ToString()<<endl;
return 1;
}
else
{
cout<<"The ipadress retrieved was not valid."<<endl;
return -1;
}
}
else
{
cout<<"Could not connect."<<endl;
return -2;
}
}
int main()
{
getMyIP();
// Wait until the user presses 'enter' key
std::cout << "Press enter to exit..." << std::endl;
std::cin.ignore(10000, '\n');
return EXIT_SUCCESS;
}
ip.php
<?php
echo $_SERVER['REMOTE_ADDR'];
?>
-
There are already functions which get the ip:
sf::IpAddress::GetLocalAddress(); // my address on the local network
sf::IpAddress::GetPublicAddress(); // my address on the internet
-
Yes, but this is also a simple introduction on how to get a value from your own server.