Results 1 to 2 of 2

Thread: Multithreaded Server

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2010
    Posts
    6
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Multithreaded Server

    I'm writing a simple multithreaded server and I wind up with this code:

    Qt Code:
    1. ServerThread::ServerThread(int handle, QObject* parent)
    2. :QThread(parent)
    3. {
    4. m_descriptor = handle;
    5. init();
    6. }
    7.  
    8. void ServerThread::run()
    9. {
    10. m_socket = new QTcpSocket();
    11.  
    12. if(!m_socket->setSocketDescriptor(m_descriptor))
    13. {
    14. qDebug("Socket Error!");
    15. exit(-1);
    16. }
    17.  
    18. connect(m_socket, SIGNAL(readyRead()), this, SLOT(ready()),Qt::DirectConnection);
    19.  
    20. m_socket->waitForReadyRead(10000);
    21. }
    22.  
    23. void ServerThread::ready()
    24. {
    25. disconnect(m_socket, SIGNAL(readyRead()));
    26.  
    27. //Check incoming command
    28. if(m_socket->canReadLine())
    29. {
    30. QStringList tokens = QString(m_socket->readLine()).split(QRegExp("[\r\n][\r\n]*"));
    31.  
    32. switch(map.value(tokens[0]))
    33. {
    34. case GETA:
    35. doSomeThing();
    36. break;
    37. case GETB:
    38. doSomeThing();
    39. break;
    40. case QUIT:
    41. m_socket->disconnectFromHost();
    42. break;
    43. default:
    44. m_socket->disconnectFromHost();
    45. return;
    46. }
    47. }
    48. }
    To copy to clipboard, switch view to plain text mode 

    The above code works, but is it a good way to do it ?
    Last edited by wysota; 24th February 2010 at 17:31.

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

    Default Re: Multithreaded Server

    There is more than one path that leads to the same goal... I don't like the part where you read the tokens as you expect to always get one line at a time which is a false assumption. You should have a while(m_socket->canReadLine()) loop there. Also the splitting regexp seems odd.
    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. Multithreaded OpenGL
    By spraff in forum Qt Programming
    Replies: 2
    Last Post: 5th March 2010, 16:55
  2. Qt + gprof in a multithreaded app?
    By papercut in forum Qt Programming
    Replies: 0
    Last Post: 21st October 2009, 17:12
  3. Designing Multithreaded app in Qt
    By summer_of_69 in forum Qt Programming
    Replies: 3
    Last Post: 18th June 2009, 15:46
  4. Multithreaded spend CPU 100%
    By wisconxing in forum Qt Programming
    Replies: 1
    Last Post: 18th December 2008, 07:03
  5. multithreaded OpenGL Qt application
    By yuriy in forum Qt Programming
    Replies: 2
    Last Post: 1st September 2006, 17:54

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
  •  
Qt is a trademark of The Qt Company.