How to launch a Wt Client? - c++

I have just started a development using wt(with c++ binding). And i have could done till now, is reading a very few documentation and a little no of sample programs(written in c++ and wt).
After that I installed wt on my machine amd tried to run one one of those demo programs.
hello.cc
#include <Wt/WApplication>
#include <Wt/WBreak>
#include <Wt/WContainerWidget>
#include <Wt/WLineEdit>
#include <Wt/WPushButton>
#include <Wt/WText>
class HelloApplication : public Wt::WApplication
{
public:
HelloApplication(const Wt::WEnvironment& env);
private:
Wt::WLineEdit *nameEdit_;
Wt::WText *greeting_;
void greet();
};
HelloApplication::HelloApplication(const Wt::WEnvironment& env)
: Wt::WApplication(env)
{
setTitle("Hello world");
root()->addWidget(new Wt::WText("Your name, please ? "));
nameEdit_ = new Wt::WLineEdit(root());
Wt::WPushButton *button = new Wt::WPushButton("Greet me.", root());
root()->addWidget(new Wt::WBreak());
greeting_ = new Wt::WText(root());
button->clicked().connect(this, &HelloApplication::greet);
}
void HelloApplication::greet()
{
greeting_->setText("Hello there, " + nameEdit_->text());
}
Wt::WApplication *createApplication(const Wt::WEnvironment& env)
{
return new HelloApplication(env);
}
int main(int argc, char **argv)
{
return Wt::WRun(argc, argv, &createApplication);
}
I complied this code
g++ -o hello hello.cc -lwthttp -lwt
It was compiled successfully.Then I could run this server application successfully to run it on localhost
[manmatha#manmatha Lab]$ su
Password:
[root#manmatha Lab]# ./hello --docroot . --http-address 127.0.0.1 --http-port 9090
[2013-Jun-14 13:58:08.585419] 5066-[info] "WServer/wthttp:initializing built-in wthttpd"
[2013-Jun-14 13:58:08.590955] 5066-[info] "wthttp:started server: http://127.0.0.1:9090"
problem is when I type
localhost::9090
on the address bar of the internet browser on local machine., anything does not show up.
In this context , my specific question is how to start a wt client??
Thanx in advannce

try 127.0.0.1:9090
You specified 127.0.0.1 on the command line so type it in the browser's address bar.
This is a specific of the Wt embedded http server.

You have to mention the --deploy-path variable in your command line arguments. Try this
--http-address 127.0.0.1 --http-port 9090 --deploy-path=/hello --docroot=.
In the browser type http://localhost:9090/hello

Related

No reply data with QNetworkAccessManager when running on another machine

I have a simple program that should retrieve the HTML from a website URL.
main.cpp
#include "Downloader.h"
#include <QCoreApplication>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
auto dl = new Downloader(&a);
QString url = "https://www.dognow.at/ergebnisse/?page=1";
dl->fetch(url);
return a.exec();
}
Downloader.h
#ifndef DOWNLOADER_H
#define DOWNLOADER_H
#include <QNetworkReply>
#include <QObject>
class Downloader : public QObject
{
Q_OBJECT
public:
explicit Downloader(QObject* parent = nullptr);
void fetch(QString &url);
private:
QNetworkAccessManager* m_manager;
private slots:
void replyFinished(QNetworkReply* rep);
};
#endif // DOWNLOADER_H
Downloader.cpp
#include "Downloader.h"
#include <QDebug>
Downloader::Downloader(QObject* parent): QObject(parent),
m_manager(new QNetworkAccessManager(parent))
{}
void Downloader::fetch(QString& url)
{
qDebug() << "fetch " << url;
connect(m_manager, &QNetworkAccessManager::finished, this, &Downloader::replyFinished);
m_manager->get(QNetworkRequest(QUrl(url)));
}
void Downloader::replyFinished(QNetworkReply* rep)
{
QByteArray data=rep->readAll();
QString str(data);
qDebug() << "data len: " << str.length();
rep->close();
}
When I run the program on my local PC it works fine. When I run it on another machine the reply data is empty. On both systems I use Linux (x86_64) and Qt 5.15.0.
I hope someone can give me a hint where I should have a look at.
UPDATE: 2022-04-04 - 16:22:
when I run a simple curl command on the failing machine it works fine.
Ok, I found the problem.
On the failing machin I have an older ubuntu (16.04 LTS) running with an incompatible openssl version.
I found it out because I copied my own Qt libs build (debug) to the other machine and I got SSL error (incompatbile version).
I installed a newer openssl version and know it works.

fatal error: Wt/WApplication.h: No such file or directory

I'm following along with this tutorial for Webtoolkit: https://www.webtoolkit.eu/wt/doc/tutorial/wt.html
I am running all this on a Rasbian/Debian on a Virtual Machine and I am using C++14
I decided to copy paste the hello.cpp code onto my Desktop and am compiling it like this on my terminal as I was facing linking errors and thus followed along with command line examples in the tutorial:
g++ -std=c++14 -o hello hello.cpp -I/usr/include -L/usr/lib
However I still get:
hello.cpp:1:29: fatal error: Wt/WApplication.h: No such file or directory
#include <Wt/WApplication.h>
My Wt files are located in /usr/include and /usr/lib which is why I used them.
This stackoverflow did not solve my issue: How to Install Wt into a Custom Folder Without "fatal error: Wt/WApplication: No such file or directory"
EDIT: I was able to run the example files located in the Wt folders in /usr/lib/Wt/examples but can't run it on Desktop, I followed the command line examples on the tutorial for linking errors
EDIT 2: The cpp code if it helps, same as tutorial, just copy pasted:
#include <Wt/WApplication.h>
#include <Wt/WBreak.h>
#include <Wt/WContainerWidget.h>
#include <Wt/WLineEdit.h>
#include <Wt/WPushButton.h>
#include <Wt/WText.h>
class HelloApplication : public Wt::WApplication
{
public:
HelloApplication(const Wt::WEnvironment& env);
private:
Wt::WLineEdit *nameEdit_;
Wt::WText *greeting_;
};
HelloApplication::HelloApplication(const Wt::WEnvironment& env)
: Wt::WApplication(env)
{
setTitle("Hello world");
root()->addWidget(std::make_unique<Wt::WText>("Your name, please? "));
nameEdit_ = root()->addWidget(std::make_unique<Wt::WLineEdit>());
Wt::WPushButton *button = root()->addWidget(std::make_unique<Wt::WPushButton>("Greet me."));
root()->addWidget(std::make_unique<Wt::WBreak>());
greeting_ = root()->addWidget(std::make_unique<Wt::WText>());
auto greet = [this]{
greeting_->setText("Hello there, " + nameEdit_->text());
};
button->clicked().connect(greet);
}
int main(int argc, char **argv)
{
return Wt::WRun(argc, argv, [](const Wt::WEnvironment& env) {
return std::make_unique<HelloApplication>(env);
});
}
The Wt include files do not have the .h extension on some OSs.
Try #include <Wt/WApplication>
root#08c7a05c8129:/usr/lib/Wt/examples/hello# ls /usr/include/Wt
Auth WBatchEditProxyModel WDateValidator WHTML5Audio WLocalizedStrings WPopupWidget WSslInfo WTimePicker
Chart WBoostAny WDefaultLayout WHTML5Video WLogger WProgressBar WStackedWidget WTimeValidator
Dbo WBootstrapTheme WDefaultLoadingIndicator WIOService WMatrix4x4 WPushButton WStandardItem WTimer
Ext WBorder WDialog WIcon WMeasurePaintDevice WRadioButton WStandardItemModel WTimerWidget
Http WBorderLayout WDllDefs.h WIconPair WMediaPlayer WRandom WStatelessSlot WToolBar
Json WBoxLayout WDoubleSpinBox WIdentityProxyModel WMemoryResource WRasterImage WStreamResource WTransform
Mail WBreak WDoubleValidator WImage WMenu WReadOnlyProxyModel WString WTree
Payment WBrush WEnvironment WInPlaceEdit WMenuItem WRectArea WStringListModel WTreeNode
Render WButtonGroup WEvent WIntValidator WMessageBox WRectF WStringStream WTreeTable
Utils WCalendar WException WInteractWidget WMessageResourceBundle WRegExp WStringUtil WTreeTableNode
WAbstractArea WCanvasPaintDevice WFileResource WItemDelegate WMessageResources WRegExpValidator WSubMenuItem WTreeView
WAbstractGLImplementation WCheckBox WFileUpload WItemSelectionModel WModelIndex WResource WSuggestionPopup WVBoxLayout
WAbstractItemDelegate WCircleArea WFitLayout WJavaScript WNavigationBar WScrollArea WSvgImage WValidationStatus
WAbstractItemModel WClientGLWidget WFlags WJavaScriptPreamble WObject WScrollBar WTabWidget WValidator
WAbstractItemView WColor WFlashObject WJavaScriptSlot WOverlayLoadingIndicator WSelectionBox WTable WVector3
WAbstractListModel WCombinedLocalizedStrings WFont WLabel WPaintDevice WServer WTableCell WVector4
WAbstractMedia WComboBox WFontMetrics WLayout WPaintedWidget WServerGLWidget WTableColumn WVectorImage
WAbstractProxyModel WCompositeWidget WFormModel WLayoutItem WPainter WShadow WTableRow WVideo
WAbstractSpinBox WConfig.h WFormWidget WLayoutItemImpl WPainterPath WSignal WTableView WViewWidget
WAbstractTableModel WContainerWidget WGLWidget WLength WPanel WSignalMapper WTemplate WVirtualImage
WAbstractToggleButton WCssDecorationStyle WGenericMatrix WLengthValidator WPdfImage WSlider WTemplateFormView WVmlImage
WAccordionLayout WCssStyleSheet WGlobal WLineEdit WPen WSocketNotifier WText WWebWidget
WAggregateProxyModel WCssTheme WGoogleMap WLineF WPoint WSortFilterProxyModel WTextArea WWidget
WAnchor WDate WGradient WLink WPointF WSound WTextEdit WWidgetItem
WAnimation WDateEdit WGridLayout WLoadingIndicator WPolygonArea WSpinBox WTheme
WApplication WDatePicker WGroupBox WLocalDateTime WPopupMenu WSplitButton WTime
WAudio WDateTime WHBoxLayout WLocale WPopupMenuItem WSslCertificate WTimeEdit
so use without extension:
#include <Wt/WApplication>
Try :
g++ -std=c++14 -o hello hello.cpp -I/usr/local/include/Wt -L/usr/local/lib64/
OR
g++ -std=c++14 -o hello hello.cpp -I/usr/local/include/Wt -L/usr/local/lib/

Boost Process fails to launch the firefox browser (ubuntu 14.04, eclipse mars CDT)

This snippet of code fails to launch the firefox browser. Note I am trying to invoke the Boost.Process on a boost thread.
#include <iostream>
#include <boost/thread.hpp>
#include <boost/process.hpp>
const std::string BROWSER_EXE = "firefox";
void LaunchStream(std::string url)
{
std::vector<std::string> args;
args.push_back(url);
boost::process::context ctx;
ctx.stderr_behavior = boost::process::redirect_stream_to_stdout();
ctx.stdout_behavior = boost::process::silence_stream();
std::string exe = boost::process::find_executable_in_path(BROWSER_EXE);
boost::process::child proc = boost::process::launch(exe, args, ctx); <---DOES NOT LAUNCH FIREFOX
proc.wait();
}
int main(int argc, char **argv)
{
std::string url = <some url to open>;
boost::thread streamThread(LaunchStream, url);
<wait a long time>
}
What I do observe is a zombie firefox process from: ps -ef | grep "firefox"
What am I doing wrong?

cppcms can't work

My OS: Mac OSX
My gcc version: 4.2.1
My clang version: llvm 6.1.0
cppcms version: 1.0.5
I download the cppcms framework and install the framework.The commands:
cd cppcms-1.0.5
mkdir build & cd build
cmake ..
make
make test
make install
No error found.
The I write a cpp file named hello.cpp. The program is like this:
#include <cppcms/application.h>
#include <cppcms/applications_pool.h>
#include <cppcms/service.h>
#include <cppcms/http_response.h>
#include <iostream>
using namespace std;
class hello : public cppcms::application{
public:
hello(cppcms::service &srv) : cppcms::application(srv){
}
virtual void main(std::string url);
};
void hello::main(std::string /*url*/)
{
response().out() <<
"<html>\n"
"<body>\n"
" <h1>Hello World</h1>\n"
"</body>\n"
"</html>\n";
}
int main(int argc,char ** argv) {
try {
cppcms::service srv(argc,argv);
srv.applications_pool().mount(
cppcms::applications_factory<hello>()
);
}
catch (std::exception const &e){
std::cerr << e.what() << std::endl;
}
return 0;
}
My config.js:
{
"service" : {
"api" : "http",
"port" : 8008
},
"http" : {
"script_names" : [ "/hello" ]
}
}
Compile commands:
c++ hello.cpp -lcppcms -o hello
./hello -c config.js
I visited the url "http://localhost:8008/hello",then web browser show me
"This webpage is not available".
What's wrong? How to fix the problem.
Most importantly you have to start your service after mounting it:
cppcms::service srv(argc,argv);
srv.applications_pool().mount(
cppcms::applications_factory<hello>()
);
srv.run();
I would also include the following two header files (at least on Linux
I got a compilation error otherwise):
#include <cppcms/application.h>
#include <cppcms/applications_pool.h>

Can multiple WT applications run on same port?

Update 1: I have recently found out that WT communicates using TCP (HTTP), if that helps anyone.
The title says it all, is it possible to run 2 different WT applications or projects on the same port? I know WT has come control over how the application is hosted with its start up parameters as follows. I am using Visual Studio 2010 and the parameters below are entered in the debugging->command arguments box as follows:
--http-address=0.0.0.0 --http-port=8080 --deploy-path=/hello --docroot=.
The above parameters will require that the user opens a web page at
http://127.0.0.1:8080/hello
So I though, hey what if I host another project with the below parameters
--http-address=0.0.0.0 --http-port=8080 --deploy-path=/world --docroot=.
so then I would need to connect to
http://127.0.0.1:8080/world
The above actually does not work, it only hosts the first application that connects, and the second will not connect until the first shuts down. So this brings me here. Are there any other ways to run multiple WT applications on the same port?
Thank you in advance for any help!
Yes, you can achieve that but you will need to create your own WRun and use addEntryPoint. This is actually mentioned in the tutorial and goes as follows:
Inside WRun()
WRun() is actually a convenience function which creates and configures a WServer instance. If you want more control, for example if you have multiple “entry points”, or want to control the server starting and stopping, you can use the WServer API directly instead.
Here you have an example, both applications are the Hello World application, but notice that they have different titles and different messages on the buttons and when you press them. You can find another implementation of WRun on src/http/Serve.C and src/Wt/WServer.
two_helloworlds.cc
#include <Wt/WApplication>
#include <Wt/WBreak>
#include <Wt/WContainerWidget>
#include <Wt/WLineEdit>
#include <Wt/WPushButton>
#include <Wt/WText>
#include <Wt/WException>
#include <Wt/WLogger>
#include <Wt/WServer>
class HelloApplication : public Wt::WApplication
{
public:
HelloApplication(const Wt::WEnvironment& env, const std::string& title);
private:
Wt::WLineEdit *nameEdit_;
Wt::WText *greeting_;
void greet();
};
HelloApplication::HelloApplication(const Wt::WEnvironment& env, const std::string& title)
: Wt::WApplication(env)
{
setTitle(title);
root()->addWidget(new Wt::WText("Your name, please ? "));
nameEdit_ = new Wt::WLineEdit(root());
Wt::WPushButton *button = new Wt::WPushButton("Greet me.", root());
root()->addWidget(new Wt::WBreak());
greeting_ = new Wt::WText(root());
button->clicked().connect(this, &HelloApplication::greet);
}
void HelloApplication::greet()
{ greeting_->setText("Hello there, " + nameEdit_->text());
}
class GoodbyeApplication : public Wt::WApplication{
public:
GoodbyeApplication(const Wt::WEnvironment& env, const std::string& title);
private: Wt::WLineEdit *nameEdit_;
Wt::WText *greeting_;
void greet();
};
GoodbyeApplication::GoodbyeApplication(const Wt::WEnvironment& env, const std::string& title)
: Wt::WApplication(env)
{
setTitle(title);
root()->addWidget(new Wt::WText("Your name, please ? "));
nameEdit_ = new Wt::WLineEdit(root());
Wt::WPushButton *button = new Wt::WPushButton("Say goodbye.", root());
root()->addWidget(new Wt::WBreak());
greeting_ = new Wt::WText(root());
button->clicked().connect(this, &GoodbyeApplication::greet);
}
void GoodbyeApplication::greet()
{
greeting_->setText("Goodbye, " + nameEdit_->text());
}
Wt::WApplication *createApplication(const Wt::WEnvironment& env)
{
return new HelloApplication(env, "First app");
}
Wt::WApplication *createSecondApplication(const Wt::WEnvironment& env)
{
return new GoodbyeApplication(env, "Second app");
}
int YourWRun(int argc, char *argv[], Wt::ApplicationCreator createApplication, Wt::ApplicationCreator createSecondApplication)
{
try {
// use argv[0] as the application name to match a suitable entry
// in the Wt configuration file, and use the default configuration
// file (which defaults to /etc/wt/wt_config.xml unless the environment
// variable WT_CONFIG_XML is set)
Wt::WServer server(argv[0],"");
// WTHTTP_CONFIGURATION is e.g. "/etc/wt/wthttpd"
server.setServerConfiguration(argc, argv, WTHTTP_CONFIGURATION);
// add a single entry point, at the default location (as determined
// by the server configuration's deploy-path)
server.addEntryPoint(Wt::Application, createApplication);
server.addEntryPoint(Wt::Application, createSecondApplication,"/second");
if (server.start()) {
int sig = Wt::WServer::waitForShutdown(argv[0]);
std::cerr << "Shutdown (signal = " << sig << ")" << std::endl;
server.stop();
/*
if (sig == SIGHUP)
WServer::restart(argc, argv, environ);
*/
}
} catch (Wt::WServer::Exception& e) {
std::cerr << e.what() << "\n";
return 1;
} catch (std::exception& e) {
std::cerr << "exception: " << e.what() << "\n";
return 1;
}
}
int main(int argc, char **argv)
{
return YourWRun(argc, argv, &createApplication, &createSecondApplication);
}
Compile it with
g++ -g -o two_helloworlds two_helloworlds.cc -I/usr/local/include -L/usr/local/lib -lwthttp -lwt -lboost_random -lboost_regex -lboost_signals -lboost_system -lboost_thread -lboost_filesystem -lboost_program_options -lboost_date_time
and execute with
./two_helloworlds --docroot . --http-address 0.0.0.0 --http-port 8080
on localhost:8080 you will access one of the applications and on localhost:8080/second you will access the other.