Results 1 to 15 of 15

Thread: Qnetworkaccessmanager and qnetworkreply are returning an error "connection closed"

  1. #1
    Join Date
    May 2015
    Posts
    26
    Thanks
    11
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Qnetworkaccessmanager and qnetworkreply are returning an error "connection closed"

    Hi,
    I am developing a plugin for to transfer a XML file over Http protocol on top of the TCP layer.
    I am Able to send the XML body from Client to the Server using Networkaccessmanager and I can able to receive both the header and body at server.
    But the issue is am getting an error saying "Connection Closed" from the NetworkReply Even though all my data is transferred to the server.
    And also sometimes Server will get called 2 times also for the same Build.
    and also sometimes at server side, data will not be received .

    Please check my code below.

    /* Client_cs.cpp */


    Qt Code:
    1. Client_CS::Client_CS(QObject* parent): QObject(parent)
    2. { qDebug() <<"data starting";
    3. client_socket.connectToHost("127.0.0.1", 80);
    4. connect(&client_socket, SIGNAL(connected()), this, SLOT(startTransfer()));
    5. }
    6.  
    7. Client_CS::~Client_CS()
    8. {}
    9.  
    10. void Client_CS::startTransfer()
    11. { QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    12. QHttpPart filePart;
    13. filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/xml"));
    14. filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; filename=\"text\""));
    15. QFile *file = new QFile(xmllink);
    16. if ( !file->exists() )
    17. { qDebug() << "File Does not exist";
    18. }
    19. file->open(QIODevice::ReadOnly);
    20. filePart.setBodyDevice(file);
    21. file->setParent(multiPart);
    22. multiPart->append(filePart);
    23. QUrl url("http://127.0.0.1:80");
    24. QNetworkRequest request(url);
    25. pManager = new QNetworkAccessManager();
    26. pReply = pManager->post(request, multiPart);
    27. multiPart->setParent(pReply);
    28. QObject::connect(pReply, SIGNAL(finished()),this, SLOT(replyFinished())); // here is the issue
    29. client_socket.close();
    30. }
    31.  
    32.  
    33. void Client_CS::replyFinished()
    34. { if ( pReply->error() > 0 )
    35. { qDebug() << "Error occurred: " << pReply->error() << " : " << pReply->errorString();
    36. }
    37. else
    38. { qDebug() << "Upload success";
    39. }
    40. pReply->deleteLater();
    41. }
    To copy to clipboard, switch view to plain text mode 


    /* Server_CS.cpp */


    Qt Code:
    1. Server_CS::Server_CS(QObject* parent): QObject(parent)
    2. {
    3. qDebug() << "server initiated";
    4. QHostAddress hostadd(QHostAddress::Any);
    5. cs_server.listen(hostadd, 80);
    6. connect(&cs_server, SIGNAL(newConnection()),this, SLOT(acceptConnection()));
    7. }
    8.  
    9. Server_CS::~Server_CS()
    10. {
    11. cs_server.close();
    12. }
    13.  
    14. void Server_CS::acceptConnection()
    15. {
    16. _cs_Socket = new QTcpSocket(this);
    17. qDebug() << "server got new connection";
    18. _cs_Socket = cs_server.nextPendingConnection();
    19. connect(_cs_Socket, SIGNAL(readyRead()),this, SLOT(startRead()));
    20. }
    21.  
    22. void Server_CS::startRead()
    23. {
    24. qDebug() << "server started reading the data received";
    25. QByteArray buffer;
    26. buffer = _cs_Socket->read(_cs_Socket->bytesAvailable());
    27. qDebug() <<"value read from the port is :"<< buffer ;
    28. // _handleIncomingData(buffer);
    29. _cs_Socket->close();
    30.  
    31. }
    To copy to clipboard, switch view to plain text mode 



    /* main.cpp */

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4.  
    5. Server_CS server;
    6.  
    7. Client_CS client;
    8.  
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 



    The error output am getting is
    Error occured: 2 : "Connection closed"


    Please give me some suggestion to resolve this issue.

    Thank you

  2. #2
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qnetworkaccessmanager and qnetworkreply are returning an error "connection close

    You immediately close the socket without giving the eventloop time to process the connections (which will havppen in a.exec()).
    Move the socket close to the destructor.

  3. #3
    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: Qnetworkaccessmanager and qnetworkreply are returning an error "connection close

    What is client_socket? How is it related to the functionality of the provided snippet? Do you use the socket in any way?
    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.


  4. #4
    Join Date
    May 2015
    Posts
    26
    Thanks
    11
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qnetworkaccessmanager and qnetworkreply are returning an error "connection close

    Quote Originally Posted by wysota View Post
    What is client_socket? How is it related to the functionality of the provided snippet? Do you use the socket in any way?
    Actually my requirement is to develop a plug-in using Http protocol over TCP layer.
    That's the reason first am trying to establish a TCP connection with server and client by using

    Qt Code:
    1. client_socket.connectToHost("127.0.0.1", 80); //at the constructor of client
    2. connect(&client_socket, SIGNAL(connected()), this, SLOT(startTransfer()));
    To copy to clipboard, switch view to plain text mode 

    Above code is at the Client side. and below code at the server side

    Qt Code:
    1. cs_server.listen(hostadd, 80); //at the constructor of server
    2. connect(&cs_server, SIGNAL(newConnection()),this, SLOT(acceptConnection()));
    To copy to clipboard, switch view to plain text mode 

    And even without using "client_socket" I can establish a connection directly by calling the function like below
    Qt Code:
    1. client.startTransfer(); // in the main function
    To copy to clipboard, switch view to plain text mode 
    But for to make a proper TCP connection(to receive signal connected ), I have developed like this.

  5. #5
    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: Qnetworkaccessmanager and qnetworkreply are returning an error "connection close

    Quote Originally Posted by gowreesh View Post
    Actually my requirement is to develop a plug-in using Http protocol over TCP layer.
    That doesn't answer wysota's question.
    What do you need the client_socket for, i.e. why do you establish two connections from the client to the server.

    Also you are creating a QTcpSocket in Server_CS::acceptConnection() and then immediately forgetting about it (leaking it).

    Since your server can only work with one connection at a time (using a single socket member variable _cs_Socket) you should first get rid of the second connection you are currently establishing and then test again.

    Your server and client code are currently "out of sync"

    Cheers,
    _

  6. #6
    Join Date
    May 2015
    Posts
    26
    Thanks
    11
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qnetworkaccessmanager and qnetworkreply are returning an error "connection close

    Sorry , I have understood the concept wrongly.
    Please check my modified code below .
    After the code updation, am facing an issue with signal "SIGNAL(finished())". This signal is not getting emitted . And that's the reason the function replyFinished() is not getting called.
    I tried understanding this problem using Debugger also . But it is giving an time-out error(saying that gdb could have stuck in endless loop). Please find the attachment of Debugger output error .

    Qt Code:
    1. [B]/* Client_CS.h */[/B]
    2. class Client_CS: public QObject
    3. {
    4. Q_OBJECT
    5. public:
    6. QNetworkReply *pReply;
    7. QNetworkAccessManager *pManager;
    8. Client_CS(QObject* parent = 0);
    9. ~Client_CS();
    10. void startTransfer();
    11. public slots:
    12. void replyFinished();
    13.  
    14. };
    15.  
    16. [B]/* Client_CS.cpp */[/B]
    17.  
    18. Client_CS::Client_CS(QObject* parent): QObject(parent)
    19. { qDebug() <<"data starting";
    20. }
    21.  
    22. Client_CS::~Client_CS()
    23. {
    24. }
    25.  
    26. void Client_CS::startTransfer()
    27. {
    28. QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    29. QHttpPart filePart;
    30. filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/xml"));
    31. filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; filename=\"text\""));
    32. QFile *file = new QFile(xmllink);
    33. if ( !file->exists() )
    34. { qDebug() << "File Does not exist";
    35. }
    36. file->open(QIODevice::ReadOnly);
    37. filePart.setBodyDevice(file);
    38. file->setParent(multiPart);
    39. multiPart->append(filePart);
    40. QUrl url("http://127.0.0.1:80");
    41. QNetworkRequest request(url);
    42. pManager = new QNetworkAccessManager();
    43. pReply = pManager->post(request, multiPart);
    44. multiPart->setParent(pReply);
    45. QObject::connect(pReply, SIGNAL(finished()),this, SLOT(replyFinished()));
    46. }
    47.  
    48. void Client_CS::replyFinished()
    49. {
    50. qDebug()<<"got the reply from the network";
    51. if ( pReply->error() > 0 )
    52. { qDebug() << "Error occurred: " << pReply->error() << " : " << pReply->errorString();
    53. }
    54. else
    55. { qDebug() << "Upload success";
    56. }
    57. pReply->deleteLater();
    58. }
    To copy to clipboard, switch view to plain text mode 




    Qt Code:
    1. [B]/* Server_CS.h */[/B]
    2.  
    3. class Server_CS: public QObject
    4. {
    5. Q_OBJECT
    6. public:
    7. Server_CS(QObject * parent = 0);
    8. ~Server_CS();
    9. public slots:
    10. void acceptConnection();
    11. void startRead();
    12. private:
    13. QTcpSocket* _cs_Socket;
    14. QTcpServer cs_server;
    15. };
    16.  
    17. [B]/* Server_CS.cpp */[/B]
    18.  
    19. Server_CS::Server_CS(QObject* parent): QObject(parent)
    20. { qDebug() << "server initiated";
    21. QHostAddress hostadd(QHostAddress::Any);
    22. cs_server.listen(hostadd, 80);
    23. connect(&cs_server, SIGNAL(newConnection()),this, SLOT(acceptConnection()));
    24. }
    25.  
    26. Server_CS::~Server_CS()
    27. {cs_server.close();
    28. }
    29.  
    30. void Server_CS::acceptConnection()
    31. { qDebug() << "server got new connection";
    32. _cs_Socket = cs_server.nextPendingConnection();
    33. connect(_cs_Socket, SIGNAL(readyRead()),this, SLOT(startRead()));
    34. }
    35.  
    36. void Server_CS::startRead()
    37. { qDebug() << "server started reading the data received";
    38. int size;
    39. size = _cs_Socket->bytesAvailable();
    40. QByteArray data;
    41. data.resize(size);
    42. _cs_Socket->read(data.data(), size);
    43. qDebug() <<"value read from the port is :"<< data ;
    44. // _handleIncomingData(buffer);
    45. }
    To copy to clipboard, switch view to plain text mode 



    Qt Code:
    1. [B]/* MAIN.cpp */[/B]
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QCoreApplication a(argc, argv);
    6. Server_CS server;
    7. Client_CS client;
    8. client.startTransfer();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 


    Debugger_gdb.png
    Last edited by gowreesh; 29th May 2015 at 04:00. Reason: updated contents

  7. #7
    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: Qnetworkaccessmanager and qnetworkreply are returning an error "connection close

    Does the server produce any output for the client? Does it respect the HTTP protocol when (if) doing so?
    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.


  8. #8
    Join Date
    May 2015
    Posts
    26
    Thanks
    11
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qnetworkaccessmanager and qnetworkreply are returning an error "connection close

    Quote Originally Posted by wysota View Post
    Does the server produce any output for the client? Does it respect the HTTP protocol when (if) doing so?
    Actually I don't need a response from the server, and even the data which I am passing at the client is receiving properly at the server side.
    But as per the QNetworkAccessManager I should get a reply from the server, so only I am trying to validate the network reply.

    Actually as per my project requirement, the XML data which is received at the server side will goes further to the decoder for further processing.
    and then the Encoder at the server side will send the updated XML data to the client using a separate port. (well I need to implement this part still. and my plan is to reuse the code which I have for client to server.)

    Please correct me if I am wrong.
    Thank you

  9. #9
    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: Qnetworkaccessmanager and qnetworkreply are returning an error "connection close

    Quote Originally Posted by gowreesh View Post
    Actually I don't need a response from the server, and even the data which I am passing at the client is receiving properly at the server side.
    But as per the QNetworkAccessManager I should get a reply from the server, so only I am trying to validate the network reply.
    If you are not sending a reply then why do you expect that the client receives it? How can the client know that the request is finished?

    You are using HTTP protocol as the transport layer but it seems to me this was not your conscious decision, right?
    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.


  10. #10
    Join Date
    May 2015
    Posts
    26
    Thanks
    11
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qnetworkaccessmanager and qnetworkreply are returning an error "connection close

    Quote Originally Posted by wysota View Post
    If you are not sending a reply then why do you expect that the client receives it? How can the client know that the request is finished?

    You are using HTTP protocol as the transport layer but it seems to me this was not your conscious decision, right?
    I have understood that when the "cs_server.close()" is called , the server connection will end and so client will also come to know.
    If am wrong, please tell me How to inform back the client ???
    and my requirement is to develop a Http connection over a tcp layer, please tell me if am doing wrong.

    Sorry for asking many questions . I am a fresher . new to c++ and QT . Everything am newly learning and implementing.

  11. #11
    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: Qnetworkaccessmanager and qnetworkreply are returning an error "connection close

    Quote Originally Posted by gowreesh View Post
    I have understood that when the "cs_server.close()" is called , the server connection will end and so client will also come to know.
    This closes the listening server socket, not the connection socket (in you code that would be "_cs_Socket")

    Quote Originally Posted by gowreesh View Post
    If am wrong, please tell me How to inform back the client ???
    A HTTP server would at least respond with an HTTP response code.

    Quote Originally Posted by gowreesh View Post
    and my requirement is to develop a Http connection over a tcp layer, please tell me if am doing wrong.
    Client side this is taken care of by QNetworkAccessManager.

    Maybe you could clarify what you are actually trying to develop?
    A client using HTTP to upload data to a webserver?
    A webserver to receive uploads from a webclient?
    Both?

    Cheers,
    _

  12. #12
    Join Date
    May 2015
    Posts
    26
    Thanks
    11
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qnetworkaccessmanager and qnetworkreply are returning an error "connection close

    Quote Originally Posted by anda_skoa View Post
    Client side this is taken care of by QNetworkAccessManager.

    Maybe you could clarify what you are actually trying to develop?
    A client using HTTP to upload data to a webserver?
    A webserver to receive uploads from a webclient?
    Both?

    Cheers,
    _
    Thanks for the response.
    My plan of developing is as per below..
    I have 2 isolated system's that is system1 and system2.
    where as the system1 gives the output data as a XML file.
    where the system2 is a simulator which accepts input as XML file data , and then it further process the received XML file data and returns an updated XML file data.

    I have assumed that System1 is a Client and system2 is a Server.
    My intention is to develop an interface between these 2 system's.

    Initially "my interface for client"(Client_CS.cpp) receives the XML file from the system1 . and the received XML file data will be sent to my interface for server(Server_CS.cpp) through the http protocol.
    And this received XML data at Server_CS.cpp will be further sent to the decoder.The decoder will pass this data to the system2.
    and this part only am trying to implemented above. and this one is done using port 80.
    (Note : I am no need to worry about the Decoder and Encoder part , that part is already there and its not in my scope to develop. and I just need to call the Decoder and Encoder in my interface)

    where there is a one more requirement, I still need to develop i.e
    the system2 will give the updated XML data to the encoder. and My "interface at server" will read this encoder data and send it to the client interface using http protocol with different port 8080. and the received data at "client interface" will be sending to the system1. and am planning to reuse the same above code for this requirement with different port number.


    My issue is , am just expecting any reply from the QNetworkAccessManager because the signal finished is not getting emitted only. Even though this finished signal is not getting emitted But still am able to receive the full data at the server side.
    Can I ignore this reply ? or its mandatory to get a reply from the QNetworkAccessManager, If so then please tell how to get this signal emitted.
    and please tell me if you find any incoherence with my above code and requirement.
    Last edited by gowreesh; 1st June 2015 at 01:27. Reason: updated contents

  13. #13
    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: Qnetworkaccessmanager and qnetworkreply are returning an error "connection close

    Why have you chosen HTTP as the transport protocol?
    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.


  14. #14
    Join Date
    May 2015
    Posts
    26
    Thanks
    11
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qnetworkaccessmanager and qnetworkreply are returning an error "connection close

    Quote Originally Posted by wysota View Post
    Why have you chosen HTTP as the transport protocol?

    my project manager wants me to develop this plugin only by using Http protocol.
    Sorry I don't know why Http protocol only.
    Last edited by gowreesh; 1st June 2015 at 07:19. Reason: updated contents

  15. #15
    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: Qnetworkaccessmanager and qnetworkreply are returning an error "connection close

    Quote Originally Posted by gowreesh View Post
    my project manager wants me to develop this plugin only by using Http protocol.
    Are you going to implement an HTTP server as well? From your previous responses it seems that you are but the same responses seem to suggest you do not understand that protocol at all. Are you 100% sure what you are doing?
    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.


Similar Threads

  1. Replies: 5
    Last Post: 20th May 2015, 11:53
  2. Replies: 0
    Last Post: 14th April 2014, 14:58
  3. QNetworkReply and "unknown error" problem
    By hybrid_snyper in forum Newbie
    Replies: 3
    Last Post: 3rd December 2012, 00:45
  4. "Treat wchar_t as Built-in Type" to "yes" link error
    By sungaoyong in forum Qt Programming
    Replies: 1
    Last Post: 5th June 2008, 11:45
  5. QFile Problem~ "Unknow error" in "open(QIODevice::ReadWrite)"
    By fengtian.we in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 15:58

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.