12. Qt5 实现telnet的功能

QT += network
win32{
LIBS +=-lws2_32
}

在这里插入图片描述
mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "qttelnet.h"
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    QtTelnet * telnet ;
private:
    Ui::MainWindow *ui;

private slots:
    void telnetMessage(const QString &);
    void telnetLoginRequired(); //
    void telnetLoginFailed();  // 登录失败
    void telnetLoggedOut();  // 退出成功
    void telnetLoggedIn();  // 登录成功
    void telnetConnectionError(QAbstractSocket::SocketError);  // 连接错误
    void on_pb_Link_clicked();
    void on_pb_close_clicked();
    void on_pb_Send_clicked();
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    telnet = new QtTelnet(this);

    connect(telnet, SIGNAL(message(const QString &)),this, SLOT(telnetMessage(const QString &)));
    connect(telnet, SIGNAL(loginRequired()),this, SLOT(telnetLoginRequired()));
    connect(telnet, SIGNAL(loginFailed()),this, SLOT(telnetLoginFailed()));
    connect(telnet, SIGNAL(loggedOut()),this, SLOT(telnetLoggedOut()));
    connect(telnet, SIGNAL(loggedIn()),this, SLOT(telnetLoggedIn()));
    connect(telnet, SIGNAL(connectionError(QAbstractSocket::SocketError)),
            this, SLOT(telnetConnectionError(QAbstractSocket::SocketError)));

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::telnetMessage(const QString &msg)
{
    ui->te_Msg->append(msg);
}

void MainWindow::telnetLoginRequired()
{
    QString user = ui->le_User->text();
    QString passwd  = ui->le_Passwd->text();

    if(user == "" || passwd == "")
    {
        ui->te_Msg->append("用户名和密码不能为空!");
        return;
    }
    telnet->login(user,passwd); // 登入telnet
    // telnet->logout(); // 登出
}

void MainWindow::telnetLoginFailed()
{
    ui->te_Msg->append("登录失败!");
}

void MainWindow::telnetLoggedOut()
{
    ui->te_Msg->append("登出成功!");
}

void MainWindow::telnetLoggedIn()
{
    ui->te_Msg->append("登录成功!");
}

void MainWindow::telnetConnectionError(QAbstractSocket::SocketError error)
{
    ui->te_Msg->append(QString("连接错误: %1").arg(error));
}

void MainWindow::on_pb_Link_clicked()
{
    QString host = ui->le_Host->text();
    if(host == "")
    {
        ui->te_Msg->append("IP地址不能为空!");
        return ;
    }
    telnet->connectToHost(host);


}

void MainWindow::on_pb_close_clicked()
{
    telnet->close();
}

void MainWindow::on_pb_Send_clicked()
{
    QString cmd = ui->le_SendText->text() + "\r\n" ;
    telnet->sendData(cmd);
}

程序源码
提取码:2djg

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FlyingO123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值