Results 1 to 18 of 18

Thread: Write to Threaded Server

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2010
    Posts
    13
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Write to Threaded Server

    Actually writing to the client does work, but with annoying messages.
    When I try to write a reply immediately after receiving a message. (For debug purpose it is solved with a connect.)
    Then I got always "QObject: Cannot create children for a parent that is in a different thread." But reply message is send anyway. So i could not care about, but its a kind of distrubing. This error appears with and without moving to thread. Is there a way to fix this problem?

    serverthread.cpp
    Qt Code:
    1. #include "serverthread.h"
    2.  
    3. ServerThread::ServerThread(int socketDescriptor, QObject *parent)
    4. : QThread(parent)
    5. , m_socketDescriptor(socketDescriptor)
    6. {
    7. qDebug() << currentThreadId ();
    8. }
    9.  
    10. ServerThread::~ServerThread ()
    11. {
    12. stop();
    13. }
    14.  
    15. void ServerThread::stop()
    16. {
    17. exit();
    18. }
    19.  
    20. void ServerThread::run()
    21. {
    22. serverSocket = new ServerTcpSocket();
    23.  
    24. if (!serverSocket->setSocketDescriptor(m_socketDescriptor)) {
    25. emit error(serverSocket->error(), serverSocket->errorString());
    26. qDebug() << "setSocketDescriptor error : " << serverSocket->error() << endl;
    27. return;
    28. }
    29.  
    30. connect(serverSocket, SIGNAL(dataAvailable()), this, SLOT(writeData()));
    31.  
    32. exec();
    33. }
    34.  
    35. void ServerThread::writeData()
    36. {
    37. serverSocket->moveToThread(currentThread());
    38. //QObject::moveToThread: Current thread (0x1b102fc0) is not the object's thread (0x1b01b6a0).
    39. //Cannot move to target thread (0x1b01b6a0)
    40.  
    41. m_mutex.lock();
    42.  
    43. QString msg = "Message from Server\n";
    44. qint64 blockSize = msg.size();
    45.  
    46. serverSocket->write(msg.toAscii().data(),blockSize);
    47. // QObject: Cannot create children for a parent that is in a different thread.
    48. //(Parent is QNativeSocketEngine(0x103df1b0), parent's thread is ServerThread(0x103c2910), current thread is QThread(0x101966a0)
    49. m_mutex.unlock();
    50. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jun 2010
    Posts
    13
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile Solved - maybe not nice but it works!!!

    Solved it finally!
    Now writing and reading are running in one different thread

    I'm thankful for every improvement.

    Now the Source code:
    multiserver.cpp
    Qt Code:
    1. #include "multiserver.h"
    2.  
    3. MultiServer::MultiServer(QObject *parent)
    4. : QTcpServer(parent)
    5. {
    6. }
    7.  
    8. MultiServer::~MultiServer()
    9. {
    10. delete server;
    11. }
    12.  
    13. void MultiServer::incomingConnection(int socketDescriptor)
    14. {
    15. qDebug() << "MultiServer thread " << QThread::currentThreadId ();
    16.  
    17. server = new ServerThread(socketDescriptor);
    18. connect(server, SIGNAL(finished()), server, SLOT(deleteLater()));
    19. server->start();
    20. }
    To copy to clipboard, switch view to plain text mode 

    serverthread.cpp
    Qt Code:
    1. #include "serverthread.h"
    2.  
    3. ServerThread::ServerThread(int socketDescriptor)
    4. , m_socketDescriptor(socketDescriptor)
    5. {
    6. QObject::moveToThread(this); //destroy the obsolete thread
    7. qDebug() << "ServerThread " << QThread::currentThreadId ();
    8. }
    9.  
    10. ServerThread::~ServerThread ()
    11. {
    12. stop();
    13. delete serverSocket;
    14. }
    15.  
    16. void ServerThread::stop()
    17. {
    18. this->quit();
    19. this->wait();
    20. }
    21.  
    22. void ServerThread::run()
    23. {
    24. serverSocket = new ServerTcpSocket();
    25.  
    26. if (!serverSocket->setSocketDescriptor(m_socketDescriptor)) {
    27. emit error(serverSocket->error(), serverSocket->errorString());
    28. qDebug() << "setSocketDescriptor error : " << serverSocket->error() << endl;
    29. return;
    30. }
    31.  
    32. connect(serverSocket, SIGNAL(dataAvailable()), this, SLOT(writeData()));
    33.  
    34. this->exec();
    35. }
    36.  
    37. void ServerThread::writeData()
    38. {
    39.  
    40. qDebug() << "writing thread" << currentThreadId ();
    41.  
    42. QString msg = "Message from Server\n";//Todo just for debug purpose
    43. qint64 blockSize = msg.size();//Todo just for debug purpose
    44.  
    45. serverSocket->write(msg.toAscii().data(),blockSize);
    46. }
    To copy to clipboard, switch view to plain text mode 

    servertcpsocket.cpp
    Qt Code:
    1. #include "servertcpsocket.h"
    2.  
    3. ServerTcpSocket::ServerTcpSocket(QObject *parent)
    4. : QTcpSocket(parent)
    5. {
    6. buffer = new QBuffer();
    7. buffer->open(QIODevice::ReadWrite);
    8.  
    9. connect(this, SIGNAL(readyRead()), this, SLOT(readData()));
    10.  
    11. connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater()));
    12. }
    13.  
    14.  
    15. void ServerTcpSocket::readData()
    16. {
    17. qDebug() << "reading thread" << QThread::currentThreadId ();
    18. qint64 bytes = buffer->write(this->readAll());
    19. buffer->seek(buffer->pos() - bytes);
    20. while (buffer->canReadLine())
    21. {
    22. QString line = buffer->readLine();
    23. qDebug() << line;
    24. }
    25.  
    26. emit dataAvailable();
    27. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Simple Threaded Http Server
    By obi in forum Qt Programming
    Replies: 4
    Last Post: 20th October 2009, 10:42
  2. Network Threaded server problem
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2008, 09:08
  3. Threaded TCP server
    By Sysace in forum Newbie
    Replies: 4
    Last Post: 21st February 2008, 12:37
  4. Threaded server.
    By nithinin2001 in forum Qt Programming
    Replies: 2
    Last Post: 15th November 2007, 16:37

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