Results 1 to 2 of 2

Thread: High performance Http Server

  1. #1
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default High performance Http Server

    Hi,

    I want to write a high performance http server. The attached code is what I came up with so far.
    Benchmarking showed that it serves just a little more than an apache http server - while apache does a lot more.

    Any suggestions on how I could improve this?

    thanks,
    Niko

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QTcpServer>
    3. #include <QThreadPool>
    4. #include <QDateTime>
    5. #include <QTcpSocket>
    6. #include <QStringList>
    7.  
    8. class Request : public QRunnable
    9. {
    10. public:
    11. Request (int socketDescriptor)
    12. : m_socketDescriptor(socketDescriptor)
    13. {}
    14.  
    15. virtual void run()
    16. {
    17. QTcpSocket socket;
    18. socket.setSocketDescriptor(m_socketDescriptor);
    19. socket.waitForReadyRead();
    20. QStringList tokens = QString(socket.readLine()).split(QRegExp("[ \r\n][ \r\n]*"));
    21. if (tokens[0] == "GET") {
    22. QTextStream os(&socket);
    23. os.setAutoDetectUnicode(true);
    24. os << "HTTP/1.0 200 Ok\r\n"
    25. "Content-Type: text/html; charset=\"utf-8\"\r\n"
    26. "\r\n"
    27. "<h1>Nothing to see here</h1>\n"
    28. << QDateTime::currentDateTime().toString() << "\n";
    29. socket.close();
    30. socket.waitForDisconnected();
    31. }
    32. }
    33.  
    34. private:
    35. int m_socketDescriptor;
    36. };
    37.  
    38. class HttpDaemon : public QTcpServer
    39. {
    40. public:
    41. HttpDaemon(quint16 port, QObject* parent = 0)
    42. : QTcpServer(parent)
    43. {
    44. if (!listen(QHostAddress::Any, port)) {
    45. qFatal(errorString().toAscii());
    46. }
    47. QThreadPool::globalInstance()->setMaxThreadCount(10);
    48. }
    49.  
    50. private:
    51. void incomingConnection(int socket)
    52. {
    53. Request *r = new Request(socket);
    54. QThreadPool::globalInstance()->start(r);
    55. }
    56. };
    57.  
    58. int main(int argc, char** argv)
    59. {
    60. QCoreApplication app(argc, argv);
    61. HttpDaemon daemon(8080);
    62. return app.exec();
    63. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2009
    Posts
    37
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: High performance Http Server

    The code looks fine to me.
    However, I would not expect to be able to use Qt for a high performance webserver. You'll find a lot of highly optimized webservers on sites like freshmeat. They are using very low level network and IO operations to achieve this. You just can't get to that level of performance with a cross platform library that needs to layer/abstract things.
    Disclaimer: Although I work on Qt for Nokia, anything I post here is personal

  3. The following user says thank you to mgoetz for this useful post:

    niko (11th December 2009)

Similar Threads

  1. Sample of Http server
    By Lele in forum Qt Programming
    Replies: 2
    Last Post: 30th November 2010, 23:43
  2. Simple Threaded Http Server
    By obi in forum Qt Programming
    Replies: 4
    Last Post: 20th October 2009, 10:42
  3. A simple HTTP server going wrong.
    By spraff in forum Qt Programming
    Replies: 1
    Last Post: 12th November 2008, 20:09
  4. Poor performance with Qt 4.3 and Microsoft SQL Server
    By Korgen in forum Qt Programming
    Replies: 2
    Last Post: 23rd November 2007, 10:28
  5. Replies: 1
    Last Post: 4th October 2006, 16:05

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.