无法从QtcpSocket读取

时间:2016-01-11 16:07:51

标签: qt

我需要一些认真的帮助。我没有找出问题的原因。 我有一个客户端服务器应用程序正在尝试开发。在这个应用程序中,我从客户端请求一个文件,服务器发送该文件。服务器端似乎发生但我在客户端遇到问题。对于大文件,客户端tcpsocket无法读取服务器发送的最后一个字节。服务器显示发送的字节,但客户端显示它仍在等待这些字节。这是代码

服务器代码

/ 将文件复制或传输到目标 /

 void TcpConnecton::copyFileFunction(){

  QTcpSocket* socket;

  switch(copyFile.operation){

    case 1:
        socket = getSocket();
        break;

    case 2:
        socket = tcpSocket;
        break;
}

FileObject fileObject;
QFileInfo fileInfo(copyFile.filePath);
QString file_Name(QString(tr("%1/%2")).arg(copyFile.remoteDestination).arg(fileInfo.fileName()));

fileObject.setFilename(file_Name);

QFile file(copyFile.filePath);

if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;

if(fileInfo.size() <= 2*1024){
    {
        QByteArray block;
        QDataStream out(&block, QIODevice::WriteOnly);
        out.setVersion(QDataStream::Qt_5_5);

        QByteArray filebytesRead = file.readAll();

        fileObject.setFileData(filebytesRead);

        out << quint32(0) << quint32(3) << fileObject;
        out.device()->seek(0);
        out << quint32(block.size() - sizeof(quint32));

        socket->write(block);
        socket->waitForBytesWritten();

        qDebug() << "file bytes read" << filebytesRead.size();
        qDebug() << "file object data" << fileObject.getFileData().size();
        qDebug() << "file size" << fileInfo.size();
        qDebug() << "block size" << block.size() - sizeof(quint32);

    }
}else{

    while(true){

        QByteArray filebytesRead = file.read(2*1024);

        if(filebytesRead.size() == 0) break;

        fileObject.setFileData(filebytesRead);

        {
            QByteArray block;
            QDataStream out(&block, QIODevice::WriteOnly);
            out.setVersion(QDataStream::Qt_5_5);

            out << quint32(0) << quint32(3) << fileObject;
            out.device()->seek(0);
            out << quint32(block.size() - sizeof(quint32));

            socket->write(block);
            socket->waitForBytesWritten();

            qDebug() << "file bytes read" << filebytesRead.size();
            qDebug() << "file object data" << fileObject.getFileData().size();
            qDebug() << "file size" << fileInfo.size();
            qDebug() << "block size" << block.size() - sizeof(quint32);
        }
    }

}

fileTransfered.transfer_Operation = (copyFile.operation == 1)? 1 : 2;
fileTransfered.fileName = copyFile.filePath;

QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_5_5);

out << quint32(0) << quint32(7) << fileTransfered;
out.device()->seek(0);
out << quint32(block.size() - sizeof(quint32));

tcpSocket->write(block);
//tcpSocket->flush();

switch(copyFile.operation){
    case 1:
        socket->deleteLater();
        break;
}

qDebug() << "filetransfer object transfered" ;

}

客户代码

void FileTransferSystem::readResponseData(){
    QDataStream in(&tcpSocket);
    in.setVersion(QDataStream::Qt_5_5);

    if(nextblockSize == 0) {
       if (tcpSocket.bytesAvailable() < sizeof(quint32)) return;
       in >> nextblockSize;
     }

    if(tcpSocket.bytesAvailable() < nextblockSize) return;

     responseByteArrayData = tcpSocket.read(nextblockSize);


    qCritical("Data received..... " );
    qDebug() << " block size received: "<< (int)nextblockSize;
    qDebug() << " responseByteArrayData received: " <<     responseByteArrayData.size();

   dispatchResponse();

   nextblockSize = 0;

 }

void FileTransferSystem::dispatchResponse(){

   quint32 cmd;
   QDataStream in(&responseByteArrayData, QIODevice::ReadOnly);
   in.setVersion(QDataStream::Qt_5_5);
   in >> cmd;

   qDebug() << " command received: " << (int)cmd;
   QString path;
   switch((int)cmd){        

    case 5:
        in >> copyFileObject;
        handleGetCopyFile_Object();
        break;
    case 7:
        in >> fileTransferedObject;
        handleGetTransferedFile_Object();
        break;        
    case 9:
        in >> path;
        newFolderCreated(path);
        break;
    default:
        qCritical("unknown command..... " );
        return;
   }
}

void FileTransferSystem::handleGetFileObject_Object(){

   QFile file(fileObject.getFilename());

   if(file.exists()){

   file.open(QIODevice::Append);
   file.write(fileObject.getFileData());
   file.close();

}else{
    file.open(QIODevice::WriteOnly);//will create the file if it does not yet exist
    file.write(fileObject.getFileData());
    file.close();
 }

}

对于小文件(2kb),客户端接收所有字节,但是如果文件较大,则客户端无法读取服务器发送的最后一个字节。我哪里错了。请帮忙

1 个答案:

答案 0 :(得分:0)

正如评论中所提到的,许多与I / O相关的调用(如read()write())都会返回状态变量,您必须检查这些变量以确保正确执行操作。

作为一个例子,我将引用QIODevice::write()的文档:

  

qint64 QIODevice :: write(const char * data,qint64 maxSize)

     

将数据的最多 maxSize字节数据写入设备。返回   实际写入的字节数,,如果是错误则为-1   发生

提示:Qt附带的许多示例都包含代码片段,显示如何正确使用读/写,计算返回代码。