QTcpSocket 使用QDataStream 傳送和儲存圖片檔案
阿新 • • 發佈:2019-02-11
圖片檔案
現在遇到的問題是writeRawData()讀取大檔案時程式崩潰,思路是分多次進行讀取和傳送,待寫
={}不明白可以去看我前面的文章,connect的過載
傳送端
qDebug()<<"start to find pix"<<endl;
QImage pic("D:/svn//timg.jpg");
if(pic.isNull()){
qDebug()<<"can not find pix"<<endl;
return ;
}
else{
qDebug()<<"succeed tot find pix" <<endl;
}
QByteArray block; //用於暫存我們要傳送的資料
QDataStream out(&block,QIODevice::ReadWrite);
out.setVersion(QDataStream::Qt_4_6);
qDebug()<<"pic.size:"<<pic.size()<<endl;
// out<<tr("hello Tcp!!!");
out<<pic;
out.device()->seek(0);
QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
clientConnection->write(block);
接收端
/*全域性變數
*QImage image;
*QByteArray res_block;
*/
connect(tcpSocket, &QTcpSocket::readyRead,
[=](){
//從通訊套接字中取出內容
QByteArray array = tcpSocket->readAll();
qDebug()<<"size:"<<array. size()<<endl;
res_block=res_block+array;
qDebug()<<"res_block size:"<<res_block.size()<<endl;
QDataStream image_stream(&res_block,QIODevice::ReadWrite);
image_stream>>image;
});
任意檔案(無法讀取大檔案)
傳送端
QFile file("D:/svn/UVSScure/Client/build/jpeg_2_128.jpg");
int flag=file.open(QIODevice::ReadOnly);
QDataStream in(&file);
QFileInfo fileInfo("D:/svn/UVSScure/Client/build/jpeg_2_128.jpg");
QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
char datRawArray[fileInfo.size()];
in.readRawData(datRawArray, fileInfo.size());
QByteArray block(datRawArray,fileInfo.size());
quint32 write_len = clientConnection->write(block);
接收端
/*全域性變數
*QDataStream file_stream;
*QByteArray res_block;
*QFile file;
*/
file.setFileName("D:/svn/UVSScure/Client/build/image_res.jpg");
file.open(QIODevice::ReadWrite);
file_stream.setDevice(&file);
connect(tcpSocket, &QTcpSocket::readyRead,
[=](){
//從通訊套接字中取出內容
QByteArray array = tcpSocket->readAll();
qDebug()<<"size:"<<array.size()<<endl;
res_block=res_block+array;
qDebug()<<"res_block size:"<<res_block.size()<<endl;
QDataStream image_stream(&res_block,QIODevice::ReadWrite);
file_stream.writeRawData(res_block,res_block.size();//這個函式放在ondisconnect中
});
}
TCP收發大檔案
1. TCP分多次傳送讀取和接收大檔案
QFile file("D:/svn/UVSScure/Client/build/jpeg_2_128.jpg");
int flag=file.open(QIODevice::ReadOnly);
QDataStream in(&file);
QFileInfo fileInfo("D:/svn/UVSScure/Client/build/jpeg_2_128.jpg");
QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
connect(clientConnection,SIGNAL(disconnected()),clientConnection,
SLOT(deleteLater()));
int file_size;
file_size=fileInfo.size();
connect(clientConnection,&QTcpSocket::bytesWritten,[=](qint64 num){
static int total_size=0;
total_size+=num;
qDebug()<<"num:"<<num<<endl;
qDebug()<<"total_size:"<<total_size<<endl;
qDebug()<<"file_size:"<<file_size<<endl;
if(total_size>file_size){
clientConnection->disconnectFromHost();
qDebug()<<"wrirte finished!"<<endl;
total_size=0;
}
});
int buffer_size=65535;
char datRawArray[buffer_size];
qDebug()<<"nomal"<<fileInfo.size()<<endl;
while(!in.atEnd()){
in.readRawData(datRawArray, buffer_size);
QByteArray block(datRawArray,buffer_size);
qDebug()<<"block_size:"<<block.size()<<endl;
quint32 write_len = clientConnection->write(block);
qDebug("write len:%d",write_len);
}
2. TCP接收檔案
connect(tcpSocket,SIGNAL(disconnected()),this,SLOT(ondisconnect()));
file.setFileName("D:/svn/UVSScure/Client/build/image_res.jpg");
file.open(QIODevice::ReadWrite);
file_stream.setDevice(&file);
connect(tcpSocket, &QTcpSocket::readyRead,
[=](){
//從通訊套接字中取出內容
QByteArray array = tcpSocket->readAll();
qDebug()<<"size:"<<array.size()<<endl;
res_block=res_block+array;
qDebug()<<"res_block size:"<<res_block.size()<<endl;
});
void ondisconnect(){
if(file_stream.writeRawData(res_block,res_block.size())){
qDebug()<<"write csv file successful"<<endl;
}
else{
qDebug()<<"fail to save csv file"<<endl;
}
file.close();
qDebug()<<"file closed !"<<endl;
}
TCP一次性發送發大檔案
QFile file("D:/svn/UVSScure/Client/build/jpeg_2_128.jpg");
int flag=file.open(QIODevice::ReadOnly);
QDataStream in(&file);
QFileInfo fileInfo("D:/svn/UVSScure/Client/build/jpeg_2_128.jpg");
QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
QByteArray total_file;
connect(clientConnection,SIGNAL(disconnected()),clientConnection,
SLOT(deleteLater()));
int file_size;
file_size=fileInfo.size();
connect(clientConnection,&QTcpSocket::bytesWritten,[=](qint64 num){
clientConnection->disconnectFromHost();
});
int buffer_size=65535;
char datRawArray[buffer_size];
qDebug()<<"nomal"<<fileInfo.size()<<endl;
while(!in.atEnd()){
in.readRawData(datRawArray, buffer_size);
QByteArray block(datRawArray,buffer_size);
qDebug()<<"block_size:"<<block.size()<<endl;
total_file+=block
}
quint32 write_len = clientConnection->write(total_file);
qDebug("write len:%d",write_len);