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. QTcpSocket never emits readyRead when connected
QtWS25 Last Chance

QTcpSocket never emits readyRead when connected

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtcpsoketqnetworkpoxyreadyread
6 Posts 2 Posters 2.7k Views
  • 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.
  • K Offline
    K Offline
    KirstenS
    wrote on 13 Jun 2017, 16:56 last edited by kshegunov
    #1

    I have been connecting to a network through an ssh command on a Linux machine:

    ssh user@host.colo.edu
    and then I am prompted in the terminal for a password:

    mypassword
    at which point, I am connected to the network. I then run netcat (a simple Unix utility that reads and writes data across network connections, using the TCP or UDP protocol) with the command:

    nc -l -p 1313
    where 1313 is the port number and JSON messages begin to appear in my terminal window. I would like to use Qt to have a similar result. I am open to suggestions, but I believe I need to use QTcpSocket with a QNetwork Proxy. Here is my mainwindow.cpp (I have changed the host, user, and password to something generic):

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        serverSocket = new QTcpSocket(this);
        connect(serverSocket,SIGNAL(readyRead()),this,SLOT(readyReadSlot()));
        connect(serverSocket,SIGNAL(connected()),this,SLOT(connectedSlot()));
        connect(serverSocket, SIGNAL(hostFound()),this,SLOT(hostFoundSlot()));
        connect(serverSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(errorSlot(QAbstractSocket::SocketError)));
        connect(serverSocket,SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),this,SLOT(proxyAuthenticationRequiredSlot(QNetworkProxy,QAuthenticator*)));
        connect(serverSocket,SIGNAL(stateChanged(QAbstractSocket::SocketState)),this,SLOT(stateChangedSlot(QAbstractSocket::SocketState)));
        QNetworkProxy proxy;
        proxy.setHostName("host.colo.edu");
        proxy.setPort(1313);
        proxy.setUser("user");
        proxy.setPassword("mypassword");
        serverSocket->setProxy(proxy);
        serverSocket->connectToHost("host.colo.edu",1313);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::readyReadSlot()
    {
        qDebug() << "reading...";
        while (!serverSocket->atEnd())
        {
         QByteArray data = serverSocket->read(100);
         int istop = 0;
        }
    }
    
    void MainWindow::connectedSlot()
    {
        qDebug() << "connected...";
    }
    
    void MainWindow::hostFoundSlot()
    {
        qDebug() << "found host...";
    }
    
    void MainWindow::errorSlot(QAbstractSocket::SocketError)
    {
        qDebug() << "an error...";
    }
    
    void MainWindow::proxyAuthenticationRequiredSlot(QNetworkProxy, QAuthenticator *)
    {
        qDebug() << "authentication...";
    }
    
    void MainWindow::stateChangedSlot(QAbstractSocket::SocketState socketState)
    {
        qDebug() << "state changed...";
    }
    

    And here is my mainwindow.h:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QTcpSocket>
    #include <QNetworkProxy>
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
        QTcpSocket *serverSocket;
    public slots:
        void readyReadSlot();
        void connectedSlot();
        void hostFoundSlot();
        void errorSlot(QAbstractSocket::SocketError);
        void proxyAuthenticationRequiredSlot(QNetworkProxy,QAuthenticator*);
        void stateChangedSlot(QAbstractSocket::SocketState socketState);
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    

    And here is the output from debug:

    Debugging starts
    state changed...
    state changed...
    found host...
    state changed...
    connected...
    Debugging has finished

    At no point do I have any errors or authentication requests, but readyRead signal never emits and readyReadSlot never gets called? Am I missing something?

    [Code tags added ~kshegunov]

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 13 Jun 2017, 21:30 last edited by
      #2

      Hi and welcome to devnet,

      Something strange here, you seem to log through SSH and then, in your code, you are using a proxy which is not the same thing AFAIK.

      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
      • K Offline
        K Offline
        KirstenS
        wrote on 13 Jun 2017, 22:39 last edited by
        #3

        Thanks. I want to read messages across a network connection. I was explaining how I can see the messages on Linux. I first ssh into the network and then I can run netcat , a simple Unix utility that reads and writes data across network connections, using the TCP or UDP protocol.
        Based on that description, I assumed I would need a QTcpSocket. I want to use my Windows computer and my Qt application to achieve the same end. I don't think I need to ssh, but that is the method I use in Linux. What do you suggest instead?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 14 Jun 2017, 14:42 last edited by
          #4

          First you need to determine whether that machine is accessible from the outside on that port and how.

          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
          • K Offline
            K Offline
            KirstenS
            wrote on 15 Jun 2017, 17:50 last edited by
            #5

            Thanks for explaining! Reading data across network connections is a new endeavor for me. I am confused as to what you mean by accessible to the outside on that port. I thought since I had successfully connected using that port number that would mean that it is accessible. My qdebug messages imply I have connected.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 16 Jun 2017, 21:43 last edited by
              #6

              There's one other thing that is strange, the documentation of netcat explicitly state that you should not use -p and -l together. So what exactly are you connecting to ?

              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

              4/6

              14 Jun 2017, 14:42

              • Login

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