Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说QTcpSocket实现客户端,希望能够帮助你!!!。
实现tcp客户端通信并支持keepAlive探测包
#pragma once
#include <QObject>
#include <QTcpSocket>
/**********************************************
* 作者:wujianhua
* 时间:2021/02/25
* 类介绍:socket tcp客户端通信类
*
***********************************************/
class TcpClient : public QObject
{
Q_OBJECT
public:
TcpClient(QObject *parent);
~TcpClient();
/**********************************************
* 作者:wujianhua
* 时间:2021/02/25
* 函数:连接服务器
* 参数:
* ip:服务器地址
* port:服务器端口
* timeout:超时时间
* 返回:
* 连接成功返回true,失败返回false
***********************************************/
bool connect(const QString ip, int port, int timeout = 3000);
/**********************************************
* 作者:wujianhua
* 时间:2021/02/25
* 函数:断开服务器连接
* 参数:
* 无
* 返回:
* 无
***********************************************/
void disconnect();
/**********************************************
* 作者:wujianhua
* 时间:2021/02/25
* 函数:是否连接
* 参数:
* 无
* 返回:
* 返回true为连接状态,false为断开状态
***********************************************/
bool isOnline();
/**********************************************
* 作者:wujianhua
* 时间:2021/02/25
* 函数:发送数据
* 参数:
* dat:数据对象
* 返回:
* 发送成功返回对应发的字节数,-1设备未连接
***********************************************/
int send(QByteArray dat);
/**********************************************
* 作者:wujianhua
* 时间:2021/02/25
* 函数:接收设备发送过来的数据
* 参数:
* buf:接收设备发送的数据缓存
* timeout:超时时间
* 返回:
* 返回接收到的数据字节数,-1设备未连接
***********************************************/
int recv(QByteArray &buf, int timeout = 10*1000);
/**********************************************
* 作者:wujianhua
* 时间:2021/03/02
* 函数:设置心跳保活机制
* 参数:
* keepIdle:没有数据交互后发送心跳探测包 单位秒
* keepInterval:探测包间隔时间 单位秒
* keepCount:探测重试次数
* 返回:
* 无
***********************************************/
void setKeepAlive(int keepIdle = 2, int keepInterval = 1, int keepCount = 3);
private slots:
void slot_connected();
void slot_readData();
void slot_disconnected();
void slot_stateChange(QAbstractSocket::SocketState state);
private:
QTcpSocket *_tcp = nullptr;
QString _ip;
int _port;
bool _online = false;
QByteArray _readbuf;
};
#include "TcpClient.h"
#include <QSettings>
#include <QDebug>
#ifdef Q_OS_WIN
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <ws2ipdef.h>
#include <mstcpip.h>
#include <winsock.h>
#pragma comment(lib, "Ws2_32.lib")
#endif // Q_OS_WIN
#ifdef Q_OS_LINUX
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#endif // Q_OS_LINUX
TcpClient::TcpClient(QObject *parent)
: QObject(parent){
_tcp = new QTcpSocket(this);
QObject::connect(_tcp, &QAbstractSocket::connected, this, &TcpClient::slot_connected);
QObject::connect(_tcp, &QIODevice::readyRead, this, &TcpClient::slot_readData);
QObject::connect(_tcp, &QAbstractSocket::disconnected, this, &TcpClient::slot_disconnected);
QObject::connect(_tcp, &QAbstractSocket::stateChanged, this, &TcpClient::slot_stateChange);
}
TcpClient::~TcpClient() {
disconnect();
delete _tcp;
_tcp = nullptr;
}
bool TcpClient::connect(const QString ip, int port, int timeout /*= 3000*/){
disconnect();
_tcp->abort();
_ip = ip;
_port = port;
_tcp->connectToHost(_ip, _port);
_tcp->waitForConnected(timeout);
if (_online) {
setKeepAlive();
}
return _online;
}
void TcpClient::disconnect(){
if (_online) {
_tcp->disconnectFromHost();
}
}
bool TcpClient::isOnline(){
return _online;
}
int TcpClient::send(QByteArray dat){
int size = -1;
if (_online) {
size = _tcp->write(dat);
_tcp->flush();
}
return size;
}
int TcpClient::recv(QByteArray &buf, int timeout /* = 10*1000 */){
int size = -1;
if (_online) {
_tcp->waitForReadyRead(timeout);
size = _readbuf.size();
buf = _readbuf;
}
return size;
}
void TcpClient::setKeepAlive(int keepIdle /* = 2 */, int keepInterval /* = 1 */, int keepCount /* = 3 */){
//设置keepAlive心跳检测
int fd = _tcp->socketDescriptor();
int keepAlive = 1; //开启keepAlive属性
setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char*)&keepAlive, sizeof(keepAlive));
setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, (char*)&keepIdle, sizeof(keepIdle));
setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, (char*)&keepInterval, sizeof(keepInterval));
setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, (char*)&keepCount, sizeof(keepCount));
}
void TcpClient::slot_connected(){
_online = true;
}
void TcpClient::slot_readData(){
_readbuf = _tcp->readAll();
}
void TcpClient::slot_disconnected(){
_online = false;
}
void TcpClient::slot_stateChange(QAbstractSocket::SocketState state){
switch (state){
case QAbstractSocket::UnconnectedState:
_online = false;
break;
case QAbstractSocket::ConnectedState:
_online = true;
break;
default:
break;
}
}
今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
上一篇
已是最后文章
下一篇
已是最新文章