资源简介
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 “ << file;
}
void Client::sendFile(const QString &file const QString &dest)
{
filePath = file;
sendfile=true;
connectToHost(dest 5234);
qDebug() << “Send file “ << file;
}
void Client::sendMsg(const QString textconst QHostAddress &dest)
{
sendmsg=true;
msg=text;
connectToHost(dest 5234);
qDebug() << “Send message “ << text;
}
void Client::sendMsg(const QString textconst QString &dest)
{
sendmsg=true;
msg=text;
connectToHost(dest 5234);
qDebug() << “Send message “ << text;
}
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 << qint16(0x0001) << fileName.toUtf8();
size = block.size();
write((char*)&size sizeof(qint64));
write(block.data() size);
//flush();
if(!waitForBytesWritten(-1))
{
qDebug() << “writen Bytes error “ << errorString();
throw 2;
}
do
{
block.clear();
out.device()->seek(0);
out << qint16(0x0002) << file.read(0xFFF0);
size = block.size();
qDebug() << “Send Data “ << size << “ byte.“;
write((char*)&size sizeof(qint64));
write(block.data() size);
if(!waitForBytesWritten(-1))
{
qDebug() << “writen Bytes error “ << errorString();
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单击按钮关闭一个窗口,并且打开一个窗口
相关资源
- TCP通讯(接收.vi)
- 在LabVIEW 中利用TCPIP协议实现网络通信
- mgn-mqt82.exe
- Trojan-Qt5-Windows-0.0.4.rar
- TCP IP通信labview程序
- Modbus协议官方文档中、英文全
- 酒店管理系统基于Qt Creator5)
- Omron ETN21模块进行modbustcp通讯
- vtk QT做的三维地质可视化系统2of2
- Qt局域网聊天软件
- Qt Creator opengl实现四元数鼠标控制轨迹
- QT局域网聊天系统(基于QT5.修改过)
- qt-电子点菜系统
- Openssl给文件传输加密
- 组态王中S7-300TCP驱动
- C 餐厅叫号系统(QT平)
- 基于Apache Mina实现的TCP长连接和短连接
- QT 实现文件下载
- qt图像处理
- tftp文件传输工具
- CVI下的TCP服务器和客户端
- 代码客:G-TcpServer(IOCP) 1.0 正式版及
- QT,JPEG解码源代码(已完成)
- TCP 发包工具(windows)
- Qt 播放音频文件
- VxWorks TCPIP协议栈
- socket小工具(可方便建立TCP UDP的soc
- vc 编写的基于TCP协议的客户/服务器
- 基于CSocket的多人聊天室
- 采用WINSOCK2 编写的TCP/UDP通信程序
评论
共有 条评论