MySQL connection through QTcpSocket/QTcpServer ?

  • Hello.
    In first, sorry for my english.

    I try during all the weekend to realize a SSH tunnel and I'm blocked with the QTcp. So for my question, I talk about a simple port forwarding.

    I create a QTcpserver listening on the arbitrary port 4587. When a new connection occure, a slot create two sockets :

    • The first assuring the client connection (4587<->Appli)

    • The second assuring the server connection (Appli<->3306)

    Each socket have its associated slot assuring the listening (signal readyRead()) and transmit directly the data to the opposite.

    I don't understand why (but I know why I don't understand : I'm a newb) : if I transmit line by line (readLine directly injected in the QDatastream) : the connections are totaly blocked (tested for port 80HTTP & 3306SQL). If I pool the incomming data, it's the same for MySQL but not for HTTP (I have the html, whitout linked content like picture : it's normal I think).

    My objectif is juste to forward my MySQL_client:3306 <-to an arbitrary port-> 4587:MySQL_server. (the SSH tunnel is done and fonctionnal)

    Thanks for your help !



    #ifndef SERVERTCP_H
    #define SERVERTCP_H
    #include <QObject>
    #include <QTcpSocket>
    #include <QTcpServer>
    #include <QDebug>
    #include <QMutex>
    #include <stdio.h>
    #include <QApplication>
    #include <QVector>
    #include <QByteArray>
    #include <QBitArray>
    #include <QHostInfo>
    class serverTcp : public QTcpServer
    explicit serverTcp();
    public slots:
    void demande_connexion() ;
    void lecture();
    void lectureE();
    QTcpSocket *clientConnection;
    QTcpSocket *emitter;
    QDataStream texte;
    QDataStream texteE;
    #endif // SERVERTCP_H


    #include "servertcp.h"
    qDebug() << "Server on";
    QObject:: connect(this, SIGNAL(newConnection()),
    this, SLOT(demande_connexion()));
    void serverTcp :: demande_connexion()
    qDebug() << "Incoming connection";
    clientConnection = nextPendingConnection();
    qDebug() << "Feedback";
    emitter = new QTcpSocket();
    emitter->connectToHost("", 80, QTcpSocket::ReadWrite);
    qDebug() << "Listener initialization";
    QObject:: connect(clientConnection, SIGNAL(readyRead()), this, SLOT(lecture()));
    QObject:: connect(emitter, SIGNAL(readyRead()), this, SLOT(lectureE()));
    void serverTcp ::lecture()
    qDebug() << "Listenning...";
    QByteArray tempIn;
    // tempIn.append(clientConnection->readLine());
    if(clientConnection->readLine().length()) { texte.writeRawData(clientConnection->readLine(), clientConnection->readLine().length()); }
    //texte.writeRawData(tempIn, tempIn.length());
    qDebug() << tempIn << "-" << tempIn.length();
    void serverTcp ::lectureE()
    qDebug() << "Answers...";
    QByteArray tempOut;
    if(emitter->readLine().length() > 0) { texteE.writeRawData(emitter->readLine(), emitter->readLine().length()); }
    //texteE.writeRawData(tempOut, tempOut.length());
    qDebug() << tempOut << "-" << tempOut.length();


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Why not use openssh directly to create your tunnel ?

  • Hi.

    I wanted integrate the SSH tunnel into the software client.

    So it's done... I just arrived to do my SSH tunnel \o/

    I think, because I have spend 4 days on this problem, share the source with libssh.

    EDIt ; I don't know openssh, I haven't find c++ library...

  • Lifetime Qt Champion

    OpenSSH however, it's not a library

    Did you also check the possibility to setup a secure channel directly through the MySQL plugin ?

  • Yes. QSqlDatabase include a SSL connection method, but to do my connection through the hardware firewall, I must do a Tunnel on SSH protocol (always opened).

    All work, I post the "solution" here :

    For an "integrated" use, I must launch it by QProcess. It's is very efficiency for me, simple and I can get the stdout easly by the QProcess.

  • Lifetime Qt Champion

    Thanks for sharing your solution !

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.