Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. MySQL connection through QTcpSocket/QTcpServer ?
Forum Updated to NodeBB v4.3 + New Features

MySQL connection through QTcpSocket/QTcpServer ?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tran
    wrote on last edited by Tran
    #1

    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 !

    Tran


    serverTcp.h


    #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
    {
    Q_OBJECT
    public:
    explicit serverTcp();
    ~serverTcp();
    signals:
    public slots:
    void demande_connexion() ;
    void lecture();
    void lectureE();
    private:
    QTcpSocket *clientConnection;
    QTcpSocket *emitter;
    QDataStream texte;
    QDataStream texteE;
    };
    #endif // SERVERTCP_H


    serverTcp.cpp


    #include "servertcp.h"
    serverTcp::serverTcp()
    {
    qDebug() << "Server on";
    listen(QHostAddress::Any,4587);
    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("127.0.0.1", 80, QTcpSocket::ReadWrite);
    texte.setDevice(emitter);
    texteE.setDevice(clientConnection);
    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;
    while(clientConnection->canReadLine())
    {
    // 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;
    while(emitter->canReadLine())
    {
    //tempOut.append(emitter->readLine());
    if(emitter->readLine().length() > 0) { texteE.writeRawData(emitter->readLine(), emitter->readLine().length()); }
    }
    //texteE.writeRawData(tempOut, tempOut.length());
    qDebug() << tempOut << "-" << tempOut.length();
    }
    serverTcp::~serverTcp()
    {

    }

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Why not use openssh directly to create your tunnel ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Tran
        wrote on last edited by Tran
        #3

        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...

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by SGaist
          #4

          OpenSSH however, it's not a library

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • T Offline
            T Offline
            Tran
            wrote on last edited by
            #5

            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 : http://www.qtfr.org/viewtopic.php?id=17959.

            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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Thanks for sharing your solution !

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved