在QT实现文件传输

来源:互联网 发布:网络管理视频教程下载 编辑:程序博客网 时间:2024/06/13 06:12

过程如下:

1、服务器端设置监听套接字,开始监听;

2、客户端在连接成功时开始传送文件,有connected()信号连接send()槽,send()发送文件头信息,包括文件名、文件总大小和文件名大小等;

3、传送完文件头信息时开始传送文件内容,有bytesWritten(qint64)信号连接到goOnSend(qint64)槽,前者是当想套接字写入数据时会出发的信号,即当已经想套接字写入数据,就继续传送数据,有send()传送文件头信息开始触发,直到文件传完为止。

4、在服务器端,首先接收文件头信息,然后读取文件名来用新建文件的方式打开一个文件,然后读取文件名即文件等大小信息,用来更新进度条和继续接收数据;

5、实现循环传输,在客户端,因为第一次send()是由connected()信号触发的,之后的每次传送应该手动调用send();在服务器端,在有新数据到达时,会判断是否为头文件,因此在每次文件传完的时候将byteReceived重置为0,即下一次再接收到数据的时候依据byteReceived判断的结果就是一个新文件了。

客户端代码:

[cpp] view plain copy
  1. #ifndef WIDGET_H  
  2. #define WIDGET_H  
  3.   
  4. #include <QWidget>  
  5. #include <QTcpSocket>  
  6. #include <QFile>  
  7. #include <string>  
  8.   
  9. namespace Ui {  
  10. class Widget;  
  11. }  
  12.   
  13. class Widget : public QWidget  
  14. {  
  15.     Q_OBJECT  
  16.   
  17. public:  
  18.     explicit Widget(QWidget *parent = 0);  
  19.     ~Widget();  
  20.       
  21. private:  
  22.     Ui::Widget *ui;  
  23.   
  24.     QTcpSocket *tcpClient;  
  25.     QFile *localFile;  
  26.     QString fileName;  //文件名  
  27.   
  28.     QByteArray outBlock;  //分次传  
  29.     qint64 loadSize;  //每次发送数据的大小  
  30.     qint64 byteToWrite;  //剩余数据大小  
  31.     qint64 totalSize;  //文件总大小  
  32.   
  33.     int sendTimes;  //用来标记是否为第一次发送,第一次以后连接信号触发,后面的则手动调用  
  34.   
  35. private slots:  
  36.     void send();  //传送文件头信息  
  37.     void goOnSend(qint64);  //传送文件内容  
  38.     void on_openPushButton_clicked();  
  39.     void on_sendPushButton_clicked();  
  40. };  
  41.   
  42. #endif // WIDGET_H  
widget.cpp

[cpp] view plain copy
  1. #include "widget.h"  
  2. #include "ui_widget.h"  
  3. #include <QHostAddress>  
  4. #include <QTextCodec>  
  5. #include <QFileDialog>  
  6.   
  7. Widget::Widget(QWidget *parent) :  
  8.     QWidget(parent),  
  9.     ui(new Ui::Widget)  
  10. {  
  11.     ui->setupUi(this);  
  12.   
  13.     ui->progressLabel->hide();  
  14.     QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF8"));
  15.   
  16.     tcpClient = new QTcpSocket(this);  
  17.     sendTimes = 0;  
  18.   
  19.     connect(tcpClient, SIGNAL(connected()), this, SLOT(send()));  //当连接成功时,就开始传送文件  
  20.     connect(tcpClient, SIGNAL(bytesWritten(qint64)), this, SLOT(goOnSend(qint64)));   
  21. }  
  22.   
  23. void Widget::send()  //发送文件头信息  
  24. {  
  25.     byteToWrite = localFile->size();  //剩余数据的大小  
  26.     totalSize = localFile->size();  
  27.   
  28.     loadSize = 4*1024;  //每次发送数据的大小  
  29.   
  30.     QDataStream out(&outBlock, QIODevice::WriteOnly);  
  31.     QString currentFileName = fileName.right(fileName.size() - fileName.lastIndexOf('/')-1);  
  32.   
  33.     out<<qint64(0)<<qint64(0)<<currentFileName;  
  34.   
  35.     totalSize += outBlock.size();  //总大小为文件大小加上文件名等信息大小  
  36.     byteToWrite += outBlock.size();  
  37.   
  38.     out.device()->seek(0);  //回到字节流起点来写好前面连个qint64,分别为总大小和文件名等信息大小  
  39.     out<<totalSize<<qint64(outBlock.size());  
  40.   
  41.     tcpClient->write(outBlock);  //将读到的文件发送到套接字  
  42.   
  43.     ui->progressLabel->show();  
  44.     ui->sendProgressBar->setMaximum(totalSize);  
  45.     ui->sendProgressBar->setValue(totalSize - byteToWrite);  
  46. }  
  47.   
  48. void Widget::goOnSend(qint64 numBytes)  //开始发送文件内容  
  49. {  
  50.     byteToWrite -= numBytes;  //剩余数据大小  
  51.     outBlock = localFile->read(qMin(byteToWrite, loadSize));  
  52.     tcpClient->write(outBlock);  
  53.   
  54.     ui->sendProgressBar->setMaximum(totalSize);  
  55.     ui->sendProgressBar->setValue(totalSize - byteToWrite);  
  56.   
  57.     if(byteToWrite == 0)  //发送完毕  
  58.         ui->sendStatusLabel->setText(tr("文件发送完毕!"));  
  59. }  
  60.   
  61. Widget::~Widget()  
  62. {  
  63.     delete ui;  
  64. }  
  65.   
  66. void Widget::on_openPushButton_clicked()  //打开文件并获取文件名(包括路径)  
  67. {  
  68.     ui->sendStatusLabel->setText(tr("正在打开文件..."));  
  69.     ui->sendProgressBar->setValue(0);  //非第一次发送  
  70.   
  71.     loadSize = 0;  
  72.     byteToWrite = 0;  
  73.     totalSize = 0;  
  74.     outBlock.clear();  
  75.   
  76.     fileName = QFileDialog::getOpenFileName(this);  
  77.     localFile = new QFile(fileName);  
  78.     localFile->open(QFile::ReadOnly);  
  79.   
  80.     ui->sendStatusLabel->setText(tr("已打开文件 %1").arg(fileName));  
  81. }  
  82.   
  83. void Widget::on_sendPushButton_clicked()  
  84. {  
  85.     if(sendTimes == 0)  //只有第一次发送的时候,是发生在连接产生信号connect时  
  86.     {  
  87.         tcpClient->connectToHost(QHostAddress("192.168.1.137"), 1000);  
  88.         sendTimes = 1;    
  89.     }  
  90.     else  
  91.         send();  //第一次发送的时候是由connectToHost出发connect信号才能调用send,第二次之后就需要调用send了  
  92.   
  93.     ui->sendStatusLabel->setText(tr("正在发送文件 %1").arg(fileName));  
  94. }  

服务端代码:

widget.h

[cpp] view plain copy
  1. #ifndef WIDGET_H  
  2. #define WIDGET_H  
  3.   
  4. #include <QWidget>  
  5. #include <QtNetwork/QTcpServer>  
  6. #include <QtNetwork/QTcpSocket>  
  7. #include <QFile>  
  8. #include <QString>  
  9.   
  10. namespace Ui {  
  11. class Widget;  
  12. }  
  13.   
  14. class Widget : public QWidget  
  15. {  
  16.     Q_OBJECT  
  17.       
  18. public:  
  19.     explicit Widget(QWidget *parent = 0);  
  20.     ~Widget();  
  21.       
  22. private:  
  23.     Ui::Widget *ui;  
  24.   
  25.     QTcpServer *server;  
  26.     QTcpSocket *receivedSocket;  
  27.     QFile *newFile;  
  28.   
  29.     QByteArray inBlock;  
  30.     QString fileName;  
  31.     qint64 totalSize;  //总共需要发送的文件大小(文件内容&文件名信息)  
  32.     qint64 byteReceived;  //已经发送的大小  
  33.   
  34. private slots:  
  35.     void acceptConnection();  
  36.     void readClient();  
  37.     void on_pushButton_clicked();  
  38. };  
  39.   
  40. #endif // WIDGET_H  


widget.cpp

[cpp] view plain copy
  1. #include "widget.h"  
  2. #include "ui_widget.h"  
  3. #include <QTextCodec>  
  4.   
  5. Widget::Widget(QWidget *parent) :  
  6.     QWidget(parent),  
  7.     ui(new Ui::Widget)  
  8. {  
  9.     ui->setupUi(this);  
  10.     ui->progressLabel->hide();   
  11.     QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF8"));
  12. }  
  13.   
  14. void Widget::acceptConnection()  
  15. {  
  16.     ui->receivedStatusLabel->setText(tr("已连接,正在准备接收文件!"));  
  17.   
  18.     receivedSocket = server->nextPendingConnection();  
  19.     connect(receivedSocket, SIGNAL(readyRead()), this, SLOT(readClient()));  
  20. }  
  21.   
  22. void Widget::readClient()  
  23. {  
  24.     ui->receivedStatusLabel->setText(tr("正在接收文件..."));  
  25.   
  26.     if(byteReceived == 0)  //才刚开始接收数据,此数据为文件信息  
  27.     {  
  28.         ui->receivedProgressBar->setValue(0);  
  29.   
  30.         QDataStream in(receivedSocket);  
  31.         in>>totalSize>>byteReceived>>fileName;    
  32.         newFile = new QFile(fileName);  
  33.         newFile->open(QFile::WriteOnly);  
  34.     }  
  35.     else  //正式读取文件内容  
  36.     {  
  37.         inBlock = receivedSocket->readAll();  
  38.   
  39.         byteReceived += inBlock.size();  
  40.         newFile->write(inBlock);  
  41.         newFile->flush();  
  42.     }   
  43.     ui->progressLabel->show();  
  44.     ui->receivedProgressBar->setMaximum(totalSize);  
  45.     ui->receivedProgressBar->setValue(byteReceived);  
  46.   
  47.     if(byteReceived == totalSize)  
  48.     {  
  49.         ui->receivedStatusLabel->setText(tr("%1接收完成").arg(fileName));  
  50.   
  51.         inBlock.clear();  
  52.         byteReceived = 0;  
  53.         totalSize = 0;  
  54.     }  
  55. }  
  56.   
  57. Widget::~Widget()  
  58. {  
  59.     delete ui;  
  60. }   
  61. void Widget::on_pushButton_clicked()  
  62. {  
  63.     totalSize = 0;  
  64.     byteReceived = 0;  
  65.   
  66.     server = new QTcpServer(this);  
  67.     server->listen(QHostAddress("192.168.1.137"), 1000);  
  68.   
  69.     connect(server, SIGNAL(newConnection()), this, SLOT(acceptConnection()));  
  70.   
  71.     ui->receivedProgressBar->setValue(0);  
  72.     ui->receivedStatusLabel->setText(tr("开始监听..."));  







原创粉丝点击