Qt PC 安卓 tcp传输文件
废话不多说,如题,上代码
qt PC端 头文件 //网络部分 #include <QTcpSocket> #include <QFile> #include <QFileDialog> #include <QHeaderView> #include <QScrollBar> 具体实现 void MainForm::openFile() //打开文件 { filename = QFileDialog::getOpenFileName(this); if(!filename.isEmpty()) { qDebug()<<"open file ok"<<filename; } } void MainForm::send() { tcpClient->connectToHost(ui->lineEdit->text(),8888); } void MainForm::startTransfer() { localFile = new QFile(filename); if(!localFile->open(QFile::ReadOnly)) { QMessageBox::information(this,"警告","文件打开失败",QMessageBox::Ok); return; } totalBytes = localFile->size(); QDataStream sendout(&outlock,QIODevice::WriteOnly); sendout.setVersion(QDataStream::Qt_5_3); QString currentFileName = filename.right(filename.size()-filename.lastIndexOf("/")-1); sendout << qint64(0) << qint64(0) <<currentFileName; totalBytes += outlock.size(); sendout.device()->seek(0); sendout<<totalBytes<<qint64((outlock.size() - sizeof(qint64)*2)); bytestowrite = totalBytes - tcpClient->write(outlock); qDebug("已连接"); outlock.resize(0); } void MainForm::updaraclientprogress(qint64 numbytes) { bytesWrittens += (int)numbytes; if(bytestowrite > 0) { outlock = localFile->read(qMin(bytestowrite,locaksize)); bytestowrite -= (int)tcpClient->write(outlock); outlock.resize(0); } else { localFile->close(); } qDebug()<<bytestowrite<<"-------------------->>"; if(bytesWrittens == totalBytes) { qDebug("发送完成"); localFile->close(); tcpClient->close(); QMessageBox::information(this,"提示","同步完成",QMessageBox::Ok); } }
接收端 void Widget::start() { reciverBytes = 0; tcpserver.listen(QHostAddress::Any,8888); //监听任意IP的8888端口 connect(&tcpserver,SIGNAL(newConnection()),this,SLOT(acceptConnection())); //当有新的连接连入客户端时,启动文件接受 } //建立连接 void Widget::acceptConnection() { tcpserverconnection = tcpserver.nextPendingConnection(); //建立连接 connect(tcpserverconnection,SIGNAL(readyRead()),this,SLOT(updataServerProgress())); //发送连接信号 connect(tcpserverconnection,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(disPlayError(QAbstractSocket::SocketError))); //将新的连接与信息处理连接,当连接出现问题是,输出相应的错误信息 QString IPAddressString; //用于存放对方的Ip地址, QList<QHostAddress> ipAddresslist = QNetworkInterface::allAddresses(); //获取所有的IP地址 for(int i=0;i<ipAddresslist.size();i++) //获取连入主机的IP { if(ipAddresslist.at(i) != QHostAddress::LocalHost && ipAddresslist.at(i).toIPv4Address()) //获得IPv4 { IPAddressString = ipAddresslist.at(i).toString(); //获得IPv4地址 break; } if(ipAddresslist.isEmpty()) //如果ip地址列表为空,则表示现在没有任何用户连接到主机,显示主机自己的IP,其实,这个也是不会执行的,有连接不然会有IP,不会存在连接存在,而IP没有的时候 { IPAddressString = QHostAddress(QHostAddress::LocalHost).toString(); //获取本机IP地址 } } } void Widget::updataServerProgress() //接收文件 { QDataStream in(tcpserverconnection); in.setVersion(QDataStream::Qt_5_3); //使用QDataStream::Qt_5_3 协议解析数据 if(reciverBytes <= sizeof(qint64)*2) //如果文件数据小雨16字节,那么是刚开始传输数据,我们保存的我//未来的头文件信息 { if((tcpserverconnection->bytesAvailable()>=sizeof(qint64)*2)&&(fileNameSize ==0)) { //接收数据总大小信息和文件名大小信息 in >> totalBytes >>fileNameSize; reciverBytes += sizeof(qint64)*2; } if((tcpserverconnection->bytesAvailable()>=fileNameSize)&&(fileNameSize!=0)) { //接受文件名,并建立文件 in >> fileName; reciverBytes += fileNameSize; localFile = new QFile(fileName); if(!localFile->open(QFile::WriteOnly)) { return; } } else return; } if(reciverBytes < totalBytes) { //如果接收数据小于总数据,那么将数据信息写入文件中。 reciverBytes += tcpserverconnection->bytesAvailable(); inblokc = tcpserverconnection->readAll(); localFile->write(inblokc); inblokc.resize(0); } if(reciverBytes == totalBytes) { //接受完成时,关闭文件,变量初始化 tcpserverconnection->close(); localFile->close(); totalBytes =0; reciverBytes =0; fileNameSize = 0; // opendatabase(); //如果要使用数据库作为文件的解析,可直接调用数据库文件解析函数即可 } }