I wrote a simple class that works as a single-stream http server, issuing the current time and date, if you contact 127.0.0.1:
simple_server.h
#ifndef SIMPLE_SERVER_H #define SIMPLE_SERVER_H #include <QObject> #include <QTcpServer> #include <QTcpSocket> #include <QDateTime> class Simple_Server : public QTcpServer { Q_OBJECT public: explicit Simple_Server(QObject *parent = 0); void incomingConnection(qintptr handle); signals: public slots: void onReadyRead(); void onDisconnected(); }; #endif // SIMPLE_SERVER_H simple_server.cpp
#include "simple_server.h" Simple_Server::Simple_Server(QObject *parent) : QTcpServer(parent) { if(listen(QHostAddress::Any, 80)) { qDebug() << "listerning..."; } else { qDebug() << "error " << errorString(); } } void Simple_Server::incomingConnection(qintptr handle) { QTcpSocket *socket = new QTcpSocket(); socket->setSocketDescriptor(handle); connect(socket, SIGNAL(readyRead()), this, SLOT(onReadyRead())); connect(socket, SIGNAL(disconnected()), this, SLOT(onDisconnected())); } void Simple_Server::onReadyRead() { QTcpSocket * socket = qobject_cast<QTcpSocket*>(sender()); qDebug() << socket->readAll(); QString response = "HTTP/1.1 200 OK\r\n\r\n%1"; socket->write(response.arg(QDateTime::currentDateTime().toString()).toUtf8()); socket->disconnectFromHost(); } void Simple_Server::onDisconnected() { QTcpSocket * socket = qobject_cast<QTcpSocket*>(sender()); socket->close(); socket->deleteLater(); } The question is how to add different directories (probably this is the name) For example, so that when accessing 127.0.0.1/time, the server gives the time, on request 127.0.0.1/date, the server gives the date. How can I do that?
qDebug(), and for the described task you will still have to sort it out in pieces, what's written there. - D-side