(C++) WtGetIpAddress

February 24, 2017 · View on GitHub

 

 

 

 

 

(C++) WtGetIpAddress

 

WtQt
CreatorLubuntu

 

Wt FAQ on how to obtain the client's (that is: any person that browser your Wt website from any place in the world) IP address.

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

  • Boost Boost: version 1.55
  • Qt Qt: version 5.4.1 (32 bit)
  • STL STL: GNU ISO C++ Library, version 4.9.2
  • Wt Wt: version 3.3.3

 

 

 

 

 

Qt project file: ./CppWtGetIpAddress/CppWtGetIpAddress.pro

 


#------------------------------------------------- # # Project created by QtCreator 2011-05-15T20:17:39 # #------------------------------------------------- QT       += core QT       -= gui LIBS += -lwt -lwthttp -lboost_program_options INCLUDEPATH += ../../Classes/CppWtAutoConfig TARGET = CppWtGetIpAddress CONFIG   += console CONFIG   -= app_bundle TEMPLATE = app SOURCES += main.cpp \     ../../Classes/CppWtAutoConfig/wtautoconfig.cpp HEADERS += \     ../../Classes/CppWtAutoConfig/wtautoconfig.h

 

 

 

 

 

./CppWtGetIpAddress/main.cpp

 


#include <Wt/WApplication> #include <Wt/WEnvironment> #include <Wt/WContainerWidget> #include <Wt/WLabel> //--------------------------------------------------------------------------- #include "wtautoconfig.h" //--------------------------------------------------------------------------- const std::string GetIpAddress(const Wt::WEnvironment& env) {   return env.clientAddress(); } //--------------------------------------------------------------------------- struct WtDialog : public Wt::WContainerWidget {   WtDialog(const std::string& ip_address)   {     new Wt::WLabel(       std::string("Client IP address: ")       + ip_address,this);   } }; //--------------------------------------------------------------------------- struct WtApplication : public Wt::WApplication {   WtApplication(const Wt::WEnvironment& env)   : Wt::WApplication(env)   {     this->setTitle("CppWtGetIpAddress");     this->useStyleSheet("wt.css");     this->enableUpdates();     const std::string ip_address = env.clientAddress();     root()->addWidget(new WtDialog(ip_address));   } }; //--------------------------------------------------------------------------- Wt::WApplication * createApplication(const Wt::WEnvironment& env) {   return new WtApplication(env); } //--------------------------------------------------------------------------- int main(int argc, char **argv) {   WtAutoConfig a(argc,argv,createApplication);   WtAutoConfig::SaveDefaultStylesheet();   return a.Run(); } //---------------------------------------------------------------------------