Results 1 to 5 of 5

Thread: Client QNetworkReply is returning Connection Closed Status

  1. #1
    Join Date
    Apr 2014
    Posts
    125
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Android Maemo/MeeGo

    Question Client QNetworkReply is returning Connection Closed Status

    Hello;

    Based on the different code/examples found online, I have built a bidirectional qt http communication between server (on a linux machine) and client (on another linux machine).
    The requirement is to use PUT on the client side.
    Server is able to receive data from client but I'm having these problems in the direction: server -> client:

    - Client: No emission of the readyRead() signal
    - Client: QNetworkReply status is returning "Connection Closed" inside the function onFinished() slot
    - Server: client->write() is failing with "QIODevice::write: device not open" and also "disconnected" signal is emitted.

    Any thoughts/hints on why I'm encountering these problems?

    Thank you very much in advance.

    Mut.

    =================== Client Part =============
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4.  
    5. qDebug() << "========================== Client ========================";
    6. Client myclient;
    7.  
    8. QByteArray bytes("Hello");
    9.  
    10. myclient.Start(QUrl("http://10.77.32.14:2859"), bytes); // server ip is 10.77.32.14
    11.  
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. class Client : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit Client(QObject *parent = 0);
    6. ~Client();
    7.  
    8.  
    9. QNetworkAccessManager *m_manager;
    10.  
    11. void Start(const QUrl url, const QByteArray array);
    12.  
    13. QNetworkReply *m_reply;
    14.  
    15. signals:
    16.  
    17. public slots:
    18.  
    19. void onFinished();
    20. void readData();
    21. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. Client::Client(QObject *parent) : QObject(parent)
    2. {
    3. m_manager = new QNetworkAccessManager(this);
    4. }
    5.  
    6. void Client::Start(const QUrl url, const QByteArray array)
    7. {
    8. qDebug() << "Client::Start:";
    9.  
    10. QNetworkRequest request(url);
    11. request.setHeader(QNetworkRequest::ContentTypeHeader, "text/plain");
    12.  
    13. request.setUrl(url);
    14.  
    15. m_reply = m_manager->post(request, array);
    16.  
    17. m_reply->ignoreSslErrors();
    18.  
    19. bool ok = QObject::connect(m_reply, SIGNAL(finished()), this, SLOT(onFinished()));
    20. ok = QObject::connect(m_reply, SIGNAL(readyRead()), this, SLOT(readData()));
    21.  
    22. Q_ASSERT(ok);
    23. }
    24.  
    25. void Client::readData()
    26. {
    27. qDebug() << "Client::readData()";
    28.  
    29. if (NULL != m_reply)
    30. {
    31. QByteArray data = m_reply->readAll();
    32.  
    33. if (data.size() > 0)
    34. {
    35. QString str(data);
    36. qDebug() << "Client::readData(): data:" << str;
    37. }
    38. }
    39. }
    40.  
    41. void Client::onFinished()
    42. {
    43. QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
    44.  
    45. qDebug() << "Client::onFinshed():";
    46.  
    47. QString response;
    48. if (reply) {
    49. if (reply->error() == QNetworkReply::NoError) {
    50. const int available = reply->bytesAvailable();
    51. if (available > 0) {
    52. const QByteArray buffer(reply->readAll());
    53. response = QString::fromUtf8(buffer);
    54.  
    55. qDebug() << "Client Received:" << response;
    56. }
    57. } else {
    58. response = tr("Error: %1 status: %2").arg(reply->errorString(), reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toString());
    59. qDebug() << response;
    60. }
    61.  
    62. reply->deleteLater();
    63. }
    64.  
    65. if (response.trimmed().isEmpty()) {
    66. response = tr("Unable to retrieve post response");
    67. }
    68. }
    69.  
    70. Client::~Client()
    71. {
    72.  
    73. }
    To copy to clipboard, switch view to plain text mode 

    ======================= Server =================
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4.  
    5. qDebug() << "====================== Server ==========================";
    6.  
    7. Server myserver;
    8.  
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. class Server: public QTcpServer
    2. {
    3.  
    4. Q_OBJECT
    5. public:
    6.  
    7. Server(QObject * parent = 0 , quint16 port = 2859);
    8. virtual ~Server();
    9.  
    10. private slots:
    11.  
    12. void acceptConnection();
    13. void startRead();
    14. void disconnected();
    15.  
    16. private:
    17.  
    18. QTcpSocket * client;
    19.  
    20. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. Server::Server(QObject* parent , quint16 port): QTcpServer(parent)
    2. {
    3. connect(this, SIGNAL(newConnection()),this, SLOT(acceptConnection()));
    4.  
    5. listen(QHostAddress::Any, port );
    6. }
    7.  
    8. Server::~Server()
    9. {
    10. qDebug() << "Server destructor called:";
    11. delete client;
    12. close();
    13. }
    14.  
    15. void Server::acceptConnection()
    16. {
    17. client = nextPendingConnection();
    18.  
    19. connect(client, SIGNAL(readyRead()), this, SLOT(startRead()));
    20. connect(client, SIGNAL(disconnected()), this, SLOT(disconnected()));
    21.  
    22. qDebug() << "New client from:" << client->peerAddress().toString();
    23. }
    24.  
    25. void Server::startRead()
    26. {
    27. while(client->canReadLine())
    28. {
    29. QString line = QString::fromUtf8(client->readLine()).trimmed();
    30. qDebug() << "Client :" << line;
    31.  
    32. client->write(QString("Server : I've taken your message (:\n").toUtf8());
    33. client->flush();
    34. }
    35.  
    36. }
    37.  
    38. void Server::disconnected()
    39. {
    40. qDebug() << "Client disconnected:" << client->peerAddress().toString();
    41.  
    42. client->write(QString("Server : I wish you didn't leave ):\n").toUtf8());
    43. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 9th October 2015 at 12:35. Reason: missing [code] tags

  2. #2
    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: Client QNetworkReply is returning Connection Closed Status

    Have you tried sending a valid HTTP response?

    Cheers,
    _

  3. #3
    Join Date
    Apr 2014
    Posts
    125
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Android Maemo/MeeGo

    Default Re: Client QNetworkReply is returning Connection Closed Status

    Thanks for the reply. I'm not sure that I understand what you mean by sending a valid HTTP response?

  4. #4
    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: Client QNetworkReply is returning Connection Closed Status

    Well, you are using QNetworkAccessManager on a HTTP URL.
    But your server does not respond with an HTTP response, but with just a string.

    It is very likely that the HTTP handler on the client aborts the connection when it gets this unexpected and (for HTTP) invalid data.

    Hence the suggestion to try sending a valid response and see if that fixed the problems you are observing.

    Cheers,
    _

  5. #5
    Join Date
    Apr 2014
    Posts
    125
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Android Maemo/MeeGo

    Default Re: Client QNetworkReply is returning Connection Closed Status

    Thanks for the reply. You are absolutely right, sending a valid HTTP reply is fixing the problem. Ex:

    /* Respond with a basic HTTP message. */
    client->write("HTTP/1.1 200 OK\r\n"
    "Content-type: text/plain\r\n"
    "Content-length: 12\r\n"
    "\r\n"
    "Hello World!");

Similar Threads

  1. Replies: 14
    Last Post: 1st June 2015, 09:15
  2. Replies: 5
    Last Post: 29th March 2015, 21:31
  3. client & server connection -Beginer
    By sathees in forum Newbie
    Replies: 1
    Last Post: 11th May 2014, 12:00
  4. Replies: 6
    Last Post: 2nd April 2014, 09:57
  5. How do I keep the client connection open ?
    By probine in forum Newbie
    Replies: 2
    Last Post: 25th March 2006, 19:06

Tags for this Thread

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.