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?

KirstenS
  • 145
  • 1
  • 1
  • 7

0 Answers0