file.open(QFile::ReadOnly); QDataStream read(&file); lBytes = 0; char * ch; ch = (char*)malloc(sizeof(char) * 1024); ch[1023] = '\0'; while(!read.atEnd()){ int l = read.readRawData(ch, sizeof(char)*1023); QByteArray ba(ch, sizeof(char)*l); lBytes += m_pTcpSocket->write(ba, sizeof(char)*l); m_pTcpSocket->flush(); if (-1 == lBytes){ qWarning() << "Error"; m_pTcpSocket->close(); //Закрываем устройство сокета return; } float procentage = ((float)lBytes / package.filelength) * 100; emit setProcentage((int)procentage); }//while(!readEnd()) free((void*)ch);
QFile save(package.filename); save.open(QFile::WriteOnly); QDataStream write(&save); long int lBytesDone = 0; //Пока ничего не приняли long int lSize = package.filelength; long int lBytes; while (lBytesDone < lSize){//Пока приняли меньше, чем указано //Ожидаем данные lBytes = 0; while (lBytes == 0) lBytes = m_pTcpSocket->waitForReadyRead(-1); if (-1 == lBytes){//Если произошла ошибка сокета, то qWarning("-----TFileServer: Aborting download."); m_pTcpSocket->close(); return; }//if (-1 == lBytes) QByteArray tmp = m_pTcpSocket->readAll(); lBytes += write.writeRawData(tmp.data(), tmp.size()); lBytesDone += tmp.size(); float procentage = ((float)lBytes / package.filelength) * 100; emit setProcentage((int)procentage); }//while (lBytesDone < lSize) save.close();