GrabBag/VrNets/tcpClient/Test/test_tcp_client.cpp

40 lines
1.1 KiB
C++
Raw Normal View History

#include "../Inc/VrTcpClient.h"
#include <QCoreApplication>
#include <QTimer>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
VrTcpClient client;
// 连接信号槽
QObject::connect(&client, &IVrTcpClient::connected, []() {
qDebug() << "Connected to server!";
});
QObject::connect(&client, &IVrTcpClient::disconnected, []() {
qDebug() << "Disconnected from server!";
});
QObject::connect(&client, &IVrTcpClient::connectionError, [](const QString &error) {
qDebug() << "Connection error:" << error;
});
QObject::connect(&client, &IVrTcpClient::dataReceived, [](const QByteArray &data) {
qDebug() << "Received data:" << data.toHex();
});
// 设置自动重连
client.setAutoReconnect(true);
client.setReconnectInterval(3000);
// 连接到服务器(这里使用一个不存在的地址进行测试)
client.connectToServer("127.0.0.1", 12345);
// 5秒后退出
QTimer::singleShot(5000, &app, &QCoreApplication::quit);
return app.exec();
}