QT代码
下面展示一些 客户端的代码 。 因为是TCP通信,要在工程文件加上netwo
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include<QTcpSocket>
#include<QLabel>
#include<QFile>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void senddata();
private:
Ui::MainWindow *ui;
QTcpSocket *tcp;
QLabel *client_status;
QString filename;
quint64 filesize;
quint64 sendsize;
QFile *file;
};
#endif
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHostAddress>
#include<QPixmap>
#include<QFile>
#include<QFileDialog>
#include<QMessageBox>
#include<QDebug>
#include<QTimer>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
setFixedSize(500,600);
ui->setupUi(this);
ui->port->setText("8899");
ui->ip->setText("192.168.0.55");
client_status = new QLabel(this);
client_status->setPixmap(QPixmap(":/greene.jfif").scaled(20,20));
ui->statusbar->addWidget(new QLabel("连接状态"));\
ui->statusbar->addWidget(client_status);
tcp = new QTcpSocket(this);
connect(ui->choose_pushbutton,&QPushButton::clicked,this,[=](){
QString path = QFileDialog::getOpenFileName(this,"打开文件"," D:\\");
if(path.isEmpty())
{
QMessageBox::information(this,"提示",path+"文件路径无效");
}
file = new QFile(path);
filename.clear();
filesize = 0;
sendsize = 0;
QFileInfo info(path);
filename = info.fileName();
filesize = info.size();
qDebug()<<filesize;
bool isOk = file->open(QIODevice::ReadOnly);
if(!isOk){
QMessageBox::information(this, "提示","只读方式打开文件失败");
return ;
}
ui->record_text->append(path);
});
connect(tcp,&QTcpSocket::readyRead,this,[=](){
ui->record_text->append("服务器说"+tcp->readAll());
});
connect(ui->cleaar_button,&QPushButton::clicked,this,[=](){\
ui->record_text->clear();
});
connect(tcp,&QTcpSocket::disconnected,this,[=](){
tcp->close();
tcp->deleteLater();
});
connect(ui->disconnect_button,&QPushButton::clicked,this,[=](){
tcp->disconnected();
client_status->setPixmap(QPixmap(":/greene.jfif").scaled(20,20));
ui->statusbar->addWidget(client_status);
ui->connect_server_button->setEnabled(true);
});
connect(ui->connect_server_button,&QPushButton::clicked,this,[=](){
unsigned short port = ui->port->text().toUShort();
QString ip = ui->ip->text();
tcp->connectToHost(QHostAddress(ip),port);
connect(tcp,&QTcpSocket::connected,this,[=](){
client_status->setPixmap(QPixmap(":/red.jfif").scaled(20,20));
ui->statusbar->addWidget(client_status);
ui->connect_server_button->setDisabled(true);
});
connect(ui->send_messag_button,&QPushButton::clicked,this,[=](){
QString mes = ui->message_text->toPlainText();
tcp->write(mes.toUtf8());
ui->record_text->append("客户端说"+mes);
});
connect(ui->send_file_button,&QPushButton::clicked,this,[=](){
QString head = QString("%1##%2").arg(filename).arg(filesize);
quint64 len = tcp->write(head.toUtf8());
QTimer timer;
if(len>0)
{
timer.start(20);
}
else if(len<=0)
{
QMessageBox::information(this, "提示","头部信息发送失败");
file->close();
return ;
}
if(len>0)
{
char buf[3000] = {0};
len = 0;
len = file->read(buf,sizeof(buf));
len = tcp->write(buf,len);
sendsize += len;
}
});
});
}
MainWindow::~MainWindow()
{
delete ui;
}
```bash
|