QWebsocket 的特性之一就是建立连接之后,就稳定不断,可以随时的进行数据的上传和下载,Qt封装的QWebSocket我目前利用来进行小量数据的发送和接收感觉还是挺快的。没有遇到什么问题。
本来想利用QDataStream来对二进制文件进行读写的,没想到直接传QbyteArray就行了,利用QFile就可以把文件的读写利用 QbyteArray搞定了。write(QbyteArray) readAll(QbyteArray)
QDataStream还存在一个隐藏的bug,如果不小心难免浪费个半个小时去寻找,。
使用如下QDataStream方式输出二进制文件,文件头部多四个字节,代表文件大小,不管怎么都去不了;
//写二进制文件
QFile file("F:/1.rgb");
if(!file.open(QIODevice::WriteOnly))
{
QMessageBox::critical(this,"Warning", "Can't open file for writing",QMessageBox::Ok);
return;
}
QDataStream out(&file);
out.setVersion(QDataStream::Qt_4_0);
out << data;
file.close();
最终舍弃,使用write方式写入二进制文件,输出正常
//写二进制文件
QFile file("F:/1.rgb");
if(!file.open(QIODevice::WriteOnly))
{
QMessageBox::critical(this,"Warning", "Can't open file for writing",QMessageBox::Ok);
return;
}
file.write(data);
file.close();
接下来上QWebSocket的 Client:
#include "dialog.h" | |
#include<QUrl> | |
#include<QFile> | |
#include<QDataStream> | |
Dialog::Dialog(QWidget *parent) | |
: QDialog(parent) | |
{ | |
m_client=new QWebSocket();//"test", QWebSocketProtocol::NonSecureMode,this | |
m_label=new QLabel(QStringLiteral("ready to push"),this); | |
m_button=new QPushButton(QStringLiteral("Push"),this); | |
m_filePathEdit=new QTextEdit(QStringLiteral("input file path"),this); | |
QHBoxLayout *hlay=new QHBoxLayout(); | |
QVBoxLayout *vlay=new QVBoxLayout(); | |
QHBoxLayout *hlay2=new QHBoxLayout(); | |
hlay->addWidget(m_button); | |
hlay->addWidget(m_label,5); | |
hlay2->addWidget(m_filePathEdit); | |
vlay->addLayout(hlay,1); | |
vlay->addLayout(hlay2); | |
this->setLayout(vlay); | |
this->setMinimumSize(500,800); | |
connect(m_client,SIGNAL(connected()),this,SLOT(onConnected())); | |
connect(m_button,SIGNAL(clicked()),this,SLOT(onPush())); | |
} | |
Dialog::~Dialog() | |
{ | |
} | |
void Dialog::connectToServer() | |
{ | |
QString ipAndPort="ws://10.10.2.30:12345"; | |
QUrl url(ipAndPort); | |
m_client->open(url); | |
} | |
void Dialog::onConnected() | |
{ | |
qDebug()<<"onConnected"<<endl; | |
} | |
void Dialog::onPush() | |
{ | |
//QByteArray txt("110"); | |
// m_filePath=m_filePathEdit->toPlainText(); | |
m_filePath=QStringLiteral("C:/Foot2Last/Footdata/脚模_L_M.stl"); | |
QFile file(m_filePath); | |
if(!file.open(QIODevice::ReadOnly)) | |
{ | |
qDebug()<<"read file failed"<<endl; | |
} | |
//QDataStream data(&file); | |
QByteArray dataByteArray=file.readAll(); | |
//data>>dataByteArray; | |
qDebug()<<dataByteArray.length()<<endl; | |
m_client->sendBinaryMessage(dataByteArray); | |
file.close(); | |
} |
然后是Server:
#include "mainwindow.h" | |
#include<QDataStream> | |
#include<QFile> | |
Mainwindow::Mainwindow(QWidget *parent) | |
: QDialog(parent) | |
{ | |
// m_button=new QPushButton(QStringLiteral("received"),this) | |
m_label=new QLabel(QStringLiteral("init"),this); | |
QHBoxLayout *hlay=new QHBoxLayout(this); | |
hlay->addWidget(m_label); | |
this->setLayout(hlay); | |
this->setMinimumSize(500,800); | |
ws=new QWebSocketServer("test", QWebSocketServer::NonSecureMode); | |
if(!ws->listen(QHostAddress::Any, 12345)){ | |
qCritical()<<"Cannot listen to port 12345!"; | |
// return -1; | |
} | |
qDebug()<<"xxxxxxxxxxxxxxxxxxxxxxxxxxx"<<endl; | |
connect(ws,SIGNAL(newConnection()),this,SLOT(onNewConnection())); | |
} | |
Mainwindow::~Mainwindow() | |
{ | |
} | |
void Mainwindow::onNewConnection() | |
{ | |
m_server=ws->nextPendingConnection(); | |
connect(m_server,&QWebSocket::binaryMessageReceived,this,&Mainwindow::onBinaryDataReceived); | |
qDebug()<<"new connection connected"<<endl; | |
} | |
void Mainwindow::onBinaryDataReceived( QByteArray message) | |
{ | |
qDebug()<<"received"<<message.length()<<endl; | |
QFile file("C:/Users/shining3d/Desktop/stl/test.stl"); | |
if(!file.open(QIODevice::ReadWrite|QIODevice::Truncate)) | |
{ | |
qDebug()<<"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"<<endl; | |
} | |
//QDataStream dataStream(&file); | |
//dataStream.setVersion(QDataStream::Qt_5_12); | |
//dataStream<<message; | |
file.write(message); | |
file.close(); | |
} |
上面的代码基本清楚了,如果想要具体的例子,请访问我的github.哈哈哈.
https://github.com/szgturnin