Results 1 to 5 of 5

Thread: QTcpSocket - Cannot create children for a parent that is in a different thread

  1. #1
    Join Date
    Apr 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default QTcpSocket - Cannot create children for a parent that is in a different thread

    Hello,
    I am new to QT.
    In my project I have QTcpServer with multithreading. I need to implement that when server recieve something from one client(client is GUI aplication), send it to all clients except the one who sent data. Problem occurs when I call socket->write(data) in function sendToAllClients() in server.cpp. I got following:
    QObject:Cannot create children for the parent that is in different thread qt.

    Could anyone please help me with this. I was searching about on google, but whatever I tried it doesn't work.
    Thanks a lot.


    My code is below:

    mythread.cpp
    Qt Code:
    1. MyThread::MyThread(qintptr ID, QObject *parent) :
    2. QThread(parent)
    3. {
    4. this->socketDescriptor = ID;
    5. this->disconnectedSocket=-1;
    6. }
    7.  
    8. void MyThread::run()
    9. {
    10. socket = new QTcpSocket();
    11.  
    12. if(!socket->setSocketDescriptor(this->socketDescriptor))
    13. {
    14. emit error(socket->error());
    15. return;
    16. }
    17.  
    18. connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
    19. connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
    20.  
    21. exec();
    22. }
    23.  
    24. void MyThread::readyRead()
    25. {
    26. readyToRead=true;
    27.  
    28. if(readyToRead==true)
    29. emit ReadyToReadAllSockets(); //ReadyToReadAllSockets() is signal I wrote to make connection between thread and server.
    30.  
    31. }
    To copy to clipboard, switch view to plain text mode 

    server.cpp

    Qt Code:
    1. Server::Server(QObject *parent) :
    2. QTcpServer(parent)
    3. {
    4. connect(this,SIGNAL(newConnection()),this,SLOT(numOfClients()));
    5.  
    6.  
    7. }
    8.  
    9. void Server::incomingConnection(qintptr socketDescriptor)
    10. {
    11. clients_list.append(socketDescriptor);
    12. MyThread *thread = new MyThread(socketDescriptor, this);
    13. threads.append(thread);
    14. connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    15. connect(thread, SIGNAL(finished()), this, SLOT(clientDisconnect()));
    16. connect(thread, SIGNAL(finished()), this, SLOT(removeClientFromList()));
    17. connect(thread, SIGNAL(ReadyToReadAllSockets()), this, SLOT(sendToAllClients()));
    18.  
    19.  
    20. thread->start();
    21.  
    22. }
    23.  
    24. void Server::sendToAllClients()
    25. {
    26.  
    27. qDebug()<< "Writing in sockets....";
    28. MyThread* thread;
    29. QByteArray Data;
    30.  
    31.  
    32. for(int i=0;i<threads.size();i++)
    33. {
    34. if(threads.at(i)->getIfReady())
    35. {
    36.  
    37. thread=threads.at(i);
    38. Data = threads.at(i)->getSocket()->readAll();
    39. threads.at(i)->setReady(false);
    40.  
    41. }
    42. }
    43.  
    44. for(int i=0;i<threads.size();i++)
    45. {
    46. if(threads.at(i)!=thread)
    47. {
    48. socket=threads.at(i)->getSocket();
    49. socket->write(Data); // this is where occur error
    50.  
    51. }
    52. }
    53.  
    54. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Begginer; 16th April 2016 at 23:40.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket - Cannot create children for a parent that is in a different thread

    This is very wrong even if you hadn't gotten an error.

    There is no protection what so ever against concurrent access of either the sockets or the readyToRead flag (assuming setReady changes that).

    If the idea is to use cross-thread signal/slots to savely transfer data between threads, then you need to refrain from accessing data directly.

    If you want to do it with direct method calls, these methods need to use appropriately synchronized data access procedures.

    In general you should make sure that you really need this to be multithreaded.

    Cheers,
    _

  3. #3
    Join Date
    Apr 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QTcpSocket - Cannot create children for a parent that is in a different thread

    Could you please give me some guidelines, how can I implement that server can communicate with multiple clients simultaneosly, and that server can comunicate with all clients, without using threads..And what is the best way to solve this type of project.. My client is GUI aplication, and server needs to be console application.. I started learning QT before 1 week, and I don't know a lot about what I can do, and which way is the most correct.

    Sorry if my english is not good, I hope you understand me.
    Thanks you a loot.
    Best regards

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket - Cannot create children for a parent that is in a different thread

    If you don't do any heavy processing on the client data you should be able to do this single threaded, which makes it a lot easier to achieve.

    My suggestion would be:
    - use a client handler object, a bit like now but not deriving from QThread, just from QObject.
    - this class has a signal that is emitted when data arrives on the respective client connection. A bit like you do now, but actually having the data as the signal argument
    - this class also has a slot with a compatible signature, that sends the given data to the client

    - your server needs a list of these objects

    - when you get a new incoming connection, create such a client handler instance
    - iterate over the list and connect each list entry's signal with the new objects's slot and the new object's signal with the list entry's slot
    - add the new object to the list

    So when data comes in on any connection, it is emitted by that connection's handler.
    All other handlers are connected to that signal and will send the data to their clients.

    Cheers,
    _

  5. #5
    Join Date
    Apr 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QTcpSocket - Cannot create children for a parent that is in a different thread

    Thanks you a lot, I fixed that and it works now...
    Best regards.

Similar Threads

  1. Replies: 4
    Last Post: 26th January 2016, 14:25
  2. Replies: 6
    Last Post: 22nd December 2011, 21:03
  3. Replies: 3
    Last Post: 19th March 2011, 15:01
  4. Replies: 6
    Last Post: 8th July 2009, 13:24
  5. Replies: 4
    Last Post: 1st May 2009, 11:00

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.