資源簡介
Qt實現的文件傳輸和客戶端和服務器端的對話,使用TCP協議,使用多線程

代碼片段和文件信息
#include?“Client.h“
#include?
#include?
#include?
#include?
Client::Client(Qobject?*parent)
????:?QTcpSocket(parent)
{
????sendfile=false;
????sendmsg=false;
????connect(this?SIGNAL(connected())?this?SLOT(on_connected()));
????connect(this?SIGNAL(error(QAbstractSocket::SocketError))
????????????this?SLOT(displaySocketError(QAbstractSocket::SocketError)));
}
void?Client::sendFile(const?QString?&file?const?QHostAddress?&dest)
{
????filePath?=?file;
????sendfile=true;
????connectToHost(dest?5234);
????qDebug()?<“Send?file?“?<}
void?Client::sendFile(const?QString?&file?const?QString?&dest)
{
????filePath?=?file;
????sendfile=true;
????connectToHost(dest?5234);
????qDebug()?<“Send?file?“?<}
void?Client::sendMsg(const?QString?textconst?QHostAddress?&dest)
{
????sendmsg=true;
????msg=text;
????connectToHost(dest?5234);
????qDebug()?<“Send?message?“?<}
void?Client::sendMsg(const?QString?textconst?QString?&dest)
{
????sendmsg=true;
????msg=text;
????connectToHost(dest?5234);
????qDebug()?<“Send?message?“?<}
void?Client::on_connected()
{
????qDebug()?<“Connected“;
????if(sendfile)
????{
????????QFile?file(filePath);
????????if(!file.open(QIODevice::ReadOnly))
????????{
????????????emit?onerror(2);
????????????disconnectFromHost();
????????????return;
????????}
????????else
????????????emit?fileSize(file.size());
????????emit?message(tr(“?Start?send!“));
????????QString?fileName?=?file.fileName();
????????fileName?=?fileName.remove(0?fileName.lastIndexOf(‘/‘)+1);
????????qint64?size;
????????QByteArray?block;
????????QDataStream?out(&block?QIODevice::WriteOnly);
????????out.setVersion(QDataStream::Qt_5_0);
????????out?<????????size?=?block.size();
????????write((char*)&size?sizeof(qint64));
????????write(block.data()?size);
????????//flush();
????????if(!waitForBytesWritten(-1))
????????{
????????????qDebug()?<“writen?Bytes?error?“?<????????????throw?2;
????????}
????????do
????????{
????????????block.clear();
????????????out.device()->seek(0);
????????????out?<????????????size?=?block.size();
????????????qDebug()?<“Send?Data?“?<
????????????write((char*)&size?sizeof(qint64));
????????????write(block.data()?size);
????????????if(!waitForBytesWritten(-1))
????????????{
????????????????qDebug()?<“writen?Bytes?error?“?<????????????????throw?2;
????????????}
????????}while(!file.atEnd());
????????sendEOF();
????????qDebug()?<“FileSend?finished.“;
????????emit?message(tr(“?Send?finished!“));
????????forever/*(int?i=0;?i?10;?i++)*/
????????{
????????????if(!waitForDisconnected())
????????????{
????????????????//sendEOF();
????????????}
????????????else
????????????{
????????????????//qDebug
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????4489??2014-12-11?14:53??TCP\Client\Client.cpp
?????文件????????826??2014-12-11?11:41??TCP\Client\Client.h
?????文件????????428??2014-12-10?00:30??TCP\Client\Client.pro
?????文件??????77697??2014-12-11?15:47??TCP\Client\Client.pro.user
?????文件????????183??2014-12-09?21:49??TCP\Client\main.cpp
?????文件???????3403??2014-12-11?14:53??TCP\Client\mainwindow.cpp
?????文件????????723??2014-12-11?11:49??TCP\Client\mainwindow.h
?????文件???????3970??2014-12-11?15:07??TCP\Client\mainwindow.ui
?????文件????????206??2014-12-11?13:51??TCP\Server\main.cpp
?????文件????????883??2014-12-11?15:29??TCP\Server\mainwindow.cpp
?????文件????????574??2014-12-11?15:27??TCP\Server\mainwindow.h
?????文件???????2354??2014-12-11?15:26??TCP\Server\mainwindow.ui
?????文件???????1078??2014-12-11?15:26??TCP\Server\Server.cpp
?????文件????????484??2014-12-11?15:26??TCP\Server\Server.h
?????文件????????477??2014-12-11?12:07??TCP\Server\Server.pro
?????文件??????77697??2014-12-11?15:31??TCP\Server\Server.pro.user
?????文件???????3323??2014-12-11?15:26??TCP\Server\Thread.cpp
?????文件????????860??2014-12-11?15:26??TCP\Server\Thread.h
?????目錄??????????0??2014-12-11?16:12??TCP\Client
?????目錄??????????0??2014-12-11?16:12??TCP\Server
?????目錄??????????0??2014-12-11?16:12??TCP
-----------?---------??----------?-----??----
???????????????179655????????????????????21
- 上一篇:模糊c均值法分割圖像
- 下一篇:QT單擊按鈕關閉一個窗口,并且打開一個窗口
評論
共有 條評論