Results 1 to 4 of 4

Thread: Problems with development proxy server.

  1. #1
    Join Date
    Jul 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problems with development proxy server.

    Hi. I decided to write a proxy server. The problems are - the application shortly after the launch fails - QThread: Destroyed while thread is still running, and in general a proxy is acting funny, retarding the page is refreshed.
    Source code here - _http :/ / zalil.ru/33553766. Can anybody help me?

  2. #2
    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: Problems with development proxy server.

    Can anybody help me?
    Probably not until you ask a smart question and don't expect everyone else to download a RAR file from a Russian site (that is unlikely to be known and trusted outside Russia) just to have a chance of debugging your program code.

  3. #3
    Join Date
    Jul 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with development proxy server.

    Hi. The problem now was. Through a proxy I can get one site, if I go to the other site, I get 404 - Not Found, but if I restart the proxy and again I come to this second site, then it works fine, but if I once again I go to the next site, I get 404 - Not Found again. That is, it turns the proxy works for one site. I always clear out browser cache, internet works fine. I tested proxy in a few browsers, the result is the same. How can I fix it? Thanks. Source:

    ///////////////////
    main.cpp

    #include <QtGui/QApplication>
    #include "dialog.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    dialog d;
    d.show();
    return a.exec();
    }
    ****************
    dialog.h

    #ifndef DIALOG_H
    #define DIALOG_H

    #include <QWidget>
    #include "socketserver.h"
    #include <QtGui>

    class dialog : public QWidget
    {
    Q_OBJECT
    public:
    explicit dialog(QWidget *parent = 0);

    private:
    SocketServer m_SocketServer;

    QLabel *statusLabel;
    signals:

    public slots:
    };

    #endif // DIALOG_H
    ***************
    dialog.cpp

    #include "dialog.h"
    #include <QtGui>
    #include <QtNetwork>

    dialog::dialog(QWidget *parent) :
    QWidget(parent)
    {
    if (!m_SocketServer.listen(QHostAddress::LocalHost,10 001)) {
    QMessageBox::critical(this, tr("Proxy Sever"),
    tr("Unable to start the server: %1.")
    .arg(m_SocketServer.errorString()));
    close();
    return;
    }

    statusLabel = new QLabel();
    statusLabel->setWordWrap(true);

    QString str;
    QTextStream(&str) << "Listent ip - ";
    QTextStream(&str) << m_SocketServer.serverAddress().toString();
    QTextStream(&str) << " \n\rPort - ";
    QTextStream(&str) << m_SocketServer.serverPort();

    statusLabel->setText(str);

    QVBoxLayout * mainLayout = new QVBoxLayout;
    mainLayout->addWidget(statusLabel);
    setLayout(mainLayout);
    }

    Hi. The problem now was. Through a proxy I can get one site, if I go to the other site, I get 404 - Not Found, but if I restart the proxy and again I come to this second site, then it works fine, but if I once again I go to the next site, I get 404 - Not Found again. That is, it turns the proxy works for one site. I always clear out browser cache, internet works fine. I tested proxy in a few browsers, the result is the same. How can I fix it? Thanks. Source:

    ///////////////////
    main.cpp

    #include <QtGui/QApplication>
    #include "dialog.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    dialog d;
    d.show();
    return a.exec();
    }
    ****************
    dialog.h

    #ifndef DIALOG_H
    #define DIALOG_H

    #include <QWidget>
    #include "socketserver.h"
    #include <QtGui>

    class dialog : public QWidget
    {
    Q_OBJECT
    public:
    explicit dialog(QWidget *parent = 0);

    private:
    SocketServer m_SocketServer;

    QLabel *statusLabel;
    signals:

    public slots:
    };

    #endif // DIALOG_H
    ***************
    dialog.cpp

    #include "dialog.h"
    #include <QtGui>
    #include <QtNetwork>

    dialog::dialog(QWidget *parent) :
    QWidget(parent)
    {
    if (!m_SocketServer.listen(QHostAddress::LocalHost,10 001)) {
    QMessageBox::critical(this, tr("Proxy Sever"),
    tr("Unable to start the server: %1.")
    .arg(m_SocketServer.errorString()));
    close();
    return;
    }

    statusLabel = new QLabel();
    statusLabel->setWordWrap(true);

    QString str;
    QTextStream(&str) << "Listent ip - ";
    QTextStream(&str) << m_SocketServer.serverAddress().toString();
    QTextStream(&str) << " \n\rPort - ";
    QTextStream(&str) << m_SocketServer.serverPort();

    statusLabel->setText(str);

    QVBoxLayout * mainLayout = new QVBoxLayout;
    mainLayout->addWidget(statusLabel);
    setLayout(mainLayout);
    }

    ***************************
    socketserver.h

    #ifndef SOCKETSERVER_H
    #define SOCKETSERVER_H

    #include <QTcpServer>
    #include "socketclientthread.h"

    class SocketServer : public QTcpServer
    {
    Q_OBJECT
    public:
    explicit SocketServer(QObject *parent = 0);
    //QThread *thread;
    protected:
    void incomingConnection(int socketDescriptor);
    void customEvent(QEvent * pe);
    signals:
    //quitThread();
    public slots:

    };

    #endif // SOCKETSERVER_H
    **************************
    socketserver.cpp

    #include "socketserver.h"
    #include "socketclientthread.h"
    #include "threadevent.h"

    SocketServer::SocketServer(QObject *parent) :
    QTcpServer(parent)
    {
    }

    void SocketServer::incomingConnection(int socketDescriptor)
    {
    SocketClientThread * thread = new SocketClientThread(socketDescriptor,this);
    //connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));

    thread->start();
    }

    void SocketServer::customEvent(QEvent * pe)
    {
    // if ((int)pe->type() == ThreadEvent::ThreadType) {
    // if(thread != NULL) thread->quit();
    // }

    //QWidget::customEvent(pe);
    }

    ************************
    socketclientthread.h

    #ifndef SOCKETCLIENTTHREAD_H
    #define SOCKETCLIENTTHREAD_H
    #include <QThread>
    #include <QtNetwork>
    #include <QTcpSocket>
    #include "socketserver.h"

    class SocketClientThread : public QThread
    {
    public:

    Q_OBJECT

    public:
    SocketClientThread(int socketDescriptor, QObject *parent);

    void run();

    signals:
    void error(QTcpSocket::SocketError socketError);
    public slots:
    void slotReadClient1();
    void slotReadClient2();
    void disconnectedHandler1();
    // void disconnectedHandler2();
    private slots:
    void errorHandler1(QAbstractSocket::SocketError);
    void errorHandler2(QAbstractSocket::SocketError);

    private:
    int socketDescriptor;
    QTcpSocket * m_tcpSocket1;
    QTcpSocket * m_tcpSocket2;

    public:
    //SocketServer * m_socketServer;
    QTcpSocket * m_socketServer;
    QString getHost(QByteArray &arrayData);
    };

    #endif // SOCKETCLIENTTHREAD_H

    *****************************
    socketclientthread.cpp

    #include "socketclientthread.h"

    #include <QtNetwork>
    #include <QApplication>
    #include "threadevent.h"

    SocketClientThread::SocketClientThread(int socketDescriptor, QObject *parent)
    : QThread(parent), socketDescriptor(socketDescriptor),m_socketServer( (QTcpSocket * )parent)
    {

    }

    QString SocketClientThread::getHost(QByteArray &arrayData)
    {
    QString host;
    QString str = arrayData.data();
    QRegExp rx("host\\s*:\\s*([A-Za-zА-Я0-9\.]+)[\n\r]+");

    if(rx.indexIn(str.toLower()) != 1) host = rx.cap(1);

    return host;
    }

    void SocketClientThread::run()
    {
    m_tcpSocket1 = new QTcpSocket();
    m_tcpSocket2 = new QTcpSocket();

    if (!m_tcpSocket1->setSocketDescriptor(socketDescriptor)) {
    emit error(m_tcpSocket1->error());
    return;
    }

    connect(m_tcpSocket1, SIGNAL(disconnected()),
    m_tcpSocket1, SIGNAL(destroyed()));


    connect(m_tcpSocket1, SIGNAL(disconnected()),
    this, SIGNAL(finished()));

    connect(m_tcpSocket1, SIGNAL(error(QAbstractSocket::SocketError)),
    this, SLOT(errorHandler1(QAbstractSocket::SocketError))
    ,Qt:irectConnection);

    connect(m_tcpSocket1, SIGNAL(readyRead()),
    this, SLOT(slotReadClient1())
    ,Qt:irectConnection);


    connect(m_tcpSocket2, SIGNAL(disconnected()),
    m_tcpSocket2, SIGNAL(destroyed()));

    connect(m_tcpSocket2, SIGNAL(readyRead()),
    this, SLOT(slotReadClient2())
    ,Qt:irectConnection);

    connect(m_tcpSocket2, SIGNAL(error(QAbstractSocket::SocketError)),
    this, SLOT(errorHandler2(QAbstractSocket::SocketError))
    ,Qt:irectConnection);
    exec();

    }

    void SocketClientThread::disconnectedHandler1()
    {
    // if(m_tcpSocket1 != NULL)
    // {
    // // m_tcpSocket1->deleteLater();
    // m_tcpSocket1->close();
    // delete m_tcpSocket1;
    // }

    // if(m_tcpSocket2 != NULL)
    // {
    // //m_tcpSocket2->deleteLater();
    // m_tcpSocket2->close();
    // delete m_tcpSocket2;

    // }
    // emit finished();
    //this->exit(0);
    // if(m_socketServer != NULL)
    // {
    // ThreadEvent * thEvent = new ThreadEvent() ;
    // QApplication:ostEvent(m_socketServer,thEvent);
    // }
    }


    void SocketClientThread::errorHandler1(QAbstractSocket: :SocketError socketError)
    {

    QString filePath;
    QTextStream(&filePath) << QDir::currentPath();
    QTextStream(&filePath) << "\\errorsLog.txt";
    QFile file(filePath);

    file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);

    if(file.isOpen() && file.isWritable())
    {
    QString error = m_tcpSocket1->errorString();
    error = "m_tcpSocket1 - " + error + "\n";

    file.write(error.toLatin1().data());
    file.close();
    }

    }

    void SocketClientThread::errorHandler2(QAbstractSocket: :SocketError socketError)
    {
    QString filePath;
    QTextStream(&filePath) << QDir::currentPath();
    QTextStream(&filePath) << "\\errorsLog.txt";
    QFile file(filePath);

    file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);

    if(file.isOpen() && file.isWritable())
    {
    QString error = m_tcpSocket2->errorString();
    error = "m_tcpSocket2 - " + error + "\n";
    file.write(error.toLatin1().data());
    file.close();
    }
    }

    void SocketClientThread::slotReadClient1()
    {
    QByteArray arrayData = m_tcpSocket1->readAll();
    QString filePath;
    QTextStream(&filePath) << QDir::currentPath();
    QTextStream(&filePath) << "\\logs.txt";
    QFile file(filePath);

    file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);

    if(file.isOpen() && file.isWritable())
    {
    file.write(arrayData);
    file.close();
    }

    QString host;
    QString str = arrayData.data();
    host = getHost(arrayData);

    if(m_tcpSocket2 == NULL)
    {
    m_tcpSocket2 = new QTcpSocket();

    connect(m_tcpSocket2, SIGNAL(disconnected()),
    m_tcpSocket2, SIGNAL(destroyed()));

    connect(m_tcpSocket2, SIGNAL(readyRead()),
    this, SLOT(slotReadClient2())
    ,Qt:irectConnection);

    connect(m_tcpSocket2, SIGNAL(error(QAbstractSocket::SocketError)),
    this, SLOT(errorHandler2(QAbstractSocket::SocketError))
    ,Qt:irectConnection);
    }

    if(!m_tcpSocket2->isOpen()) m_tcpSocket2->connectToHost(host,80);

    if(m_tcpSocket2->isOpen())
    {
    m_tcpSocket2->write(arrayData);
    }
    }

    void SocketClientThread::slotReadClient2()
    {
    QByteArray arrayData = m_tcpSocket2->readAll();
    QString filePath;
    QTextStream(&filePath) << QDir::currentPath();
    QTextStream(&filePath) << "\\logs.txt";
    QFile file(filePath);

    file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);

    if(file.isOpen() && file.isWritable())
    {
    file.write(arrayData);
    file.close();
    }

    m_tcpSocket1->write(arrayData);
    }

    ***************************
    threadevent.h

    #ifndef THREADEVENT_H
    #define THREADEVENT_H
    #include <QtGui>

    class ThreadEvent : public QEvent
    {
    public:
    enum {ThreadType = User + 1};
    ThreadEvent() : QEvent((Type)ThreadType)
    {
    }
    };

    #endif // THREADEVENT_H

    *******************
    ProxyServer.pro

    TEMPLATE = app
    SOURCES = main.cpp \
    socketclientthread.cpp \
    socketserver.cpp \
    dialog.cpp \

    QT += network
    HEADERS = \
    socketclientthread.h \
    socketserver.h \
    dialog.h \
    threadevent.h

    win32:TARGET = ProxyServer

  4. #4
    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: Problems with development proxy server.

    The 404 error is coming from which host? The first one you connect to, the second you attempt to connect to, or the proxy itself?

    Without delving into the logic of your program I can see you have fallen into a common trap. Your SocketClientThread::slotReadClient1() is connected to readyRead() and assumes that it always receives a complete line for getHost() to match. This may not be the case, and if this happens you will discard the partial request you have received. You need to accumulate received bytes until you can see a full request.

Similar Threads

  1. Qt Proxy server
    By saeedIRHA in forum Newbie
    Replies: 4
    Last Post: 9th October 2011, 11:32
  2. How to make a proxy server?
    By Passerby in forum Newbie
    Replies: 8
    Last Post: 17th September 2011, 14:54
  3. send request via ISA PROXY SERVER
    By rmagro in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2010, 22:56
  4. SOCKS5/proxy server in Qt4
    By chukkan in forum Qt Programming
    Replies: 1
    Last Post: 21st July 2009, 08:03
  5. Replies: 0
    Last Post: 22nd February 2009, 13:15

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.