Results 1 to 4 of 4

Thread: QTcpSocket no more data only for some sites

  1. #1
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTcpSocket no more data only for some sites

    I develped a proxy in Qt.
    It works. But just for a site, it stucks at some point during communication.
    If I delete all Internet data (IExplorer's panel), I can get a little bit more data, but never the all the data.
    Before posting code, there is something I have to check?

  2. #2
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket no more data only for some sites

    Nobody?
    I post the code.
    Qt Code:
    1. /*
    2.  
    3. */
    4.  
    5. #include "webproxy.h"
    6. #include <QtNetwork>
    7. #include <QMessageBox>
    8. #include <QtGui>
    9. #include <QHash>
    10. //#define DEBUG
    11.  
    12. WebProxy::WebProxy(QObject *parent,int port): QObject(parent)
    13.  
    14. {
    15. qDebug()<<" Listen...";
    16. authMethod = "";
    17. QTcpServer *proxyServer = new QTcpServer(this);
    18. if (!proxyServer->listen(QHostAddress::Any, port)) {
    19. emit error(1);
    20.  
    21. return;
    22. }
    23.  
    24. connect(proxyServer, SIGNAL(newConnection()), this, SLOT(manageQuery()));
    25. qDebug() << "Proxy server running at port" << proxyServer->serverPort();
    26.  
    27.  
    28. }
    29.  
    30.  
    31. void WebProxy::setAddress(const QHostAddress &a, int port)
    32. {
    33. proxyAddress = a;
    34. proxyPort = port;
    35. port = port < 0 ? 8081 : port;
    36.  
    37.  
    38. }
    39.  
    40. void WebProxy::manageQuery() {
    41. QTcpServer *proxyServer = qobject_cast<QTcpServer*>(sender());
    42. QTcpSocket *socket = proxyServer->nextPendingConnection();
    43. connect(socket, SIGNAL(readyRead()), this, SLOT(processQuery()));
    44. connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));
    45.  
    46. qDebug()<<"New connection started..."<<socket->peerAddress();
    47. }
    48. QUrl WebProxy::getUrl(QList<QByteArray > &entries)
    49. {
    50.  
    51. QByteArray method = entries.value(0);
    52. QByteArray address = entries.value(1);
    53. QByteArray version = entries.value(2);
    54.  
    55. qDebug()<<method;
    56. qDebug()<<address;
    57. qDebug()<<version;
    58. QUrl url = QUrl::fromEncoded(address);
    59. if (!url.isValid()) {
    60.  
    61. qWarning() << "Invalid URL:" << url;
    62.  
    63. return QString();
    64. }
    65.  
    66.  
    67. return url;
    68. }
    69.  
    70. void WebProxy::processQuery() {
    71.  
    72.  
    73. QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender());
    74. QByteArray requestData = socket->readAll();
    75.  
    76. qDebug()<<"Request "<<requestData;
    77.  
    78. int pos = requestData.indexOf("\r\n");
    79.  
    80.  
    81. QByteArray requestLine = requestData.left(pos);
    82. requestData.remove(0, pos + 2);
    83.  
    84. QList<QByteArray> entries = requestLine.split(' ');
    85. QByteArray method = entries.value(0);
    86. QByteArray address = entries.value(1);
    87. QByteArray version = entries.value(2);
    88.  
    89.  
    90. QUrl url = QUrl::fromEncoded(address);
    91. if (!url.isValid()) {
    92. qWarning() << "Invalid URL:" << url;
    93. socket->disconnectFromHost();
    94. return;
    95. }
    96.  
    97. QString host = url.host();
    98.  
    99.  
    100. }
    101.  
    102. int port = (url.port() <= 0) ? 80 : url.port();
    103. QByteArray req = url.encodedPath();
    104. if (url.hasQuery())
    105. req.append('?').append(url.encodedQuery());
    106.  
    107. requestLine = method + " " + req + " " + version + "\r\n";
    108. if (!authMethod.isEmpty())
    109. {
    110. requestLine.append(requestLine);
    111. requestLine.append(authMethod);
    112. requestLine.append("\r\n");
    113. }
    114.  
    115. requestData.prepend(requestLine);
    116. qDebug()<<"REQUESTE DATA.........."<<requestData;
    117.  
    118.  
    119. QString key = host + ':' + QString::number(port);
    120. QTcpSocket *proxySocket = socket->findChild<QTcpSocket*>(key);
    121.  
    122. if (proxySocket) {
    123. proxySocket->setObjectName(key);
    124. proxySocket->setProperty("url", url);
    125. proxySocket->setProperty("requestData", requestData);
    126. proxySocket->write(requestData);
    127.  
    128. } else {
    129. proxySocket = new QTcpSocket(socket);
    130. proxySocket->setObjectName(key);
    131. proxySocket->setProperty("url", url);
    132. proxySocket->setProperty("requestData", requestData);
    133. connect(proxySocket, SIGNAL(connected()), this, SLOT(sendRequest()));
    134. connect(proxySocket, SIGNAL(readyRead()), this, SLOT(transferData()));
    135. connect(proxySocket, SIGNAL(disconnected()), this, SLOT(closeConnection()));
    136. connect(proxySocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(closeConnection()));
    137. proxySocket->connectToHost(host, port);
    138. }
    139. }
    140.  
    141. void WebProxy::sendRequest() {
    142. QTcpSocket *proxySocket = qobject_cast<QTcpSocket*>(sender());
    143. QByteArray requestData = proxySocket->property("requestData").toByteArray();
    144. proxySocket->write(requestData);
    145.  
    146. void WebProxy::transferData() {
    147.  
    148.  
    149. QTcpSocket *proxySocket = qobject_cast<QTcpSocket*>(sender());
    150.  
    151. QByteArray data = proxySocket->readAll();
    152. QString host = proxySocket->peerAddress().toString();
    153. QByteArray filtered(data);
    154.  
    155.  
    156.  
    157. QTcpSocket *socket = qobject_cast<QTcpSocket*>(proxySocket->parent());
    158. qDebug()<<"WP socket write"<<filtered;
    159. if (!data.trimmed().isEmpty())
    160. if (socket->write(filtered)==-1)
    161. qDebug()<<"WP error";
    162.  
    163.  
    164. qDebug()<<"WP socket write"<<filtered;
    165. }
    166.  
    167. void WebProxy::closeConnection() {
    168.  
    169. QTcpSocket *proxySocket = qobject_cast<QTcpSocket*>(sender());
    170. if (proxySocket) {
    171. QTcpSocket *socket = qobject_cast<QTcpSocket*>(proxySocket->parent());
    172. if (socket)
    173. socket->disconnectFromHost();
    174. if (proxySocket->error() != QTcpSocket::RemoteHostClosedError)
    175. qWarning() << "Error for:" << proxySocket->property("url").toUrl()
    176. << proxySocket->errorString();
    177. proxySocket->deleteLater();;
    178. }
    179. }
    To copy to clipboard, switch view to plain text mode 
    Sometimes, in the output debug I gat strange things...expecially many Http 304 Not modified errors.
    Try with this IP plaza ip: 87.22.235.24 which is a public DVR.
    For other everythings is ok. But only for this I cannot get the response from the server....

  3. #3
    Join Date
    Aug 2011
    Location
    Canada
    Posts
    8
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTcpSocket no more data only for some sites

    have you ensured that the result of proxySocket->write(requestData); equals requestData.size()?

  4. #4
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket no more data only for some sites

    Yes. I checked the size() method on requestData and the result of the write and always they are the same...
    G


    Added after 1 41 minutes:


    I checked the webpage of the video server.
    In Linux, from the command shell, If I make a simple
    GET http://87.22.235.24/ HTTP/1.1 I get all the content correctly.
    But with the code of above, it stucks at downloading all the scripts.
    There is something to do, like flush, close, reserve memory...
    G
    Last edited by giusepped; 10th August 2011 at 11:10.

Similar Threads

  1. how to use qtcpsocket send qimage data
    By tsuibin in forum Qt Programming
    Replies: 2
    Last Post: 18th April 2012, 14:51
  2. QTcpSocket - making sure all data was sent
    By eliben in forum Qt Programming
    Replies: 4
    Last Post: 10th July 2011, 12:33
  3. QTcpSocket no more receives data after some time
    By ben_m in forum Qt Programming
    Replies: 3
    Last Post: 23rd June 2011, 00:11
  4. QTCPSocket not getting all data
    By jhowland in forum Qt Programming
    Replies: 4
    Last Post: 29th January 2010, 15:20
  5. QTcpSocket + receiving data
    By navi1084 in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2009, 08:10

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
  •  
Qt is a trademark of The Qt Company.