[Solved] QTcpServer and event loop



  • Hi,

    I'm currently working on a network application with a server and a client but I'm stuck with a problem.

    The client send a socket (QTcpSocket) to the server which gets it with the help of a QTcpServer: when readyRead() is emitted, a slot is called:

    @void Class::read()
    {
    QDataStream in(_socket);

    // [Getting data] ...
    
    _server->handleMessage(&in, this);
    
    // ...
    

    }@

    In this slot, everything is ok, but if handleMessage() block the main loop with a long operation, the problem happen: when the client ask for a message, readyRead() is called after it ask a second message. To clarify, he schema is the following after the problem happen in the first time:

    Client 1° demand => not any answer
    Client 2° demand => receive 1° demand's answer
    Client 3° demand => receive 2° demand's answer
    ...

    After some researchs and tests, I understood that it's a problem of event loop. handleMessage() blocks it for a moment, that makes the readyRead() signal is "queued", and so not received immediatly.

    So my question is the following: what do you recommend ? To use a thread for handleMessage() ? To use QCoreApplication::processEvents() all over handleMessage() ? Other ?

    Thanks,
    Amnell.

    EDIT: to reproduce this problem, send two (or more) sockets at the same time from the same client and do a long action. The latency will grow to seconds on local network and the behavior will be like described above. A solution I just found is... not to send two sockets at the same time. But is there any way to do it?



  • For every client connection you usually have a separate socket on the server side. It seems that you mixup something when your clients get the wrong answers. If you send two messages from the same client, then your protocol seems inappropriate. You should either block in the client until a pending request has been answered or add some sort of sequence or uid to identifiy the related data.

    If you have really long running operations, putting those into a separate thread could be an option. Be sure to read peppes excellent wiki article on "Threads, Events and QObjects":http://developer.qt.nokia.com/wiki/Threads_Events_QObjects to avoid common mistakes. And be aware that this will not solve your problem of wrongly ordered responses!



  • [quote author="Volker" date="1314223084"]For every client connection you usually have a separate socket on the server side. It seems that you mixup something when your clients get the wrong answers. If you send two messages from the same client, then your protocol seems inappropriate. You should either block in the client until a pending request has been answered or add some sort of sequence or uid to identifiy the related data.[/quote]

    To block the client would be inappropriate because the two - or more (depends on what the user does) - sockets are sent "in background" in way to load data, prepare fields, things like that. But to make a sequence should be correct for what I want to do. With some rewrites, it should finally works as expected.

    [quote author="Volker" date="1314223084"]If you have really long running operations, putting those into a separate thread could be an option. Be sure to read peppes excellent wiki article on "Threads, Events and QObjects":http://developer.qt.nokia.com/wiki/Threads_Events_QObjects to avoid common mistakes. And be aware that this will not solve your problem of wrongly ordered responses![/quote]

    This article sounds very nice; I'll read it tomorrow :)

    I think the topic can be marked as solved, thank you very much for your help!
    Amnell.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.