脚本宝典收集整理的这篇文章主要介绍了Qt开源作品41-网络调试助手增强版V2022,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
做网络通信少不了网络收发数据,经常用到网络数据的调试相关工具,以便侦听数据用来判断数据是否正确,许久以前就发布过类似的工具,第一版大概在2013年,第二版大概在2017年,中间参考过不少的网络调试助手,也有些叫网络调试工具等等,个人觉得做得最好的还是野人家园的NetAssist,小巧绿色,功能强大。
中间不少网友提过很多建议,比如为何没有Udp客户端只有Udp服务器,其实Udp通信是无连接的,意味着QUdpSocket即是客户端也是服务器,但是根据众多用户的操作习惯以及编程对称性法则,还是单独又做了个Udp客户端。如今WebSocket也非常流行,客户端工具和网页之间通信可以直接用上socket之类的机制,而且自从Qt5以后有了WebSocket模块,使用非常简单,封装的QWebSocket、QWebSocketServer(很奇怪这里没有叫QWebServerF1f;)和QTcpSocket、QTcpServer、QUdpSocket用法几乎一致。
//第一步:实例化对应的类
tcpSocket = new QTcpSocket(this);
connect(tcpSocket, SIGNAL(connected()), this, SLOT(connected()));
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(disconnected()));
connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readData()));
tcpServer = new TcpServer(this);
connect(tcpServer, SIGNAL(clientConnected(QString, int)), this, SLOT(clientConnected(QString, int)));
connect(tcpServer, SIGNAL(clientDisconnected(QString, int)), this, SLOT(clientDisconnected(QString, int)));
connect(tcpServer, SIGNAL(sendData(QString, int, QString)), this, SLOT(sendData(QString, int, QString)));
connect(tcpServer, SIGNAL(receiveData(QString, int, QString)), this, SLOT(receiveData(QString, int, QString)));
udpSocket = new QUdpSocket(this);
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readData()));
//第二步:收发数据
void frmTcpClient::readData()
{
QByteArray data = tcpSocket->readAll();
if (data.length() <= 0) {
return;
}
QString buffer;
if (App::HexReceiveTcpClient) {
buffer = QUIHelper::byteArrayToHexStr(data);
} else if (App::AsciiTcpClient) {
buffer = QUIHelper::byteArrayToAsciiStr(data);
} else {
buffer = QString(data);
}
append(1, buffer);
//自动回复数据,可以回复的数据是以;隔开,每行可以带多个;所以这里不需要继续判断
if (App::DebugTcpClient) {
int count = App::Keys.count();
for (int i = 0; i < count; i++) {
if (App::Keys.at(i) == buffer) {
sendData(App::Values.at(i));
break;
}
}
}
}
void frmUdpClient::readData()
{
QHostAddress host;
quint16 port;
QByteArray data;
QString buffer;
while (udpSocket->hasPendingDatagrams()) {
data.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(data.data(), data.size(), &host, &port);
if (App::HexReceiveUdpClient) {
buffer = QUIHelper::byteArrayToHexStr(data);
} else if (App::AsciiUdpClient) {
buffer = QUIHelper::byteArrayToAsciiStr(data);
} else {
buffer = QString(data);
}
QString ip = host.toString();
ip = ip.replace("::ffff:", "");
if (ip.iSEMpty()) {
continue;
}
QString str = QString("[%1:%2] %3").arg(ip).arg(port).arg(buffer);
append(1, str);
if (App::DebugUdpClient) {
int count = App::Keys.count();
for (int i = 0; i < count; i++) {
if (App::Keys.at(i) == buffer) {
sendData(ip, port, App::Values.at(i));
break;
}
}
}
}
}
以上是脚本宝典为你收集整理的Qt开源作品41-网络调试助手增强版V2022全部内容,希望文章能够帮你解决Qt开源作品41-网络调试助手增强版V2022所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。