前言
QTcpSocket QUdpSocket 在多线程里的其中一种方式代码分享 说明一点:QTcpSocket QUdpSocket 都是基于 QAbstractSocket,都是异步调用,绝大多数据情况下是不需要使用多线程的,不会阻塞UI线程。
看官方说明就得知,在线程中使用waitFor**系列的阻塞函数是最简单的通信方式,但Qt官方有个警告: 注意:此函数在Windows上可能随机失败。如果您的软件要在Windows上运行,可以考虑使用事件循环和readyRead()信号。
所以在windows上,我这里写一个简单的线程客户端,同时有一个重要功能,断线自动重连,只要识别到服务器主动断开,或者本地网络出现问题等错误引起的断开,都会自动重连,代码并不完善,只是提供一个思路.
一、QTcpSocket
LinkTCP.h
class LinkTCP : public QThread{
Q_OBJECT
public:
LinkTCP()
void close();
void writeByte(const QByteArray& byte);
void run() override;
signals:
void receivecBytes(const QByteArray& byte);
private:
QMutex m_mutex;
QList<QByteArray> m_list;
QString m_ip;
quint16 m_port;
std::atomic_bool m_isConnected;
}
LinkTCP.cpp
void LinkTCP::close(){
m_isConnected = false;
requestInterruption();
quit();
wait();
}
void LinkTCP::writeByte(const QByteArray& byte){
m_mutex.lock();
m_list.append(byte)
m_mutex.unlock();
}
void LinkTCP::run(){
threadConnect();
}
void LinkTCP::threadConnect()
{
m_mutex.lock();
QString ip = m_ip;
quint16 port = m_port;
m_mutex.unlock();
while (!isInterruptionRequested()) {
QTcpSocket socket;
socket.connectToHost(ip, port);
auto c1 = connect(&socket, &QTcpSocket::connected, this, [&]() {
m_isConnected = true;
quit();
});
auto c2 = connect(&socket, &QTcpSocket::errorOccurred, this, [&]() {
m_isConnected = false;
quit();
});
auto c3 = connect(&socket, &QTcpSocket::disconnected, this, [&]() {
m_isConnected = false;
quit();
});
auto c4 = connect(
&socket, &QTcpSocket::readyRead, this,
[&]() {
auto byte_size = socket.bytesAvailable();
if (byte_size > 0) {
QByteArray buffer;
buffer.resize(byte_size);
socket.read(buffer.data(), buffer.size());
emit receivecBytes(buffer);
}
},
Qt::DirectConnection);
do {
exec();
QMutexLocker l(&m_mutex);
while (!m_list.isEmpty()) {
socket.write(m_list.takeFirst());
}
l.unlock();
} while (m_isConnected);
disconnect(c1);
disconnect(c2);
disconnect(c3);
disconnect(c4);
socket.abort();
socket.close();
}
}
二、QUdpSocket
Udp 同Tcp的思路一样
void LinkUDP::threadConnect()
{
bool m_isConnected = false;
m_mutex.lock();
QHostAddress ip(m_ip);
quint16 port = m_port;
LinkType type = m_type;
m_mutex.unlock();
LOG_DEBUG() << ip << port;
while (!isInterruptionRequested()) {
QUdpSocket socket;
if (!socket.bind(333, QUdpSocket::ShareAddress)) {
LOG_DEBUG() << "error" << socket.errorString();
msleep(1000);
continue;
}
auto c1 = connect(&socket, &QUdpSocket::connected, this, [&]() {
m_isConnected = true;
quit();
});
auto c2 = connect(&socket, &QUdpSocket::errorOccurred, this, [&]() {
m_isConnected = false;
quit();
});
auto c3 = connect(&socket, &QUdpSocket::disconnected, this, [&]() {
m_isConnected = false;
quit();
});
auto c4 = connect(
&socket, &QUdpSocket::readyRead, this,
[&]() {
LOG_DEBUG() << currentThreadId();
while (socket.hasPendingDatagrams()) {
QByteArray buffer;
buffer.resize(socket.pendingDatagramSize());
QHostAddress sender;
quint16 sendPort;
int len = socket.readDatagram(buffer.data(), buffer.size(), &sender, &sendPort);
LOG_DEBUG() << sender << sendPort;
if (len == -1)
break;
emit receivecBytes(buffer);
}
},
Qt::DirectConnection);
LOG_DEBUG() << currentThreadId();
do {
exec();
m_mutex.lock();
while (!m_data.isEmpty()) {
socket.writeDatagram(m_data.takeFirst(), ip, port);
}
m_mutex.unlock();
} while (m_isConnected);
disconnect(c1);
disconnect(c2);
disconnect(c3);
disconnect(c4);
socket.abort();
socket.close();
}
}
总结
这只是使用线程的其中一种写法,当然也可以使用moveToThread()
|