Results 1 to 5 of 5

Thread: Server don't receive a new request of client

  1. #1
    Join Date
    May 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Server don't receive a new request of client

    Hi,

    I have a server and a client. The request client a information to the server. The first message received is answered by the server. But the second only is received after to disconnect and to connect again.


    It follow the source code.


    The client send request
    Qt Code:
    1. void ClientStub::sendRequest(const Message msg)
    2. {
    3. QByteArray block;
    4. QDataStream out(&block, QIODevice::WriteOnly);
    5. out.setVersion(QDataStream::Qt_4_6);
    6.  
    7. out << (quint16)0;
    8. out << msg;
    9.  
    10. out.device()->seek(0);
    11. out << (quint16)(block.size() - sizeof(quint16));
    12.  
    13. tcpSocket->write(block);
    14. tcpSocket->flush();
    15. sended++;
    16. }
    To copy to clipboard, switch view to plain text mode 


    The server receive and create a ClientSocket object to treat the connection
    Qt Code:
    1. void Server::incomingConnection(int socketId)
    2. {
    3. ClientSocket *socket = new ClientSocket();
    4. socket->setSocketDescriptor(socketId);
    5. }
    To copy to clipboard, switch view to plain text mode 


    ClientSockt code, it is exchange data with the client
    Qt Code:
    1. static int countClientSocket = 0;
    2.  
    3. ClientSocket::ClientSocket(QObject *parent)
    4. : QTcpSocket(parent)
    5. {
    6. connect(this, SIGNAL(readyRead()), this, SLOT(receive()));
    7. // connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater()));
    8.  
    9. countClientSocket++;
    10.  
    11. //#ifdef DEBUG_MACHINE
    12. printf("ctor countClientSocket: %i \n", countClientSocket);
    13. std::cout.flush();
    14. //#endif
    15.  
    16. nextBlockSize = 0;
    17.  
    18. }
    19.  
    20. ClientSocket::~ClientSocket()
    21. {
    22. // abort();
    23. countClientSocket--;
    24.  
    25. #ifdef DEBUG_MACHINE
    26. printf("dtor countClientSocket: %i \n", countClientSocket);
    27. std::cout.flush();
    28. #endif
    29.  
    30. }
    31.  
    32. bool ClientSocket::setSocketDescriptor(int socketDescriptor)
    33. {
    34. QTcpSocket::setSocketDescriptor(socketDescriptor);
    35. return waitForReadyRead(5);
    36. }
    37.  
    38. void ClientSocket::receive()
    39. {
    40. QDataStream in(this);
    41. in.setVersion(QDataStream::Qt_4_6);
    42.  
    43. if (nextBlockSize == 0)
    44. {
    45. if (bytesAvailable() < sizeof(quint16))
    46. return;
    47.  
    48. in >> nextBlockSize;
    49. }
    50.  
    51. if (bytesAvailable() < nextBlockSize)
    52. return;
    53.  
    54. Message msg;
    55. in >> msg;
    56.  
    57. invoke(msg);
    58. }
    59.  
    60. void ClientSocket::invoke(Message &msg)
    61. {
    62. QList<QVariant> msgContents = msg.getContents();
    63. WordCounter task = msgContents.first().value<WordCounter>();
    64.  
    65. msg.setTypes(Message::Info);
    66. msg.setContents(QList<QVariant>() << QString("received request"));
    67.  
    68. response(msg);
    69. printf("%i - received request\n", msg.getId());
    70.  
    71. QByteArray normalizedSignature = QMetaObject::normalizedSignature("execute (QList<QVariant> &)");
    72.  
    73. QVariant retVal;
    74. const QMetaObject *metaObject = 0;
    75. metaObject = task.metaObject();
    76. int methodIndex = -1;
    77. methodIndex = metaObject->indexOfMethod(normalizedSignature);
    78. QMetaMethod method = metaObject->method(methodIndex);
    79.  
    80. printf("Processing request of: %i \n", task.getKey());
    81.  
    82. QElapsedTimer timer;
    83. timer.start();
    84. method.invoke(
    85. &task,
    86. Qt::DirectConnection, // Qt::AutoConnection,
    87. Q_RETURN_ARG(QVariant, retVal),
    88. Q_ARG(QList<QVariant>, msgContents)
    89. );
    90. int t = timer.elapsed();
    91.  
    92. msg.setTypes(Message::Result);
    93. msg.setContents(QList<QVariant>() << retVal
    94. << QString("Time elapsed: %1 miliseconds").arg(t)); // << "done!");
    95. printf("id: %i, Result: %i \n--------&gt;>> Time elapsed: %i miliseconds\n", task.getKey(), retVal.toInt(), t);
    96.  
    97. response(msg);
    98.  
    99. // waitForDisconnected();
    100. // close();
    101. }
    102.  
    103. void ClientSocket::response(Message msg)
    104. {
    105. QByteArray block;
    106. QDataStream out(&block, QIODevice::WriteOnly);
    107. out.setVersion(QDataStream::Qt_4_6);
    108.  
    109. out << quint16(0);
    110. out << msg;
    111.  
    112. out.device()->seek(0);
    113. out << quint16(block.size() - sizeof(quint16));
    114.  
    115. write(block);
    116. flush();
    117. }
    To copy to clipboard, switch view to plain text mode 


    Thanks!!

  2. #2
    Join Date
    Aug 2008
    Posts
    45
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Server don't receive a new request of client

    Do you wait in the client side for response before sending next request?

    If you don't and just send requests as fast as you can, then in the server side the readyRead() signal might be emitted when there already is couple of requests worth of data waiting in the socket. The receive() slot handles only one request and doesn't check if there is more data available after invoking.

  3. #3
    Join Date
    May 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Server don't receive a new request of client

    Do you wait in the client side for response before sending next request?
    Yes, I do. I put a 'while' for wait the result arrive. The client only continue after.

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

    Default Re: Server don't receive a new request of client

    What kind of "while" did you put there?
    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.


  5. #5
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Server don't receive a new request of client

    If I am not mistaken, in the client code you use the condition nextBlockSize == 0 to determine in which of two states the reading operation is. However it seems that you never reset nextBlockSize to 0 after having read a message, therefore the size is only read for the first message. And, as joyer83 pointed out, you do not try to read more than one message.

    I would recommend using a separate boolean variable (or, better, a more descriptive enum) to keep track of the current state.

    Also, is there a firm guarantee that writing a quint16 to a QDataStream exactly fills the first sizeof(quint16) bytes (with no header beforehand)?

Similar Threads

  1. send request via ISA PROXY SERVER
    By rmagro in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2010, 22:56
  2. Server can't receive RawData
    By Tadas in forum Newbie
    Replies: 3
    Last Post: 19th September 2010, 23:04
  3. receive data from client via QTcpServer
    By Fallen_ in forum Qt Programming
    Replies: 4
    Last Post: 8th September 2010, 16:08
  4. Replies: 4
    Last Post: 18th August 2010, 08:13
  5. request for mail server
    By zakis in forum Qt Programming
    Replies: 0
    Last Post: 30th August 2009, 10:20

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.