tcpserver任意文件发送

上传人:xiao****1972 文档编号:84902499 上传时间:2019-03-05 格式:DOC 页数:11 大小:389KB
返回 下载 相关 举报
tcpserver任意文件发送_第1页
第1页 / 共11页
tcpserver任意文件发送_第2页
第2页 / 共11页
tcpserver任意文件发送_第3页
第3页 / 共11页
tcpserver任意文件发送_第4页
第4页 / 共11页
tcpserver任意文件发送_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《tcpserver任意文件发送》由会员分享,可在线阅读,更多相关《tcpserver任意文件发送(11页珍藏版)》请在金锄头文库上搜索。

1、QTcpServer网络编程在这里我们需要写一个客户端程序和一个服务器程序,我们让客户端进行文件的发送,服务器进行文件的接收。一、客户端这次我们先讲解客户端,在客户端里我们与服务器进行连接,一旦连接成功,就会发出connected()信号,这时我们就进行文件的发送。发送数据时候,我们要先发送文件的总大小,然后文件名长度,然后是文件名,这三部分我们合称为文件头结构,最后再发送文件数据。所以在发送函数里我们就要进行相应的处理,当然,在服务器的接收函数里我们也要进行相应的处理。对于文件大小,我们使用了qint64,它是64位的,可以表示一个很大的文件了。1、首先我们新建工程,将工程命名为“tcpSe

2、nder”。注意添加network模块。2、我们在widget.ui文件中将界面设计如下这里“主机”后的LineEdit的objectName为hostLineEdit; “端口”后的LineEdit的objectName为portLineEdit; 下面的ProgressBar的objectName为clientProgressBar,其value属性设为0;“状态”Label的objetName为clientStatusLabel;“打开”按钮的objectName为openButton;“发送”按钮的objectName为sendButton;3.在widget.h文件中进行更改。2、3

3、、3、完整程序如下所示(1)Widget.h#ifndef WIDGET_H#define WIDGET_H#include #include #include #includenamespace Ui class Widget;class Widget : public QWidget Q_OBJECTpublic: explicit Widget(QWidget *parent = 0); Widget();private: Ui:Widget *ui; QTcpSocket *tcpClient; QFile *localFile; /要发送的文件 qint64 totalBytes;

4、/数据总大小 qint64 bytesWritten; /已经发送数据大小 qint64 bytesToWrite; /剩余数据大小 qint64 loadSize; /每次发送数据的大小 QString fileName; /保存文件路径 QByteArray outBlock; /数据缓冲区,即存放每次要发送的数据private slots: void onSendFile(); void onOpenFile(); void startTransfer(); /发送文件大小等信息 void updateClientProgress(qint64); /发送数据,更新进度条 void di

5、splayError(QAbstractSocket:SocketError); /显示错误;#endif / WIDGET_H(2)Widget.cpp#include widget.h#include ui_widget.h#include #include #include static QByteArray formatLine(const QString& c) QString ret; ret.append(c).append(n); return ret.toLatin1();Widget:Widget(QWidget *parent) : QWidget(parent), ui

6、(new Ui:Widget) , tcpClient(0) , localFile(0) ui-setupUi(this); connect(ui-openButton, SIGNAL(clicked(), this, SLOT(onOpenFile(); connect(ui-sendButton, SIGNAL(clicked(), this, SLOT(onSendFile(); /开始使”发送“按钮不可用 ui-sendButton-setEnabled(false);Widget:Widget() delete ui;void Widget:startTransfer() /实现文

7、件大小等信息的发送 if (localFile) localFile-close(); delete localFile; localFile = new QFile(fileName); if(!localFile-open(QFile:ReadOnly) qDebug() fileName(); array.append(formatLine(QFileInfo(fileName).fileName(); / 文件名 array.append(formatLine(QString:number(localFile-size(); / 文件大小 array.append(n); / end

8、char tcpClient-write(array); tcpClient-waitForBytesWritten(); ui-clientProgressBar-setMaximum(localFile-size() + array.size();void Widget:updateClientProgress(qint64 numBytes) /更新进度条,实现文件的传送 bytesWritten += numBytes; static int readSize = 1024 *8; / 8K per times QByteArray array = localFile-read(rea

9、dSize); if (array.isEmpty() return; tcpClient-write(array); ui-clientProgressBar-setValue(bytesWritten); /更新进度条void Widget:displayError(QAbstractSocket:SocketError) /显示错误 qDebug() errorString(); sender()-deleteLater(); ui-clientProgressBar-reset(); ui-clientStatusLabel-setText(tr(客户端就绪); ui-sendButt

10、on-setEnabled(true);void Widget:onSendFile() bytesWritten = 0; tcpClient = new QTcpSocket(this); /当连接服务器成功时,发出connected()信号,我们开始传送文件 connect(tcpClient,SIGNAL(connected(),this,SLOT(startTransfer(); /当有数据发送成功时,我们更新进度条 connect(tcpClient,SIGNAL(bytesWritten(qint64),this, SLOT(updateClientProgress(qint64

11、); connect(tcpClient,SIGNAL(error(QAbstractSocket:SocketError),this, SLOT(displayError(QAbstractSocket:SocketError); ui-clientStatusLabel-setText(tr(连接中); tcpClient-connectToHost(ui-hostLineEdit-text(), ui-portLineEdit-text().toInt();/连接void Widget:onOpenFile() fileName = QFileDialog:getOpenFileName

12、(this); if(!fileName.isEmpty() ui-sendButton-setEnabled(true); ui-clientStatusLabel-setText(tr(打开文件 %1 成功!).arg(fileName); (3)main.cpp#include #include widget.h#include int main(int argc, char *argv) QApplication a(argc, argv); QTextCodec:setCodecForTr(QTextCodec:codecForLocale(); Widget w; w.show()

13、; return a.exec();4、在pro中添加下面语句:QT += core guiQT += network5、运行程序,效果如下。 4、程序整体思路分析。我们设计好界面,然后按下“打开”按钮,选择我们要发送的文件,这时调用了openFile()函数。然后我们点击“发送”按钮,调用send()函数,与服务器进行连接。当连接成功时就会发出connected()信号,这时就会执行startTransfer()函数,进行文件头结构的发送,当发送成功时就会发出bytesWritten(qint64)信号,这时我们执行updateClientProgress(qint64numBytes)进行文件数据的传输和进度条的更新。这里使用了一个loadSize变量,我们在构造函数中将其初始化为4*1024即4字节,它的作用

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号