QTcpSocket never emits readyRead when connected
-
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]
-
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.
-
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? -
First you need to determine whether that machine is accessible from the outside on that port and how.
-
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.
-
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 ?