void sendwidget::startTransfer()
{
    localFile = new QFile(fileName);
    if(!localFile->open(QFile::ReadOnly))
    {
        qDebug()<<"open file error";
        ui->LB_showMsg->setText(tr("open file error"));
        return;
    }
    totalBytes = localFile->size();
    QDataStream sendOut(&outBlock, QIODevice::WriteOnly);
    sendOut.setVersion(QDataStream::Qt_5_7);
    QString currentFileName = fileName.right(fileName.size()-fileName.lastIndexOf('/') - 1);
    sendOut << qint64(0) << qint64(0) << currentFileName;
    totalBytes += outBlock.size();
    sendOut.device()->seek(0);
    sendOut << totalBytes << qint64((outBlock.size() - sizeof(qint64)*2));

    bytesToWrite = totalBytes - tcpClient->write(outBlock);
    ui->LB_status->setText(tr("connected"));
    outBlock.resize(0);
    qDebug() << "start transfer file";


}
void ScreenCap::MergeSendMessage()
{

    /************************************************************
    *                数据流传送格式
    *
    *  宽度 ---|   8    |   8       |   8   |   n   |.....
    *  含义 ---|  总长度 |  头长度(8) | 头内容 | 数据内容|.....
    *  总长度 = |<----------------------------->|.....
    *  总长度 = 8 + 8 + 8 + n
    *  头内容 : 0xFFFEFFFE;//值
    *  头长度 : 8
    ************************************************************/
    TotalBytes = pCapThread->arrayNetSize.at(0); //数据大小
    //    qDebug() << "net size:" << TotalBytes;

    QDataStream sendOut(&outBlock, QIODevice::WriteOnly);
    sendOut.setVersion(QDataStream::Qt_4_3);

    qint64 headerFlag = 0xFFFEFFFE;//值
    sendOut << qint64(0) << qint64(0)  << headerFlag;
    //    qDebug() << "outBlock size:" << outBlock.size();

    TotalBytes +=  outBlock.size();//总大小 = 数据 + 8字节(存在总大小字节数) + 8"头表达式"
    //    qDebug() << "TotalBytes size:" << TotalBytes;
    sendOut.device()->seek(0);
    //    qDebug() << "header size:" << qint64((outBlock.size() - sizeof(qint64) * 2));
    //填写实际的总长度和头长度
    sendOut << TotalBytes << qint64((outBlock.size() - sizeof(qint64) * 2));

    QString HeaderInfo = QString::fromLocal8Bit("\n-->>N:%1,T:%2%3\n").arg(picNametime).arg(QDate::currentDate().toString("yyyyMMdd")).arg(QTime::currentTime().toString("HHmmss"));
    LogWriteFile(HeaderInfo);
    QString TotalBytesInfo = QString::fromLocal8Bit("TotalBytes:%1\n").arg(TotalBytes);
    LogWriteFile(TotalBytesInfo);

    //将头发送出去,并计算剩余的数据长度,即数据内容长度(n)
    qint64 len = writeNetData(outBlock, WRITE_HEAD);


    if(len != outBlock.size())
    {
        qDebug() << "Header not same!!";
        qDebug() << "to write Header len:" << outBlock.size();
        qDebug() << "written Header len:" << len;
    }
    bytesToWrite = TotalBytes - len;
    outBlock.resize(0);
    //    qDebug() << "-->TotalBytes size:" << TotalBytes;
    //    qDebug() << "-->bytesToWrite size:" << bytesToWrite;

    outBlkData = pCapThread->arrayNetData.at(0);

    writeNetData(outBlkData, WRITE_BODY);

    qDebug() << "-->outBlkData size :" << outBlkData.size();

    picNametime++;//发送次数
}
void DouyuTcpSocket::messageWrite(QString &content)
{
    const char *content_ptr = content.toStdString().c_str();
    QDataStream sendOut(&outBlock,QIODevice::WriteOnly);
    qint32 length = 4 + 4 + content.length() + 1;// 2个uint32字段长度+内容长度+'\0'
    sendOut<<qint32(hexReverse_qint32(length))<<qint32(hexReverse_qint32(length))<<qint32(_Douyu_CTS_Num);
    outBlock.append(content_ptr);
    outBlock.append('\0');
    tcpDanmuSoc.write(outBlock);
    outBlock.resize(0);
    delete content_ptr;
}
void AaServerWindowApp::processData()
{
    QByteArray array;
    QString msg = ui->m_txtInput->toPlainText();
    QDataStream sendOut(&array,QIODevice::WriteOnly);
    sendOut.setVersion(QDataStream::Qt_4_6);

    sendOut<<qint64(0) << qint64(0) << AaServerWindowApp::Message << msg;
    sendOut.device()->seek(0);
    sendOut<<array.size() <<qint64(array.size() - sizeof(qint64)*2);

    m_pServerSocket->write(array);
    ui->m_txtInput->clear();
    appendServerText(msg);
}
Beispiel #5
0
//发送数据由sndMsg()槽函数完成
void Server::sndMsg()
{
    ui->sSendBtn->setEnabled(false);
    clntConn = tSrv->nextPendingConnection();
    connect(clntConn, SIGNAL(bytesWritten(qint64)), this, SLOT(updClntProgress(qint64)));
    ui->sStatusLbl->setText(tr("开始传送文件%1!").arg(theFileName));
    locFile = new QFile(fileName);
    //首先以只读方式打开选中的文件
    if(!locFile->open((QFile::ReadOnly)))
    {
        QMessageBox::warning(this, tr("应用程序"), tr("无法读取文件%1:\n%2").arg(fileName)
                             .arg(locFile->errorString()));
        return ;
    }

    //通过QFile类的size()函数获取待发送文件的大小,并将该值暂存于totalBytes变量中
    totalBytes = locFile->size();

    //将发送缓存区outBlock封装在一个QDataStream变量中,这样做客方便地通过重载“<”
    //操作符填写文件头结构
    QDataStream sendOut(&outBlock, QIODevice::WriteOnly);
    sendOut.setVersion(QDataStream::Qt_4_7);

    //启动计时,time是QTime对象,用来统计传输所用的时间
    time.start();

    //这里通过QString类的right()函数去掉文件的路径部分,仅将文件部分保存在curFile变量中
    QString curFile = fileName.right(fileName.size()-fileName.lastIndexOf('/') - 1);

    //构造一个临时的文件头,将该值追加到totalBytes字段,从而完成实际需发送字节数的记录
    sendOut << qint64(0) << qint64(0) << curFile;
    totalBytes += outBlock.size();

    //将读写操作指向从头开始
    sendOut.device()->seek(0);

    //填写实际的总长度和文件长度
    sendOut << totalBytes << qint64((outBlock.size()-sizeof(qint64)*2));

    //将该文件头发出,同时修改待发送字节数bytesTobeWrite
    bytesTobeWrite = totalBytes - clntConn->write(outBlock);

    //清空发送缓存区以备下次使用
    outBlock.resize(0);
}
void postoperation::startTransfer()  //实现文件大小等信息的发送
{
    QDir dir;
    QString path = dir.currentPath();
    path += "/database";
    QDir dir2(path);
    QString dbname;
    if(dir2.exists())
    {
        dir2.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
        dir2.setSorting(QDir::Size | QDir::Reversed);
        QFileInfoList list = dir2.entryInfoList();
        QFileInfo fileInfo = list.at(0);
        dbname = fileInfo.fileName();
        qDebug()<<dbname;
    }
    fileName = path + "/" +dbname;

    qDebug()<<fileName;

    localFile = new QFile(fileName);
    if(!localFile->open(QFile::ReadOnly))
    {
        qDebug() << "open file error!";
        return;
    }
    totalBytes = localFile->size();
    //文件总大小
    QDataStream sendOut(&outBlock,QIODevice::WriteOnly);
    sendOut.setVersion(QDataStream::Qt_4_7);
    QString currentFileName = fileName.right(fileName.size() - fileName.lastIndexOf('/')-1);
    sendOut << qint64(0) << qint64(0) << currentFileName;
    //依次写入总大小信息空间,文件名大小信息空间,文件名---仅是文件名不含路径,没必要含路径
    totalBytes += outBlock.size();
    //这里的总大小是文件名大小等信息和实际文件大小的总和
    sendOut.device()->seek(0);
    sendOut<<totalBytes<<qint64((outBlock.size() - sizeof(qint64)*2));
    //totalBytes是文件总大小,即两个quint64的大小+文件名+文件实际内容的大小
    //qint64((outBlock.size() - sizeof(qint64)*2))得到的是文件名大小
    bytesToWrite = totalBytes - tcpClient->write(outBlock);
    //发送完头数据后剩余数据的大小,即文件实际内容的大小
    qDebug()<<"已连接"<<endl;
    outBlock.resize(0);
    qDebug()<<"#####"<<totalBytes;
}
Beispiel #7
0
void Widget::startTransferMsg(){
    if(msgSendReady){
//    localFile = new QFile(fileName);
//    if(!localFile->open(QFile::ReadOnly))
//    {
//        qDebug() << "open file error!";
//        return;
//    }
    msg = this->msgLineEdit->text();
    totalBytes = msg.size();
    //文件总大小
    QDataStream sendOut(&outBlock,QIODevice::WriteOnly);
    sendOut.setVersion(QDataStream::Qt_4_6);
    //QString currentFileName = fileName.right(fileName.size() - fileName.lastIndexOf('/')-1);
    //fileName.lastIndexOf('/')返回的是从首字符开始 到最后一个 / 的位数
    //fileName.right(8) 意思就是 从路径右面开始 到第八位数的字符串
    sendOut << qint64(0)<<qint8(0)<<qint64(0)<<msg;
    //依次写入总大小信息空间,发送类型信息空间
    totalBytes += outBlock.size();
    //这里的总大小是消息大小消息+类型信息的总和
    sendOut.device()->seek(0);
    //回到字节流起始位置
    msgtype = 2;
    sendOut<<totalBytes<<msgtype<<qint64((outBlock.size() - sizeof(qint64)*2-sizeof(qint8)));
    //totalBytes是文件总大小,即两个quint64的大小+文件名+文件实际内容的大小
    //qint64((outBlock.size() - sizeof(qint64)*2))得到的是文件名大小
    //为了保证在客户端能接收到完整的文件,我们都在数据流的最开始写入完整文件的大小信息,
    //这样客户端就可以根据大小信息来判断是否接受到了完整的文件。而在服务器端,我们在发送数据时就要首先发送实际文件的大小信息,
    //但是,文件的大小一开始是无法预知的,所以我们先使用了out<<(quint16) 0;在block的开始添加了一个quint16大小的空间,
    //也就是两字节的空间,它用于后面放置文件的大小信息。然后out<<tr(“hello Tcp!!!”);输入实际的文件,这里是字符串。
    //当文件输入完成后我们在使用out.device()->seek(0);返回到block的开始,加入实际的文件大小信息,也就是后面的代码,
    //它是实际文件的大小:out<<(quint16) (block.size() – sizeof(quint16));
    tcpClient->write(outBlock);
    //发送完头数据后剩余数据的大小,即文件实际内容的大小
//    this->stateLabel->setText(QString::fromLocal8Bit("已连接"));
    outBlock.resize(0);
    qDebug()<<"#####totalBytes"<<totalBytes;
    this->stateLabel->setText(this->msgLineEdit->text()+" is Send");
    tcpClient->close();
    }
}
void AaServerWindowApp::slotSendFile()
{
    m_fileNameSend = QFileDialog::getOpenFileName(this);
    if(!m_fileNameSend.isEmpty()){
        localFile = new QFile(m_fileNameSend);
        if (!localFile->open(QFile::ReadOnly)){
            qDebug()<<__PRETTY_FUNCTION__<<"Error";
            return;
        }
        totalBytesSend = localFile->size();
        QDataStream sendOut(&outBlock,QIODevice::WriteOnly);
        sendOut.setVersion(QDataStream::Qt_4_6);
        QString currentFileName = m_fileNameSend.right(m_fileNameSend.size()
                                                       -m_fileNameSend.lastIndexOf("/") -1);
        sendOut << qint64(0) <<qint64(0) <<AaServerWindowApp::File << currentFileName;
        totalBytesSend += outBlock.size();
        sendOut.device()->seek(0);
        sendOut << totalBytesSend << qint64(outBlock.size() - sizeof(qint64)*2);
        bytesToWriteSend = totalBytesSend - m_pServerSocket->write(outBlock);
        outBlock.resize(0);
    }
}