界面编程之QT的Socket通信20180730

时间:2023-03-09 02:18:29
界面编程之QT的Socket通信20180730

/*******************************************************************************************/

一、linux下的tcp通信过程

其中bind绑定,会固定一个端口,否则是随机的。

一个链接是由双方的ip和端口组成的,固定端口保证源的不变性,

这样另一端在任何时候访问的目的都是一致的,也可以说这个端口提供了什么服务。

同时绑定后直接操作socket id就可以操作对应的链接了。

/*******************************************************************************************/

二、QT下的TCP通信过程

Qt中提供的所有的Socket类都是非阻塞的。

Qt中常用的用于socket通信的套接字类:

QTcpServer

用于TCP/IP通信, 作为服务器端套接字使用

QTcpSocket

用于TCP/IP通信,作为客户端套接字使用。

QUdpSocket

用于UDP通信,服务器,客户端均使用此套接字。

1.QT下的服务端

1).socket函数变为QTcpServer

2).bind ,listen 统一为listen

同时没有accept,当有一个链接过来的时候,会产生一个信号:newconnection,可以从对应的槽函数中取出建立好的套接字(对方的)QTcpSocket

如果成功和对方建立好链接,通信套接字会自动触发connected信号

3).read :

对方发送数据过来,链接的套接字(通信套接字)就会触发(本机的)readyRead信号,需要在对应的槽函数中接收数据

4).write,

发送数据,对方的(客户端的)套接字(通信套接字)就会触发readyRead信号,需要在对应的槽函数中接收数据

如果对方主动断开连接,对方的(客户端的)套接字(通信套接字)会自动触发disconnected信号

2.QT下的客户端:

1).socket函数变为 QTcpSocket

2).connect变为connetToHost()

如果成功和对方建立好链接,就会自动触发connected信号

3).read :

对方发送数据过来,链接的套接字(通信套接字)就会触发(本机的)readyRead信号,需要在对应的槽函数中接收数据

4).write,

发送数据,对方的(服务器的)套接字(通信套接字)就会触发readyRead信号,需要在对应的槽函数中接收数据

如果对方主动断开连接,就会自动触发disconnected信号

具体见图《QtTCP通信过程》

界面编程之QT的Socket通信20180730

/*******************************************************************************************/

三、TCP服务器

Qwidget是基类,比较干净,QMainWindow相对比较多。

如果输入头文件没有提示,就需要在项目文件中加入对应模块,同时再编译不运行一下,让qt可以构建并

加载对应的模块。

#include <QTcpServer> //监听套接字

#include <QTcpSocket> //通信套接字//对方的(客户端的)套接字(通信套接字)

//监听套接字,指定父对象,让其自动回收空间

tcpServer = new QTcpServer(this);

tcpServer->listen(QHostAddress::Any, 8888);

setWindowTitle("服务器: 8888");

connect(tcpServer, &QTcpServer::newConnection,

[=]()//信号无参数,这里也没有参数

{

//取出建立好连接的套接字

tcpSocket = tcpServer->nextPendingConnection();

//获取对方的IP和端口

QString ip = tcpSocket->peerAddress().toString();

qint16 port = tcpSocket->peerPort();

QString temp = QString("[%1:%2]:成功连接").arg(ip).arg(port);

ui->textEditRead->setText(temp);

//必须放在里面,因为建立好链接才能读,或者说tcpSocket有指向才能操作

connect(tcpSocket, &QTcpSocket::readyRead,

[=]()

{

//从通信套接字中取出内容

QByteArray array = tcpSocket->readAll();

ui->textEditRead->append(array);

}

);

}

);

void ServerWidget::on_buttonSend_clicked()

{

if(NULL == tcpSocket)

{

return;

}

//获取编辑区内容

QString str = ui->textEditWrite->toPlainText();

//给对方发送数据, 使用套接字是tcpSocket

tcpSocket->write( str.toUtf8().data() );

}

void ServerWidget::on_buttonClose_clicked()

{

if(NULL == tcpSocket)

{

return;

}

//主动和客户端断开连接

tcpSocket->disconnectFromHost();

tcpSocket->close();

tcpSocket = NULL;

}

/*******************************************************************************************/

四、TCP客户端

可以在项目中添加新文件中选择Qt--->Qt设计师界面类(这个是带ui的),选择这个后项目会多出一个ui

ui->setupUi(this);//显示ui

tcpSocket = NULL;

//分配空间,指定父对象

tcpSocket = new QTcpSocket(this);

setWindowTitle("客户端");

connect(tcpSocket, &QTcpSocket::connected,

[=]()

{

ui->textEditRead->setText("成功和服务器建立好连接");

}

);

//因为tcpSocket已经分配了空间,有指向,所以可以放在外面

connect(tcpSocket, &QTcpSocket::readyRead,

[=]()

{

//获取对方发送的内容

QByteArray array = tcpSocket->readAll();

//追加到编辑区中

ui->textEditRead->append(array);

}

);

void ClientWidget::on_buttonConnect_clicked()

{

//获取服务器ip和端口

QString ip = ui->lineEditIP->text();

qint16 port = ui->lineEditPort->text().toInt();

//主动和服务器建立连接

tcpSocket->connectToHost(QHostAddress(ip), port);

}

void ClientWidget::on_buttonSend_clicked()

{

//获取编辑框内容

QString str = ui->textEditWrite->toPlainText();

//发送数据

tcpSocket->write( str.toUtf8().data() );

}

void ClientWidget::on_buttonClose_clicked()

{

//主动和对方断开连接

tcpSocket->disconnectFromHost();

tcpSocket->close();//这里释放连接,前面connect的时候会建立连接

}

int main(int argc, char *argv[])

{

QApplication a(argc, argv);

ServerWidget w;

w.show();

ClientWidget w2;

w2.show();//显示另外一个窗口

return a.exec();

}

上述代码具体见《TCP》

 #ifndef SERVERWIDGET_H
#define SERVERWIDGET_H #include <QWidget>
#include <QTcpServer> //监听套接字
#include <QTcpSocket> //通信套接字 namespace Ui {
class ServerWidget;
} class ServerWidget : public QWidget
{
Q_OBJECT public:
explicit ServerWidget(QWidget *parent = );
~ServerWidget(); private slots:
void on_buttonSend_clicked(); void on_buttonClose_clicked(); private:
Ui::ServerWidget *ui; QTcpServer *tcpServer; //监听套接字
QTcpSocket *tcpSocket; //通信套接字 }; #endif // SERVERWIDGET_H

serverwidget.h

 #include "serverwidget.h"
#include "ui_serverwidget.h" ServerWidget::ServerWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::ServerWidget)
{
ui->setupUi(this); tcpServer = NULL;
tcpSocket = NULL; //监听套接字,指定父对象,让其自动回收空间
tcpServer = new QTcpServer(this); tcpServer->listen(QHostAddress::Any, ); setWindowTitle("服务器: 8888"); connect(tcpServer, &QTcpServer::newConnection,
[=]()
{
//取出建立好连接的套接字
tcpSocket = tcpServer->nextPendingConnection(); //获取对方的IP和端口
QString ip = tcpSocket->peerAddress().toString();
qint16 port = tcpSocket->peerPort();
QString temp = QString("[%1:%2]:成功连接").arg(ip).arg(port); ui->textEditRead->setText(temp); connect(tcpSocket, &QTcpSocket::readyRead,
[=]()
{
//从通信套接字中取出内容
QByteArray array = tcpSocket->readAll();
ui->textEditRead->append(array);
} ); } ); } ServerWidget::~ServerWidget()
{
delete ui;
} void ServerWidget::on_buttonSend_clicked()
{
if(NULL == tcpSocket)
{
return;
}
//获取编辑区内容
QString str = ui->textEditWrite->toPlainText();
//给对方发送数据, 使用套接字是tcpSocket
tcpSocket->write( str.toUtf8().data() ); } void ServerWidget::on_buttonClose_clicked()
{
if(NULL == tcpSocket)
{
return;
} //主动和客户端端口连接
tcpSocket->disconnectFromHost();
tcpSocket->close();
tcpSocket = NULL;
}

serverwidget.cpp

 #ifndef CLIENTWIDGET_H
#define CLIENTWIDGET_H #include <QWidget>
#include <QTcpSocket> //通信套接字 namespace Ui {
class ClientWidget;
} class ClientWidget : public QWidget
{
Q_OBJECT public:
explicit ClientWidget(QWidget *parent = );
~ClientWidget(); private slots:
void on_buttonConnect_clicked(); void on_buttonSend_clicked(); void on_buttonClose_clicked(); private:
Ui::ClientWidget *ui; QTcpSocket *tcpSocket; //通信套接字
}; #endif // CLIENTWIDGET_H

clientwidget.h

 #include "clientwidget.h"
#include "ui_clientwidget.h"
#include <QHostAddress> ClientWidget::ClientWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::ClientWidget)
{
ui->setupUi(this); tcpSocket = NULL; //分配空间,指定父对象
tcpSocket = new QTcpSocket(this); setWindowTitle("客户端"); connect(tcpSocket, &QTcpSocket::connected,
[=]()
{
ui->textEditRead->setText("成功和服务器建立好连接");
}
); connect(tcpSocket, &QTcpSocket::readyRead,
[=]()
{
//获取对方发送的内容
QByteArray array = tcpSocket->readAll();
//追加到编辑区中
ui->textEditRead->append(array);
} ); } ClientWidget::~ClientWidget()
{
delete ui;
} void ClientWidget::on_buttonConnect_clicked()
{
//获取服务器ip和端口
QString ip = ui->lineEditIP->text();
qint16 port = ui->lineEditPort->text().toInt(); //主动和服务器建立连接
tcpSocket->connectToHost(QHostAddress(ip), port); } void ClientWidget::on_buttonSend_clicked()
{
//获取编辑框内容
QString str = ui->textEditWrite->toPlainText();
//发送数据
tcpSocket->write( str.toUtf8().data() ); } void ClientWidget::on_buttonClose_clicked()
{
//主动和对方断开连接
tcpSocket->disconnectFromHost();
tcpSocket->close();
}

clientwidget.cpp

/*******************************************************************************************/

五、UDP通信过程

使用Qt提供的QUdpSocket进行UDP通信。在UDP方式下,客户端并不与服务器建立连接,它只负责调用发送函数向服务器发送数据。

类似的服务器也不从客户端接收连接,只负责调用接收函数,等待来自客户端的数据的到达。

在UDP通信中,服务器端和客户端的概念已经显得有些淡化,两部分做的工作都大致相同

1.QT下的服务端

socket函数变为QUdpSocket

bind ,还是bind,(固定端口,让别人可以知道往哪里发,客户端也可以绑定)

readDatagram :

对方发送数据过来,套接字就会触发readyRead信号,需要在对应的槽函数中接收数据

writeDatagram,

发送数据,对方的(客户端的)套接字就会触发readyRead信号,需要在对应的槽函数中接收数据

close 还是close

2.QT下的客户端:

socket函数变为 QUdpSocket

readDatagram :

对方发送数据过来,套接字就会触发readyRead信号,需要在对应的槽函数中接收数据

writeDatagram,

发送数据,对方的(客户端的)套接字就会触发readyRead信号,需要在对应的槽函数中接收数据

close 还是close

具体见图《QtUDP通信过程》

界面编程之QT的Socket通信20180730

/*******************************************************************************************/

六、UDP文本发送

UDP中没有严格的区分服务端和客户端。

关闭按钮是用于关闭窗口的,这主要是由于udp不是面向连接的,没有断开连接的说法。

#include <QUdpSocket> //UDP套接字

//分配空间,指定父对象,这是为了让父对象来回收,其实也可以不用指定,自己来回收资源也行

udpSocket = new QUdpSocket(this);

//绑定

udpSocket->bind(8888);

setWindowTitle("服务器端口为:8888");

//当对方成功发送数据过来

//自动触发 readyRead()

connect(udpSocket, &QUdpSocket::readyRead, this, &Widget::dealMsg);

void Widget::dealMsg()

{

//读取对方发送的内容

char buf[1024] = {0};

QHostAddress cliAddr; //对方地址

quint16 port;    //对方端口

qint64 len = udpSocket->readDatagram(buf, sizeof(buf), &cliAddr, &port);

if(len > 0)

{

//格式化 [192.68.2.2:8888]aaaa

QString str = QString("[%1:%2] %3")

.arg(cliAddr.toString())

.arg(port)

.arg(buf);

//给编辑区设置内容

ui->textEdit->setText(str);

}

}

//发送按钮

void Widget::on_buttonSend_clicked()

{

//先获取对方的IP和端口

QString ip = ui->lineEditIP->text();

qint16 port = ui->lineEditPort->text().toInt();

//获取编辑区内容

QString str = ui->textEdit->toPlainText();

//给指定的IP发送数据

udpSocket->writeDatagram(str.toUtf8(), QHostAddress(ip), port);

}

/*******************************************************************************************/

七、UDP多播组播

1.广播

广播地址:255.255.255.255,在某个局域网上就自动会变为那个局域网的广播地址,如果指定了

是某个局域网的广播地址如:192.168.1.255,则只能在这个局域网192.168.1.x上广播。

只要是网段是一样的,对应的端口就都会收到。

比如广播地址:255.255.255.255,端口8999,则其他同网段中的端口8999就会收到。

2.组播

总是广播容易造成网络阻塞,所以就需要组播了,另外,

我们再使用广播发送消息的时候会发送给所有用户,但是有些用户是不想接受消息的,这时候我们就应该使用组播,

接收方只有先注册到组播地址中才能收到组播消息,否则则接受不到消息。另外组播是可以在Internet中使用的。

组播地址属于D类地址,D类地址又分出其他的,关于组播地址的分类:

224.0.0.0~224.0.0.255为预留的组播地址(永久组地址),地址224.0.0.0保留不做分配,其它地址供路由协议使用;

224.0.1.0~224.0.1.255是公用组播地址,可以用于Internet;

224.0.2.0~238.255.255.255为用户可用的组播地址(临时组地址),全网范围内有效;

239.0.0.0~239.255.255.255为本地管理组播地址,仅在特定的本地范围内有效。

在使用QUdpSocket类的writeDatagram()函数发送数据的时候,其中第二个参数host应该指定为组播地址,

注册加入到组播地址需要使用QUdpSocket类的成员函数:

bool joinMulticastGroup(const QHostAddress & groupAddress)

现实生活中的qq群,拉在一起这种的,用的就是组播

绑定后加入某个组播,在组播内你发组成员就都能收到,其他组成员发你也会收到

//绑定

//udpSocket->bind(8888);//使用组播只能使用(绑定)ipv4的ip,不能使用任意的ip,所以这里注释掉

udpSocket->bind(QHostAddress::AnyIPv4, 8888);//所以这里就要指定为ipv4

//加入某个组播      //广播不需要加入的操作就直接能发能收

//组播地址是D类地址

udpSocket->joinMulticastGroup( QHostAddress("224.0.0.2") );//加入后,其他人就可以向这个ip以及绑定的端口发送数据了

//udpSocket->leaveMulticastGroup(QHostAddress("224.0.0.2")); //退出组播

上述代码具体见《UDP》

 #ifndef WIDGET_H
#define WIDGET_H #include <QWidget>
#include <QUdpSocket> //UDP套接字 namespace Ui {
class Widget;
} class Widget : public QWidget
{
Q_OBJECT public:
explicit Widget(QWidget *parent = );
~Widget(); void dealMsg(); //槽函数,处理对方发过来的数据 private slots:
void on_buttonSend_clicked(); private:
Ui::Widget *ui; QUdpSocket *udpSocket; //UDP套接字
}; #endif // WIDGET_H

widget.h

 #include "widget.h"
#include "ui_widget.h"
#include <QHostAddress> Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this); //分配空间,指定父对象
udpSocket = new QUdpSocket(this); //绑定
//udpSocket->bind(8888);
udpSocket->bind(QHostAddress::AnyIPv4, ); //加入某个组播
//组播地址是D类地址
udpSocket->joinMulticastGroup( QHostAddress("224.0.0.2") );
//udpSocket->leaveMulticastGroup(); //退出组播 setWindowTitle("服务器端口为:8888"); //当对方成功发送数据过来
//自动触发 readyRead()
connect(udpSocket, &QUdpSocket::readyRead, this, &Widget::dealMsg);
} void Widget::dealMsg()
{
//读取对方发送的内容
char buf[] = {};
QHostAddress cliAddr; //对方地址
quint16 port; //对方端口
qint64 len = udpSocket->readDatagram(buf, sizeof(buf), &cliAddr, &port);
if(len > )
{
//格式化 [192.68.2.2:8888]aaaa
QString str = QString("[%1:%2] %3")
.arg(cliAddr.toString())
.arg(port)
.arg(buf);
//给编辑区设置内容
ui->textEdit->setText(str);
} } Widget::~Widget()
{
delete ui;
} //发送按钮
void Widget::on_buttonSend_clicked()
{
//先获取对方的IP和端口
QString ip = ui->lineEditIP->text();
qint16 port = ui->lineEditPort->text().toInt(); //获取编辑区内容
QString str = ui->textEdit->toPlainText(); //给指定的IP发送数据
udpSocket->writeDatagram(str.toUtf8(), QHostAddress(ip), port); }

widget.cpp

/*******************************************************************************************/

八、QTimer定时器的使用

QTimer 定时器对象,相对于那个事件的定时器好用多了。多个定时器创建多个对象即可

#include <QTimer> //定时器对象

定时器对象里面有个timeout的信号,当设置的定时时间到了的时候就会发出这样的一个信号。

当然如果停止了这个定时器就不会发送。

myTimer = new QTimer(this);

i = 0;

connect(myTimer, &QTimer::timeout,

[=]()

{

i++;

ui->lcdNumber->display(i);

}

);

void Widget::on_buttonStart_clicked()

{

//启动定时器

//时间间隔为100ms

//每隔100ms,定时器myTimer内部自动触发timeout()信号

//如果定时器没有激活,才启动

if(myTimer->isActive() == false)

{

myTimer->start(100);

}

}

void Widget::on_buttonStop_clicked()

{

if(true == myTimer->isActive())

{

myTimer->stop();

i = 0;

}

}

上述代码具体见《QTimer》

 #ifndef WIDGET_H
#define WIDGET_H #include <QWidget>
#include <QTimer> //定时器对象 namespace Ui {
class Widget;
} class Widget : public QWidget
{
Q_OBJECT public:
explicit Widget(QWidget *parent = );
~Widget(); private slots:
void on_buttonStart_clicked(); void on_buttonStop_clicked(); private:
Ui::Widget *ui; QTimer *myTimer; //定时器对象
int i;
}; #endif // WIDGET_H

widget.h

 #include "widget.h"
#include "ui_widget.h" Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this); myTimer = new QTimer(this);
i = ; connect(myTimer, &QTimer::timeout,
[=]()
{
i++;
ui->lcdNumber->display(i);
} ); } Widget::~Widget()
{
delete ui;
} void Widget::on_buttonStart_clicked()
{
//启动定时器
//时间间隔为100ms
//每隔100ms,定时器myTimer自动触发timeout()
//如果定时器没有激活,才启动
if(myTimer->isActive() == false)
{
myTimer->start();
} } void Widget::on_buttonStop_clicked()
{
if(true == myTimer->isActive())
{
myTimer->stop();
i = ;
}
}

widget.cpp

/*******************************************************************************************/

九、TCP传文件流程图

tcp中当两包数据发送间隔很短的时候,接收的时候就会出现两个包粘在一起的情况,也就是粘包。

比如简单的解决方法是控制发送间隔,使用定时器延时(图形界面不要用sleep除非开线程)让不能粘在一起的包分开。

当然也可以通过在数据包中增加包头,包长,包校验,包尾等信息来保证每一包数据的准确性。

还有一种办法是,不在乎粘的数据(比如文件数据)放在一个链接里,需要区分出来的数据(比如命令或者信息数据)放在另一个tcp链接里,

具体见图《TCP传文件流程图》

界面编程之QT的Socket通信20180730

/*******************************************************************************************/

十、TCP传文件服务器

.......

//两个按钮都不能按,按钮颜色变灰并且不能按

ui->buttonFile->setEnabled(false);

ui->buttonSend->setEnabled(false);

.......

//成功连接后,才能按选择文件

ui->buttonFile->setEnabled(true);

.......

connect(&timer, &QTimer::timeout,

[=]()

{

//关闭定时器

timer.stop();

//发送文件

sendData();

}

);

.......

//选择文件的按钮

void ServerWidget::on_buttonFile_clicked()

{

QString filePath = QFileDialog::getOpenFileName(this, "open", "../");

if(false == filePath.isEmpty()) //如果选择文件路径有效

{

fileName.clear();

fileSize = 0;

//获取文件信息

QFileInfo info(filePath);

fileName = info.fileName(); //获取文件名字

fileSize = info.size(); //获取文件大小

sendSize = 0; //发送文件的大小

//只读方式打开文件

//指定文件的名字

file.setFileName(filePath);

//打开文件

bool isOk = file.open(QIODevice::ReadOnly);

if(false == isOk)

{

qDebug() << "只读方式打开文件失败 106";

}

//提示打开文件的路径

ui->textEdit->append(filePath);

ui->buttonFile->setEnabled(false);

ui->buttonSend->setEnabled(true);

}

else

{

qDebug() << "选择文件路径出错 118";

}

}

//发送文件按钮

void ServerWidget::on_buttonSend_clicked()

{

ui->buttonSend->setEnabled(false);

//先发送文件头信息  文件名##文件大小

QString head = QString("%1##%2").arg(fileName).arg(fileSize);

//发送头部信息

qint64 len = tcpSocket->write( head.toUtf8() );

if(len > 0)//头部信息发送成功

{

//发送真正的文件信息

//防止TCP黏包

//需要通过定时器延时 20 ms

timer.start(20);

}

else

{

qDebug() << "头部信息发送失败 142";

file.close();

ui->buttonFile->setEnabled(true);

ui->buttonSend->setEnabled(false);

}

}

void ServerWidget::sendData()

{

ui->textEdit->append("正在发送文件……");

qint64 len = 0;

do

{

//每次发送数据的大小

char buf[4*1024] = {0};

len = 0;

//往文件中读数据

len = file.read(buf, sizeof(buf));

//发送数据,读多少,发多少

len = tcpSocket->write(buf, len);

//发送的数据需要累积

sendSize += len;

}while(len > 0);

//     //是否发送文件完毕

//     if(sendSize == fileSize)

//     {

//         ui->textEdit->append("文件发送完毕");

//         file.close();

//         //把客户端端口

//         tcpSocket->disconnectFromHost();

//         tcpSocket->close();

//     }

}

/*******************************************************************************************/

十一、TCP传文件客户端

connect(tcpSocket, &QTcpSocket::readyRead,

[=]()

{

//取出接收的内容

QByteArray buf = tcpSocket->readAll();

if(true == isStart)

{//接收头

isStart = false;

//解析头部信息 QString buf = "hello##1024"

//    QString str = "hello##1024#mike";

//    str.section("##", 0, 0);//"##"分段符号,0第一段开始,0第一段结束,所以取出来是hello

//初始化

//文件名

fileName = QString(buf).section("##", 0, 0);

//文件大小

fileSize = QString(buf).section("##", 1, 1).toInt();

recvSize = 0;   //已经接收文件大小

//打开文件

//关联文件名字

file.setFileName(fileName);

//只写方式方式,打开文件

bool isOk = file.open(QIODevice::WriteOnly);

if(false == isOk)

{

qDebug() << "WriteOnly error 49";

tcpSocket->disconnectFromHost(); //断开连接

tcpSocket->close(); //关闭套接字

return; //如果打开文件失败,中断函数

}

//弹出对话框,显示接收文件的信息

QString str = QString("接收的文件: [%1: %2kb]").arg(fileName).arg(fileSize/1024);

QMessageBox::information(this, "文件信息", str);

//设置进度条

ui->progressBar->setMinimum(0); //最小值

ui->progressBar->setMaximum(fileSize/1024); //最大值

ui->progressBar->setValue(0); //当前值

}

else //文件信息

{

qint64 len = file.write(buf);

if(len >0) //接收数据大于0

{

recvSize += len; //累计接收大小

qDebug() << len;

}

//更新进度条

ui->progressBar->setValue(recvSize/1024);

if(recvSize == fileSize) //文件接收完毕

{

//先给服务发送(接收文件完成的信息)

tcpSocket->write("file done");

QMessageBox::information(this, "完成", "文件接收完成");

file.close(); //关闭文件

//断开连接

tcpSocket->disconnectFromHost();

tcpSocket->close();

}

}

}

);

/*******************************************************************************************/

十二、TCP传文件进度条和黏包

//注意设置进度条使用除以1024的方法,不然太大,因为有可能文件太大,而进度条的那个值是int的

.......

//设置进度条

ui->progressBar->setMinimum(0); //最小值

ui->progressBar->setMaximum(fileSize/1024); //最大值//注意使用除以1024的方法,不然太大

ui->progressBar->setValue(0); //当前值

.......

//更新进度条

ui->progressBar->setValue(recvSize/1024);//注意使用除以1024的方法,不然太大

.......

上述代码具体见《TCPFile》

 #include "serverwidget.h"
#include <QApplication>
#include "clientwidget.h" int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ServerWidget w;
w.show(); ClientWidget w2;
w2.show(); return a.exec();
}

main.cpp

 #ifndef SERVERWIDGET_H
#define SERVERWIDGET_H #include <QWidget>
#include <QTcpServer> //监听套接字
#include <QTcpSocket> //通信套接字
#include <QFile>
#include <QTimer> namespace Ui {
class ServerWidget;
} class ServerWidget : public QWidget
{
Q_OBJECT public:
explicit ServerWidget(QWidget *parent = );
~ServerWidget(); void sendData(); //发送文件数据 private slots:
void on_buttonFile_clicked(); void on_buttonSend_clicked(); private:
Ui::ServerWidget *ui; QTcpServer *tcpServer; //监听套接字
QTcpSocket *tcpSocket; //通信套接字 QFile file; //文件对象
QString fileName; //文件名字
qint64 fileSize; //文件大小
qint64 sendSize; //已经发送文件的大小 QTimer timer; //定时器 }; #endif // SERVERWIDGET_H

serverwidget.h

 #include "serverwidget.h"
#include "ui_serverwidget.h"
#include <QFileDialog>
#include <QDebug>
#include <QFileInfo> ServerWidget::ServerWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::ServerWidget)
{
ui->setupUi(this); //监听套接字
tcpServer = new QTcpServer(this); //监听
tcpServer->listen(QHostAddress::Any, );
setWindowTitle("服务器端口为:8888"); //两个按钮都不能按
ui->buttonFile->setEnabled(false);
ui->buttonSend->setEnabled(false); //如果客户端成功和服务器连接
//tcpServer会自动触发 newConnection()
connect(tcpServer, &QTcpServer::newConnection,
[=]()
{
//取出建立好连接的套接字
tcpSocket = tcpServer->nextPendingConnection();
//获取对方的ip和端口
QString ip = tcpSocket->peerAddress().toString();
quint16 port = tcpSocket->peerPort(); QString str = QString("[%1:%2] 成功连接").arg(ip).arg(port);
ui->textEdit->setText(str); //显示到编辑区 //成功连接后,才能按选择文件
ui->buttonFile->setEnabled(true); connect(tcpSocket, &QTcpSocket::readyRead,
[=]()
{
//取客户端的信息
QByteArray buf = tcpSocket->readAll();
if(QString(buf) == "file done")
{//文件接收完毕
ui->textEdit->append("文件发送完毕");
file.close(); //断开客户端端口
tcpSocket->disconnectFromHost();
tcpSocket->close();
} } ); }
); connect(&timer, &QTimer::timeout,
[=]()
{
//关闭定时器
timer.stop(); //发送文件
sendData();
} ); } ServerWidget::~ServerWidget()
{
delete ui;
} //选择文件的按钮
void ServerWidget::on_buttonFile_clicked()
{
QString filePath = QFileDialog::getOpenFileName(this, "open", "../");
if(false == filePath.isEmpty()) //如果选择文件路径有效
{
fileName.clear();
fileSize = ; //获取文件信息
QFileInfo info(filePath);
fileName = info.fileName(); //获取文件名字
fileSize = info.size(); //获取文件大小 sendSize = ; //发送文件的大小 //只读方式打开文件
//指定文件的名字
file.setFileName(filePath); //打开文件
bool isOk = file.open(QIODevice::ReadOnly);
if(false == isOk)
{
qDebug() << "只读方式打开文件失败 106";
} //提示打开文件的路径
ui->textEdit->append(filePath); ui->buttonFile->setEnabled(false);
ui->buttonSend->setEnabled(true); }
else
{
qDebug() << "选择文件路径出错 118";
} }
//发送文件按钮
void ServerWidget::on_buttonSend_clicked()
{
ui->buttonSend->setEnabled(false); //先发送文件头信息 文件名##文件大小
QString head = QString("%1##%2").arg(fileName).arg(fileSize);
//发送头部信息
qint64 len = tcpSocket->write( head.toUtf8() );
if(len > )//头部信息发送成功
{
//发送真正的文件信息
//防止TCP黏包
//需要通过定时器延时 20 ms
timer.start(); }
else
{
qDebug() << "头部信息发送失败 142";
file.close();
ui->buttonFile->setEnabled(true);
ui->buttonSend->setEnabled(false);
}
} void ServerWidget::sendData()
{
ui->textEdit->append("正在发送文件……");
qint64 len = ;
do
{
//每次发送数据的大小
char buf[*] = {};
len = ; //往文件中读数据
len = file.read(buf, sizeof(buf));
//发送数据,读多少,发多少
len = tcpSocket->write(buf, len); //发送的数据需要累积
sendSize += len; }while(len > ); // //是否发送文件完毕
// if(sendSize == fileSize)
// {
// ui->textEdit->append("文件发送完毕");
// file.close(); // //把客户端端口
// tcpSocket->disconnectFromHost();
// tcpSocket->close();
// } }

serverwidget.cpp

 #ifndef CLIENTWIDGET_H
#define CLIENTWIDGET_H #include <QWidget>
#include <QTcpSocket>
#include <QFile> namespace Ui {
class ClientWidget;
} class ClientWidget : public QWidget
{
Q_OBJECT public:
explicit ClientWidget(QWidget *parent = );
~ClientWidget(); private slots:
void on_buttonConnect_clicked(); private:
Ui::ClientWidget *ui; QTcpSocket *tcpSocket; QFile file; //文件对象
QString fileName; //文件名字
qint64 fileSize; //文件大小
qint64 recvSize; //已经接收文件的大小 bool isStart; //标志位,是否为头部信息
}; #endif // CLIENTWIDGET_H

clientwidget.h

 #include "clientwidget.h"
#include "ui_clientwidget.h"
#include <QDebug>
#include <QMessageBox>
#include <QHostAddress> ClientWidget::ClientWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::ClientWidget)
{
ui->setupUi(this); tcpSocket = new QTcpSocket(this); isStart = true; ui->progressBar->setValue(); //当前值 setWindowTitle("客户端"); connect(tcpSocket, &QTcpSocket::readyRead,
[=]()
{
//取出接收的内容
QByteArray buf = tcpSocket->readAll(); if(true == isStart)
{//接收头
isStart = false;
//解析头部信息 QString buf = "hello##1024"
// QString str = "hello##1024#mike";
// str.section("##", 0, 0) //初始化
//文件名
fileName = QString(buf).section("##", , );
//文件大小
fileSize = QString(buf).section("##", , ).toInt();
recvSize = ; //已经接收文件大小 //打开文件
//关联文件名字
file.setFileName(fileName); //只写方式方式,打开文件
bool isOk = file.open(QIODevice::WriteOnly);
if(false == isOk)
{
qDebug() << "WriteOnly error 49"; tcpSocket->disconnectFromHost(); //断开连接
tcpSocket->close(); //关闭套接字 return; //如果打开文件失败,中断函数
} //弹出对话框,显示接收文件的信息
QString str = QString("接收的文件: [%1: %2kb]").arg(fileName).arg(fileSize/);
QMessageBox::information(this, "文件信息", str); //设置进度条
ui->progressBar->setMinimum(); //最小值
ui->progressBar->setMaximum(fileSize/); //最大值
ui->progressBar->setValue(); //当前值 }
else //文件信息
{
qint64 len = file.write(buf);
if(len >) //接收数据大于0
{
recvSize += len; //累计接收大小
qDebug() << len;
} //更新进度条
ui->progressBar->setValue(recvSize/); if(recvSize == fileSize) //文件接收完毕
{ //先给服务发送(接收文件完成的信息)
tcpSocket->write("file done"); QMessageBox::information(this, "完成", "文件接收完成");
file.close(); //关闭文件
//断开连接
tcpSocket->disconnectFromHost();
tcpSocket->close(); }
} } ); } ClientWidget::~ClientWidget()
{
delete ui;
} void ClientWidget::on_buttonConnect_clicked()
{
//获取服务器的ip和端口
QString ip = ui->lineEditIP->text();
quint16 port = ui->lineEditPort->text().toInt(); //主动和服务器连接
tcpSocket->connectToHost(QHostAddress(ip), port); isStart = true; //设置进度条
ui->progressBar->setValue();
}

clientwidget.cpp