Important: Please read the Qt Code of Conduct -

problem of qsocketnotifer in qtcpserver

  • i am designing qtcpserver for sending message from one client to another client for that:
    1.push the all socket(created in myserver class i.e. main thread)into qvector.{in thread 1} the data from thread 2.
    3.want to write data also from thread 2.
    the problem is that because i am creating socket in thread 1 and writing by
    clients[i].write("hello"); from thread 2 i am getting notification of

    qsocketnotifier socket notifiers cannot be enabled or disabled from another thread 

    i know the reason is that i am writing it from thread 2 instead of thread 1.but i want to read and write the data from same thread.
    so Is there any way to define socket in thread2 so i don't get this notification.but i also dont want to create thread eveytime when socket created

  • @JadeN001
    You do indeed want to do all all socket create/read/writes from same thread.

    So either:

    • do the socket creation in same thread as read/write (probably simplest); or
    • pass messages (signal/slot) between threads; or
    • maybe QObject::moveToThread after creating socket to push to desired read/write thread (I see you have asked about QObject::moveToThread many times)


  • @JonB after creating socket push to desire thread in movetothread , this I have done already.
    But I have created socket (more than one) in main thread and I want to use a separate thread for reading and writing .

  • @JadeN001
    I believe you have the 3 alternatives I suggested, as per the error message you report.

    Since you have kept the creation thread different from the read/write thread, you have problems.

    If you are already using moveToThread() at some point, I cannot see why you can't move the socket to the correct reading/writing thread.

  • Lifetime Qt Champion

    There were similar / same questions the last days. Please use the search function.

  • @JonB I can move the socket and i can also use for reading and writing.but the socket is created in main thread and so it is registered by qsocketnotifer in main thread and i am using that socket in different thread(read/write thread).
    thats why i am getting notification

  • Lifetime Qt Champion


    Why are you trying to do the read and write operations in different threads ?

  • @JadeN001
    You keep saying that your creation thread is separate from your read/write threads. Since that is what is causing the problem, as @SGaist is asking/suggesting why don't you redesign code so that they are not in different threads? Anyway, answer @SGaist 's question.

Log in to reply