【问题标题】:Qt HTTP Server? [closed]Qt HTTP 服务器? [关闭]
【发布时间】:2011-03-08 12:59:59
【问题描述】:

我想编写一个小型 HTTP 服务器应用程序,它接收 HTTP GET 请求、处理它们并发出回复。由于应用程序的历史,我倾向于使用 Qt,但我只能找到另一个(更常见的)方向:向服务器发送请求并使用 QNetworkAccessManager 接收回复。我需要的是一个类似于套接字的东西,当一个请求进来时,它会产生一个对象,我可以在其中选择这个请求的 url 等,这样我就可以发出适当的回复。

我只是盲目还是在 Qt 框架中没有这样的东西?如果是这样,您能推荐替代品吗?

【问题讨论】:

    标签: qt web-services qt4


    【解决方案1】:

    我找到了这个 https://github.com/vinipsmaker/tufao , 这是一个迟到的答案..不确定它是否有帮助。

    【讨论】:

    • 其实已经晚了,不过这个项目好像就是我当时要找的东西。
    【解决方案2】:

    QtWebApp 是一个支持 GET 和 POST 方法、cookie、会话和文件上传的 HTTP 服务器。使用这个库就像编写 Java Servlet 一样简单。

    项目网站是德文的,但可下载的文件都是英文的,包括文档。

    【讨论】:

    【解决方案3】:

    我刚刚发布了QHttpEngine 的第一个版本,旨在填补您所描述的空白。该项目的目标是提供一组极其简单的类,以提供与 Qt 集成的 HTTP 服务器。

    例如,要从应用程序的资源中提供静态文件,您需要做的就是:

    QFilesystemHandler handler(":/www");
    QHttpServer server(&handler);
    
    server.listen(QHostAddress::LocalHost, 8000);
    

    您还可以创建一个派生自QObject 的类,将其槽公开为 HTTP API 中的端点。例如:

    class ApiHandler : public QObjectHandler
    {
        Q_OBJECT
    
    private slots:
    
        QVariantMap doSomething(const QVariantMap &params) {
            // do something with the parameters and return a response
        }
    };
    

    然后,用户可以使用编码为 JSON 的参数向/doSomething 发送 POST 请求,并接收插槽生成的响应。

    整个库都有完整的文档记录,并附带一个相当详尽的测试套件。它是跨平台的,在 Linux、Windows 和 Mac OS X 上经过官方测试和支持。至少有一个主要的开源应用程序使用 QHttpEngine,NitroShare

    【讨论】:

      【解决方案4】:

      这是一个非常简单的 HTTP 网络服务器,它会在网络浏览器上建立连接后每秒更新秒数。 它还在屏幕上显示网络浏览器发送的数据。 程序设置为使用8080端口 例如 127.0.0.1:8080

      #-------------- Project file webServer3.pro ------- 
      QT       += core
      QT       += network
      QT       -= gui
      
      TARGET = webServer3
      CONFIG   += console
      CONFIG   -= app_bundle
      
      TEMPLATE = app
      
      
      SOURCES += main.cpp
      
      HEADERS += \
      myhttpserver.h
      
      /*------------------header file myhttpserver.h --------------*/ 
      #ifndef MYHTTPSERVER
      #define MYHTTPSERVER
      #include <QCoreApplication>
      #include <QNetworkInterface>
      #include <iostream>
      #include <QObject>
      #include <QTcpSocket>
      #include <QTcpServer>
      #include <QDebug>
      
      class myHTTPserver : public QObject
      {
          Q_OBJECT
      public:
          explicit myHTTPserver(QObject *parent = 0);
          ~myHTTPserver();
          QTcpSocket *socket ;
      public slots:
          void myConnection();
      private:
          qint64 bytesAvailable() const;
          QTcpServer *server;
      signals:
      };
      
      
      /*------------------------main.cpp -------------------------*/
      #include "myhttpserver.h"
      
      using namespace std;
      void delayms( int millisecondsToWait );
      
      int main(int argc, char *argv[])
          {
          QCoreApplication a(argc, argv);
          myHTTPserver server;
          return a.exec();
      }
      
      myHTTPserver::myHTTPserver(QObject *parent) : QObject(parent)
          {
           server = new QTcpServer(this);
          // waiting for the web brower to make contact,this will emit signal
          connect(server, SIGNAL(newConnection()),this, SLOT(myConnection()));
          if(!server->listen(QHostAddress::Any,8080))cout<< "\nWeb server      could not start";
          else cout<<"\nWeb server is waiting for a connection on port 8080";
      }
      
      void myHTTPserver::myConnection()
          {
          static qint16 count;  //count number to be displayed on web browser
          socket = server->nextPendingConnection();
          while(!(socket->waitForReadyRead(100)));  //waiting for data to be read from web browser
      
          char webBrowerRXData[1000];
          int sv=socket->read(webBrowerRXData,1000);
          cout<<"\nreading web browser data=\n";
          for(int i=0;i<sv;i++)cout<<webBrowerRXData[i];
          cout<<"\n";
      
          socket->write("HTTP/1.1 200 OK\r\n");       // \r needs to be before \n
          socket->write("Content-Type: text/html\r\n");
          socket->write("Connection: close\r\n");
          socket->write("Refresh: 1\r\n\r\n");     //refreshes web browser     every second. Require two \r\n.
      
          socket->write("<!DOCTYPE html>\r\n");
          socket->write("<html><body>Number of seconds since connected.. ");
          QByteArray str;
          str.setNum(count++);   //convert int to string
          socket->write(str);
          socket->write(" </body>\n</html>\n");
      
          socket->flush();
          connect(socket, SIGNAL(disconnected()),socket, SLOT(deleteLater()));
          socket->disconnectFromHost();
      }
      
      myHTTPserver::~myHTTPserver()
          {
          socket->close();
      }
      

      【讨论】:

        【解决方案5】:

        QttpServer = Qt + libuv + REST = 你的 API 服务器

        对于需要处理large number of concurrent connections、请求和你有什么的Qt API 服务器...我们有 libuv 库。

        虽然 libuv 是为 NodeJS 构建的,但我们 Qt 领域的粉丝仍然可以从 epolls 和 kqueues 而不是默认的 select 方法中受益。

        在 github 上查看更多信息https://github.com/supamii/QttpServer

        【讨论】:

        • 这是最好的答案......在调查了许多解决方案之后。这个解决方案很简单,基于正确的技术 + 得到了干净和简单的例子。现在去测试一下...
        【解决方案6】:

        QhttpServer 似乎完全符合您的要求。 不幸的是,它似乎不是很活跃。

        【讨论】:

          【解决方案7】:

          这是一个简单的 Qt 服务器,可以从 QML 中使用,纯 JS HTTP 处理:https://github.com/ncp1402/ql-server

          【讨论】:

            【解决方案8】:

            上述简单服务器在 MSwindows 操作系统上运行时出现问题,因为它会在一段时间后锁定。这是因为 waitForReadyRead() 在 MS 系统上以随机方式运行。 waitForReadyRead() 下面的程序被使用 readyRead() 信号的事件循环替换。

            # Project file
            QT       += core
            QT       += network
            QT       -= gui
            TARGET = webServer
            CONFIG   += console
            CONFIG   -= app_bundle
            TEMPLATE = app
            SOURCES += main.cpp
            HEADERS += myhttpserver.h
            
            //-----------myhttpserver.h--------------   
            #ifndef MYHTTPSERVER
            #define MYHTTPSERVER
            #include <QCoreApplication>
            #include <iostream>
            #include <QObject>
            #include <QTcpSocket>
            #include <QTcpServer>
            #include <QIODevice>
            
            class myHTTPserver : public QObject
            {
                Q_OBJECT
            public:
                explicit myHTTPserver(QObject *parent = 0);
                ~myHTTPserver();
                QTcpSocket *socket ;
            public slots:
                void myConnection();
                void txRx();
                void closingClient();
            private:
                qint64 bytesAvailable() const;
                QTcpServer *server;
            };
            #endif // MYHTTPSERVER
            
            //------------------------ main.cpp ----------------------  
            #include "myhttpserver.h"
            
            using namespace std;
            
            int main(int argc, char *argv[])
                {
                QCoreApplication a(argc, argv);
                myHTTPserver server;
                return a.exec();
            }
            
            myHTTPserver::myHTTPserver(QObject *parent) : QObject(parent)
                {
                server = new QTcpServer(this);
                connect(server, SIGNAL(newConnection()),this, SLOT(myConnection()));
                if(!server->listen(QHostAddress::Any,8080))cout<< "\nWeb server     could not start";
                else cout<<"\nWeb server is waiting for a connection on port 8080";
            }
            
            void myHTTPserver::myConnection()
                {
                socket = server->nextPendingConnection();
                connect(socket, SIGNAL(readyRead()), this, SLOT(txRx()));
                connect(socket, SIGNAL(disconnected()), this, SLOT(closingClient()));
            }
            void myHTTPserver::txRx()
                {
                char webBrowerRXData[1000];
                int sv=socket->read(webBrowerRXData,1000);
                cout<<"\nreading web browser data\n";
                for(int i=0;i<sv;i++)cout<<webBrowerRXData[i];
                cout<<"\n";
            
                socket->write("HTTP/1.1 200 OK\r\n");       // \r needs to be before \n
                socket->write("Content-Type: text/html\r\n");
                socket->write("Connection: close\r\n");
                socket->write("Refresh: 1\r\n");     //refreshes web browser every second. Require two \r\n.
                socket->write("Pragma: no-cache\r\n");
                socket->write("\r\n");
                socket->write("<!DOCTYPE html>\r\n");
                socket->write("<html><body>Number of seconds since connected.. ");
                QByteArray str;
                static qint16 count;  //count number to be displayed on web browser
                str.setNum(count++);   //convert int to string
                socket->write(str);
                socket->disconnectFromHost();
            }
            
            void myHTTPserver::closingClient()
                {
                    socket->deleteLater();
            }
            
            myHTTPserver::~myHTTPserver()
                {
                cout<<"\nclosing socket\n";
                socket->close();
            }
            

            【讨论】:

              猜你喜欢
              • 2017-03-31
              • 2023-03-25
              • 2013-05-03
              • 2011-07-12
              • 2019-07-29
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多