我一直在Linux机器上通过ssh命令连接到网络:
ssh user@host.colo.edu
然后我在终端中提示输入密码:
mypassword
此时,我已连接到网络。 然后,我使用以下命令运行netcat(一个简单的Unix实用程序,使用TCP或UDP协议跨网络连接读取和写入数据):
nc -l -p 1313
其中1313是端口号,JSON消息开始出现在我的终端窗口中。 我想用Qt得到类似的结果。我愿意接受建议,但我相信我需要将QTcpSocket与QNetwork Proxy一起使用。这是我的mainwindow.cpp(我已经将主机,用户和密码更改为通用的):
#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...";
}
这是我的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
以下是debug的输出:
Debugging starts
state changed...
state changed...
found host...
state changed...
connected...
Debugging has finished
我没有任何错误或身份验证请求,但readyRead信号永远不会发出,而readyReadSlot永远不会被调用?我错过了什么吗?