Results 1 to 5 of 5

Thread: Simple Threaded Http Server

  1. #1
    Join Date
    Oct 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Simple Threaded Http Server

    Hello,

    I need a simple Http Server. I found the following example:
    http://doc.trolltech.com/solutions/4...le-server.html

    Until now, the Code is pretty much the same, but I created a for every incoming connection a QThread which has a QTcpSocket Object.

    My problem is: When I start my Server and open the ip and port with my browser, the browser should send a GET request which I want to read with the readClient() function. My problem is: I can write to my socket and send something back to the browser that is no problem, but there is never a readyRead() signal emitted and there is no data to read. There should be at least the GET request in the QTcpSocket or am I wrong? Is there anything I have to care about when I use Threads with QTcpSocket?

    I hope I could describe my problem. If not, I'll see that I could get some Code the next days... It's at work.

    Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Simple Threaded Http Server

    Do you remember about running an event loop in each of the threads?

    By the way, a thread-per-connection design is very inefficient. It's better to have a limited number of threads each handling a defined number of connections simultaneously.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Simple Threaded Http Server

    Okay here is some Code:

    Qt Code:
    1. void CHttpThread::run()
    2. {
    3. QTcpSocket socket;
    4. socket.setSocketDescriptor( m_socketDescriptor );
    5.  
    6. socket.waitForReadyRead( 3000 );
    7.  
    8. socket.close();
    9. connect( &socket, SIGNAL( disconnected() ), this, SLOT( quit() ) );
    10. connect( &socket, SIGNAL(readyRead()), this, SLOT(readClient()) );
    11. exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    Hope you could help me!
    Last edited by wysota; 20th October 2009 at 10:39.

  4. #4
    Join Date
    Oct 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Simple Threaded Http Server

    Finally it works, but:

    If i want to make it more efficient, the way would be to create a limited number of Threads, and in this Threads create a limited number of QTcpSocket Object for every connection?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Simple Threaded Http Server

    Have a pool of threads and a queue of connections. When a new connection comes in, assign it to one of idle threads if there are any or queue the connection so that when a thread becomes idle, it will pick it up and handle it. When a thread handles a predefined number of requests (say.... 200?) it dies and a new thread is spawned in its place. You can use the same model with processes instead of threads.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Sample of Http server
    By Lele in forum Qt Programming
    Replies: 2
    Last Post: 30th November 2010, 23:43
  2. Multi Threaded Client Server application
    By live_07 in forum Qt Programming
    Replies: 0
    Last Post: 27th August 2009, 16:32
  3. Trouble Applying the Fortune Threaded Server
    By dhice in forum Qt Programming
    Replies: 7
    Last Post: 18th April 2009, 22:04
  4. A simple HTTP server going wrong.
    By spraff in forum Qt Programming
    Replies: 1
    Last Post: 12th November 2008, 20:09
  5. Network Threaded server problem
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2008, 09:08

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.