Results 1 to 14 of 14

Thread: unable to pass the data using http localhost port (QUrl url("http://localhost:445");)

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

    Smile unable to pass the data using http localhost port (QUrl url("http://localhost:445");)

    Hi Everyone,

    I am fresher, trying to develop a code for a my first project , need some help

    I am writting a program , where am trying to pass the xml file body using QHttpMultiPart.
    and the received data from the from the QNetworkAccessManager , i wanted to print and see that the data is coming from localhost port properly or not.
    Please find the below code i have developed

    Main.c


    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    Uploader obj;
    QString xmllink = "C:/Users/ramachandrug/Documents/QT/XML_pharse/rtioInterface.xml";
    obj.upload(xmllink);

    return a.exec();
    }



    XmlComms.cpp

    void Uploader::upload(QString xmlFilePath)
    {

    qDebug() << "Upload Starting";
    QFileInfo fileInfo(xmlFilePath);
    qDebug() << "file path is :" <<fileInfo.absoluteFilePath();
    qDebug() << "file size is :" << fileInfo.size();

    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);

    QHttpPart filePart;
    filePart.setHeader(QNetworkRequest::ContentDisposi tionHeader, QVariant("HTTP/1.1 200 OK"));
    filePart.setHeader(QNetworkRequest::ContentTypeHea der, QVariant("text/xml; charset=utf-8"));

    QFile *file = new QFile(xmlFilePath);
    if ( !file->exists() )
    {
    qDebug() << "File Does not exist";
    }

    file->open(QIODevice::ReadOnly);
    filePart.setBodyDevice(file);
    file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart

    multiPart->append(filePart);

    QUrl url("http://localhost:445");
    qDebug() << "Host:" << url.host();
    qDebug() << "Port:" << url.port();


    QNetworkRequest request(url);

    pManager = new QNetworkAccessManager();

    pReply = pManager->post(request, multiPart);
    multiPart->setParent(pReply);

    //qDebug() <<"XML body is: "<<pReply->readAll();


    QEventLoop *pELoop = new QEventLoop();


    QObject::connect(pManager, SIGNAL(finished(QNetworkReply* )),this, SLOT(replyFinished(QNetworkReply*)));

    pELoop->exec();


    }



    /**
    * @brief Uploader::replyFinished
    */

    void Uploader::replyFinished(QNetworkReply *reply)
    {

    QByteArray data = reply->readAll();
    qDebug() << "data is::"<< data;
    qDebug() << "Upload completed";

    uploadInProgress = false;
    if ( reply->error() > 0 )
    {
    qDebug() << "Error occured: " << reply->error() << " : " << reply->errorString();
    }
    else
    {
    qDebug() << "Upload success";
    }

    //_dataArrived(data); //passing the extracted XML body
    reply->deleteLater();


    }



    When i run this code, am getting the below output

    upload starting
    file path is : C:/Users/ramachandrug/Documents/QT/XML_pharse/rtioInterface.xml

    file size is 35440
    Host : localhost
    port: 445
    data is :: "" //here no output is coming, empty byte array is coming
    upload completed
    Error occured: 99 : "unknown error" // for to fix this error, i installed the openssl also but still am getting this error



    Am running this QT project in my host environment only.

    Anyone please let me know , why am not able to print this xml body here. I am stuck here from last 2days.
    please help me to fix this issue.

    Thanks
    Gowreesh

  2. #2
    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: unable to pass the data using http localhost port (QUrl url("http://localhost:445

    What is running on port 445? Are you receiving the data 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.


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

    Default Re: unable to pass the data using http localhost port (QUrl url("http://localhost:445

    thanks for the response.

    Randomly I have selected one among the List of open ports and listening services(by entering the command "netstat -an"). and the port 445 is a TCP proto .
    and yes am trying to receive the data from this port. and the same received data am trying print using:
    QByteArray data = reply->readAll();
    qDebug() << "data is::"<< data;
    Last edited by gowreesh; 12th May 2015 at 08:47. Reason: updated contents

  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: unable to pass the data using http localhost port (QUrl url("http://localhost:445

    Quote Originally Posted by gowreesh View Post
    Randomly I have selected one among the List of open ports and listening services(by entering the command "netstat -an"). and the port 445 is a TCP proto .
    If there is no HTTP service listening there then how do you expect it to accept your data and send something back?
    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. The following user says thank you to wysota for this useful post:

    gowreesh (25th May 2015)

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: unable to pass the data using http localhost port (QUrl url("http://localhost:445

    You are also sending very specific request information, obviously designed for specific service, to a random service. Even if you got a reply it is very unlikely to be useful.

    When you find the real service to make the request on you may need to set the content type on the QNetworkRequest match the multi-part data in your payload.

  7. The following user says thank you to ChrisW67 for this useful post:

    gowreesh (25th May 2015)

  8. #6
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: unable to pass the data using http localhost port (QUrl url("http://localhost:445

    And lastly, ports < 1024 are restricted ports and typically require administrative authority to listen on restricted ports. Choose something > 1024 and you won't have any special access requirements.

  9. The following user says thank you to jefftee for this useful post:

    gowreesh (25th May 2015)

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

    Default Re: unable to pass the data using http localhost port (QUrl url("http://localhost:445

    Thank you everyone for your response.
    from all of your inputs , I have developed a below piece of code.
    Expecting one more help from you all .
    Here am trying to read the XML data from the localhost port http://127.0.0.1:8888 .
    Below is the code which i have doubt

    /* main */

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    Uploader obj;
    QString xmllink = "C:/Users/ramachandrug/Desktop/XML_Pharse/XML/SC_FORM_A_AUTHORISEDResponse.xml";
    obj.upload(xmllink);
    Server server;
    return a.exec();
    }


    /* server.cpp */

    #include <QtNetwork>
    #include <QObject>
    #include <QTcpServer>
    #include <QTcpSocket>
    // server.cc
    #include "server.h"

    Server::Server()
    {
    qDebug() << "server initiated";
    QHostAddress hostadd(QHostAddress::Any);
    connect(&server, SIGNAL(newConnection()), this, SLOT(acceptConnection()));

    server.listen(hostadd , 8888);
    }

    Server::~Server()
    {
    server.close();
    }

    void Server::acceptConnection()
    {

    qDebug() << "server initiated 2";
    client = server.nextPendingConnection();

    connect(client, SIGNAL(readyRead()), this, SLOT(startRead()));
    }

    void Server::startRead()
    {

    qDebug() << "server started";
    char buffer[1024] = {0};
    client->read(buffer, client->bytesAvailable());
    qDebug() << "value read from the port is :" << buffer;
    //_handleIncomingData(buffer); // here it goes further for a separate system
    }



    /*client.cpp */

    #include <QUrl>
    #include <QFileInfo>
    #include <QMimeDatabase>
    #include <QHttpMultiPart>
    #include <QDebug>
    #include <QEventLoop>
    #include <QNetworkReply>
    #include <QDomDocument>

    #include "server.h"

    #include "XmlComms.h"


    void Uploader::upload(QString xmlFilePath)
    {

    qDebug() << "Upload Starting";
    QFileInfo fileInfo(xmlFilePath);
    qDebug() << "file path is :" <<fileInfo.absoluteFilePath();
    qDebug() << "file size is :" << fileInfo.size();

    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);

    QHttpPart filePart;
    filePart.setHeader(QNetworkRequest::ContentTypeHea der, QVariant("text/xml"));
    filePart.setHeader(QNetworkRequest::ContentDisposi tionHeader, QVariant("form-data; filename=\"text\""));

    QFile *file = new QFile(xmlFilePath);

    if ( !file->exists() )
    {
    qDebug() << "File Does not exist";
    }

    file->open(QIODevice::ReadOnly);
    filePart.setBodyDevice(file);
    file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart

    multiPart->append(filePart);

    QUrl url("http://127.0.0.1:8888");
    qDebug() << "Host:" << url.host();
    qDebug() << "Port:" << url.port();
    QNetworkRequest request(url);

    pManager = new QNetworkAccessManager();
    pReply = pManager->post(request, multiPart);
    multiPart->setParent(pReply);

    //pELoop = new QEventLoop();

    QObject::connect(pReply, SIGNAL(finished( )),this, SLOT(replyFinished()));

    //pELoop->exec(); // here is the problem


    }


    void Uploader::replyFinished()
    {

    //qDebug() << "data is::"<< pReply->readAll();;
    qDebug() << "Upload completed";

    if ( pReply->error() > 0 )
    {
    qDebug() << "Error occured: " << pReply->error() << " : " << pReply->errorString();
    }
    else
    {
    qDebug() << "Upload success";
    }

    pReply->deleteLater();

    pELoop->exit();

    }


    Issue is :
    Suppose if I don't include pELoop->exec() in a code, then I can receive the XML data in the localhost port .
    and here the function "replyFinished" is not getting invoked, it is because the signal finished() will not be emitted. and
    i dont know why it is not getting emitted.
    Here is the output below:

    Upload Starting
    file path is : "C:/Users/ramachandrug/Desktop/XML_Pharse/XML/SC_FORM_A_AUTHORISE
    DResponse.xml"
    file size is : 650
    Host: "127.0.0.1"
    Port: 8888
    server initiated
    server initiated 2
    server started
    value read from the port is : POST / HTTP/1.1
    Content-Type: multipart/form-data; boundary="boundary_.oOo._MjI5Mjc=NzEyMQ==MTk0
    MTY="
    MIME-Version: 1.0
    Content-Length: 815
    Connection: Keep-Alive
    Accept-Encoding: gzip, deflate
    Accept-Language: en-AU,*
    User-Agent: Mozilla/5.0
    Host: 127.0.0.1:8888


    server initiated 2
    server started
    value read from the port is : POST / HTTP/1.1 /* 2 times this 10 lines are coming in the output*/
    Content-Type: multipart/form-data; boundary="boundary_.oOo._MjI5Mjc=NzEyMQ==MTk0
    MTY="
    MIME-Version: 1.0
    Content-Length: 815
    Connection: Keep-Alive
    Accept-Encoding: gzip, deflate
    Accept-Language: en-AU,*
    User-Agent: Mozilla/5.0
    Host: 127.0.0.1:8888

    --boundary_.oOo._MjI5Mjc=NzEyMQ==MTk0MTY=
    Content-Type: text/xml
    Content-Disposition: form-data; filename="text"

    HTTP/1.1 200 OK
    Server: gSOAP/2.7
    Content-Type: text/xml; charset=utf-8
    Content-Length: 506
    Connection: close

    <?xml version="1.0" encoding="UTF-8" ?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xm
    lns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://
    www.w3.org/2001/XMLSchema"
    xmlns:ns1="http://interfaces.atms.org/AMS-TCS/" xmlns:ns2="http://interf
    aces.atms.org/TCS-AMS/">
    <SOAP-ENV:Body>
    <ns1:SC_FORM_A_AUTHORISEDResponse>
    <out>true</out>
    </ns1:SC_FORM_A_AUTHORISEDResponse>
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>



    But here in the above code if I include the pELoop->exec() until finished signal is emitted, then no output comes out.
    and network reply returns connection refused.
    here is the output below:

    Upload Starting
    file path is : "C:/Users/ramachandrug/Desktop/XML_Pharse/XML/SC_FORM_A_AUTHORISE
    DResponse.xml"
    file size is : 650
    Host: "127.0.0.1"
    Port: 8888
    Upload completed
    Error occured: 1 : "Connection refused"
    server initiated
    server initiated 2
    ----


    Requesting you to please tell me why it is saying connection refused . and why those 10 lines of output is printing twice
    and does it creates a problem.

  11. #8
    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: unable to pass the data using http localhost port (QUrl url("http://localhost:445

    Quote Originally Posted by gowreesh View Post
    Requesting you to please tell me why it is saying connection refused
    Look at the order in which you create the two objects in main().

    Cheers,
    _

  12. The following user says thank you to anda_skoa for this useful post:

    gowreesh (26th May 2015)

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

    Default Re: unable to pass the data using http localhost port (QUrl url("http://localhost:445

    Quote Originally Posted by anda_skoa View Post
    Look at the order in which you create the two objects in main().

    Cheers,
    _
    Hi,
    Thanks for the reply.
    I have Updated the Main() function as mentioned below.

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    QString xmllink = "C:/Users/ramachandrug/Desktop/XML_Pharse/XML/SC_FORM_A_AUTHORISEDResponse.xml";

    Server server;


    Uploader obj;

    obj.upload(xmllink);

    return a.exec();
    }



    After changing the order in the main() function I am getting 2 outputs randomly,for the same build .

    Output 1:

    server initiated
    Upload Starting
    file path is : "C:/Users/ramachandrug/Desktop/XML_Pharse/XML/SC_FORM_A_AUTHORISE
    DResponse.xml"
    file size is : 650
    Host: "127.0.0.1"
    Port: 8888
    server initiated 2
    server started
    value read from the port is : POST / HTTP/1.1
    Content-Type: multipart/form-data; boundary="boundary_.oOo._MTc4MjY=Mjg3NjU=ODI0
    Ng=="
    MIME-Version: 1.0
    Content-Length: 815
    Connection: Keep-Alive
    Accept-Encoding: gzip, deflate
    Accept-Language: en-AU,*
    User-Agent: Mozilla/5.0
    Host: 127.0.0.1:8888


    server initiated 2
    server started
    value read from the port is : POST / HTTP/1.1
    Content-Type: multipart/form-data; boundary="boundary_.oOo._MTc4MjY=Mjg3NjU=ODI0
    Ng=="
    MIME-Version: 1.0
    Content-Length: 815
    Connection: Keep-Alive
    Accept-Encoding: gzip, deflate
    Accept-Language: en-AU,*
    User-Agent: Mozilla/5.0
    Host: 127.0.0.1:8888


    Upload completed
    Error occured: 2 : "Connection closed"




    Output 2:


    server initiated
    Upload Starting
    file path is : "C:/Users/ramachandrug/Desktop/XML_Pharse/XML/SC_FORM_A_AUTHORISE
    DResponse.xml"
    file size is : 650
    Host: "127.0.0.1"
    Port: 8888
    server initiated 2
    server started
    value read from the port is : POST / HTTP/1.1
    Content-Type: multipart/form-data; boundary="boundary_.oOo._MTc1NTI=NzkzMw==OTAx
    NA=="
    MIME-Version: 1.0
    Content-Length: 815
    Connection: Keep-Alive
    Accept-Encoding: gzip, deflate
    Accept-Language: en-AU,*
    User-Agent: Mozilla/5.0
    Host: 127.0.0.1:8888

    --boundary_.oOo._MTc1NTI=NzkzMw==OTAxNA==
    Content-Type: text/xml
    Content-Disposition: form-data; filename="text"

    HTTP/1.1 200 OK
    Server: gSOAP/2.7
    Content-Type: text/xml; charset=utf-8
    Content-Length: 506
    Connection: close

    <?xml version="1.0" encoding="UTF-8" ?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xm
    lns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://
    www.w3.org/2001/XMLSchema"
    xmlns:ns1="http://interfaces.atms.org/AMS-TCS/" xmlns:ns2="http://interf
    aces.atms.org/TCS-AMS/">
    <SOAP-ENV:Body>
    <ns1:SC_FORM_A_AUTHORISEDResponse>
    <out>true</out>
    </ns1:SC_FORM_A_AUTHORISEDResponse>
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    --b►?`♥☻


    For the output 2 : After printing above output it will say XML.exe has stopped working. Please find the snapshot also below

    QT error.png

    Requesting you to please help out for to resolve this issue.
    Last edited by gowreesh; 26th May 2015 at 04:18. Reason: spelling corrections

  14. #10
    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: unable to pass the data using http localhost port (QUrl url("http://localhost:445

    Well, the stack trace at the time of the crash would probably tell you where it crashes.

    My guess is your unchecked memory buffer access here
    Qt Code:
    1. char buffer[1024] = {0};
    2. client->read(buffer, client->bytesAvailable());
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  15. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: unable to pass the data using http localhost port (QUrl url("http://localhost:445

    QByteArray is your friend

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

    Default Re: unable to pass the data using http localhost port (QUrl url("http://localhost:445

    Quote Originally Posted by anda_skoa View Post
    Well, the stack trace at the time of the crash would probably tell you where it crashes.

    My guess is your unchecked memory buffer access here
    Qt Code:
    1. char buffer[1024] = {0};
    2. client->read(buffer, client->bytesAvailable());
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _
    Thanks for the reply...

    I have modified the code with below statement, no crashes are happening now. and it works fine.
    Qt Code:
    1. QByteArray buffer;
    2. buffer = client->read(client->bytesAvailable());
    To copy to clipboard, switch view to plain text mode 

    But the issue is still the network reply error is "Connection closed" only is coming(from the below code). I am unable to find how the remote server closed the connection prematurely, before the entire reply was received and processed.
    Qt Code:
    1. pReply = pManager->post(request, multiPart);
    To copy to clipboard, switch view to plain text mode 

    I tried changing the localhost port also but still this error is coming.
    Last edited by gowreesh; 27th May 2015 at 07:56. Reason: updated contents

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

    Default Re: unable to pass the data using http localhost port (QUrl url("http://localhost:445

    I will create a New thread with proper explanation and code snippet .... Thank you

  18. #14
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: unable to pass the data using http localhost port (QUrl url("http://localhost:445

    Quote Originally Posted by gowreesh View Post
    But the issue is still the network reply error is "Connection closed" only is coming(from the below code). I am unable to find how the remote server closed the connection prematurely, before the entire reply was received and processed.
    Look at the headers returned by the server that you're communicating with. Specifically, the following header returned by the server:

    Qt Code:
    1. Connection: close
    To copy to clipboard, switch view to plain text mode 

    This is the server telling you that the http connection will be closed after completion of the response.

Similar Threads

  1. Replies: 4
    Last Post: 2nd June 2012, 07:04
  2. Replies: 11
    Last Post: 21st June 2011, 01:05
  3. why "Unable to read image data"
    By zarelaky in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 21st December 2010, 00:47
  4. Replies: 1
    Last Post: 7th April 2010, 21:46
  5. Replies: 0
    Last Post: 3rd December 2008, 11: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.