PDA

View Full Version : How to know when HTTPmessage has finished



bclay1297@gmail.com
29th May 2020, 14:12
I am using the http server example as a basic data server and QAccessManager in the test client application.

The client app contains the following code:

1, connect(mAccessManager, SIGNAL(finished(QNetworkReply *)), this, SLOT(FinishedPost(QNetworkReply*)));

2. if (postMsg == true)
3. {
4. mNetworkReply = mAccessManager->post(request, mPostMsgData);
5. }
6. else
7. {
8. mNetworkReply = mAccessManager->put(request, mPostMsgData);
9. }

10. if (mNetworkReply != NULL)
11. {
12. connect(mNetworkReply, SIGNAL(readyRead()), this, SLOT(NetworkReadyRead()));
13. // connect(mNetworkReply, SIGNAL(finished()), this, SLOT(FinishedRead()));

14. connect(mNetworkReply, SIGNAL(error(QNetworkReply::NetworkError)), this,
SLOT(NetworkErrors(QNetworkReply::NetworkError)));

I try either line 1 or line 13.

Neither finished is triggered unless the server sends an explicit message back not just an ACK.


In the server (httprequesthandler.cpp) I have the following code

connect(mTcpSocket, SIGNAL(readyRead()), this, SLOT(NetworkReadyRead()));
connect(mTcpSocket, SIGNAL(finished()), this, SLOT(FinishedRead()));
connect(mTcpSocket, SIGNAL(disconnected()), SLOT(Disconnected()));


FinishedRead is never called to tell me that the complete message has been received. I look at the length specified in the header and read until I get that many bytes. Several time however packets appear to be resent. I will get a complete file based on the size in the header and then I get more data sometime even the entire message all over again.

What handshaking has to be done to tell both apps when the message transfer is complete. the test message is a 40M tiff image. Is it possible that size is a limiting factor? For what its work we are using wired network connections not wireless so it is not a signal issue.


Bruce

ChrisW67
1st June 2020, 08:25
Please use
... tags around code. Easier to read, and you do not need to number lines yourself.

Typically the client will issue a finished() signal when the server closes the connection after sending the file.

Are you certain that you ever enter the code block at line 10?

bclay1297@gmail.com
1st June 2020, 13:06
Please use
... tags around code. Easier to read, and you do not need to number lines yourself.

Typically the client will issue a finished() signal when the server closes the connection after sending the file.

Are you certain that you ever enter the code block at line 10?

Chris:
Thank for the reply. Yes it is getting into that block. I followed in in with the debugger. This is a case where the client is sending data to the server to store. So in this case the server needs to know when it has received all of the data. It would be nice if the client knew when it was done sending the data as well but finished is only called in the client if the server sends back a reply. I modified the server code to track the incoming data and send a message as soon as the count is equal to what the header said then the server closes that TCP socket. Not sure if that is the right way but it does stop the redundant messages. None of the examples I could find online used the header information so I cannot tell how others are getting the end of data message. If the client knew when it was finished it could close the connection but that does not seem to the the case here.

Bruce

ChrisW67
1st June 2020, 23:25
Server and client are somewhat arbitrary. Perhaps I should word it this way, in the simple case the receiver knows the transmission is finished when the sender closes the socket (see Fortune Server Example). At its simplest HTTP operates that way. In practice though, network socket connections are kept open and the higher level protocol tracks the start and end of transactions.
I do not know which "http server example" you are referring to. There isn't one here (https://doc.qt.io/qt-5/examples-network.html). Can you provide a link?

bclay1297@gmail.com
2nd June 2020, 22:46
Server and client are somewhat arbitrary. Perhaps I should word it this way, in the simple case the receiver knows the transmission is finished when the sender closes the socket (see Fortune Server Example). At its simplest HTTP operates that way. In practice though, network socket connections are kept open and the higher level protocol tracks the start and end of transactions.
I do not know which "http server example" you are referring to. There isn't one here (https://doc.qt.io/qt-5/examples-network.html). Can you provide a link?

Chris:

The server sample is at https://github.com/qt-labs/qthttpserver.

Since the app I am calling the clientdoes not seem to know when it has finished sending the file I cannot close from that end. I switched the server to use console mode and notices that QTcpSocket does not have a finished slot so that explains that end. the Server only gets the finished notification when the server sends back a status message.

Bruce

ChrisW67
3rd June 2020, 11:18
QTcpSocket has a closed() signal, which the server can use to detected the other end closing the connection.

I assume the client and its behaviour is under your control. If you are not getting a finished() then there could be something wrong with the PUT or POST transaction and its payload. Are you getting an error signal?
What is going in mPostMsgData and how is it constructed?

Can you post a small, self-contained example that fails?

bclay1297@gmail.com
3rd June 2020, 15:57
Chris:
I pushed a small test client up to https://gitlab.com/bclay/http-test-client. I left code segments in that should support https that relate to a previous problem I reported but that code is commented out. Once I see what is wrong here I would like to get back to that. It also is using the same qt lab httpserver

Bruce