头文件 #ifndef TCPSERVER_H
#define TCPSERVER_H
#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>
class TcpServer : public QObject
{
Q_OBJECT
public:
TcpServer(QObject *parent = Q_NULLPTR);
~TcpServer();
static TcpServer *geInstance();
/****************************************************
* 函数作用:发送数据
* 函数名称:sendData
* 输入参数:需要发送的数据 array
* 输出参数:无
* 返回值:bool
****************************************************/
bool sendData(const QByteArray& array);
/****************************************************
* 函数作用:发送数据
* 函数名称:sendData
* 输入参数:pBuffer 数据缓冲区,nLen 数据长度
* 输出参数:无
* 返回值:成功返回ture 失败false
****************************************************/
bool sendData(char *pBuffer,int nLen = 0);
/****************************************************
* 函数作用:获取收到数据缓冲区的长度
* 函数名称:getBuffer
* 输入参数:pBuffer
* 输出参数:pBuffer
* 返回值:无
****************************************************/
void getBuffer(char *pBuffer);
/****************************************************
* 函数作用:获取接收数据缓冲区的长度
* 函数名称:getBufferSize
* 输入参数:无
* 输出参数:无
* 返回值:返回数据长度
****************************************************/
int getBufferSize();
/****************************************************
* 函数作用:获取客户端ip
* 函数名称:getClientIp
* 输入参数:
* 输出参数:
* 返回值:获取客户端ip
****************************************************/
QString getClientIp();
/****************************************************
* 函数作用:获取客户端端口
* 函数名称:getCliengPort
* 输入参数:
* 输出参数:
* 返回值:客户端端口
****************************************************/
unsigned short getCliengPort();
/****************************************************
* 函数作用:获取客户端ip
* 函数名称:getClientHost
* 输入参数:
* 输出参数:
* 返回值:客户端名称
****************************************************/
QString getClientHost();
protected:
/****************************************************
* 函数作用:
* 函数名称:
* 输入参数:
* 输出参数:
* 返回值:
****************************************************/
void init();
private slots:
//获取新链接进来的客户端
void slotNewConnection();
//准备读取客户端发送过来的消息
void slotReadyRead();
//客户端连接
void slotConnected();
//客户端断开
void slotDisconnected();
signals:
//外部调用必须发送该信号才能收到消息
void sigRecvData(char* pBuffer,int nLen);
void sigArray(const QByteArray &array);
void sigConnected();
void sigDisconnect();
private:
QTcpSocket *m_pSocket;
QTcpServer *m_pServer;
//缓冲区
char *m_pBuffer;
//缓冲区长度
int m_nBuffSize;
QString m_clientIp;
QString m_clientName;
unsigned short m_usClientPort;
static TcpServer *m_pTcpServer;
};
#endif // TCPSERVER_H
源文件
#include "TcpServer.h"
#include <QDebug>
TcpServer *TcpServer::m_pTcpServer = Q_NULLPTR;
TcpServer::TcpServer(QObject *parent):QObject(parent),
m_pSocket(Q_NULLPTR),
m_pServer(Q_NULLPTR)
{
m_pBuffer = Q_NULLPTR;
m_pBuffer = new char [1500];
init();
}
TcpServer::~TcpServer()
{
if(m_pServer != Q_NULLPTR)
{
delete m_pServer;
m_pServer = Q_NULLPTR;
}
if(m_pSocket != Q_NULLPTR)
{
delete m_pSocket;
m_pSocket = Q_NULLPTR;
}
if(m_pBuffer != Q_NULLPTR)
{
delete [] m_pBuffer;
}
}
TcpServer *TcpServer::geInstance()
{
if(m_pTcpServer == Q_NULLPTR)
{
m_pTcpServer = new TcpServer;
}
return m_pTcpServer;
}
bool TcpServer::sendData(const QByteArray &array)
{
if(m_pSocket != Q_NULLPTR)
{
if(m_pSocket->write(array) <= 0)
{
qDebug()<<"send failed!";
return false;
}
}
return true;
}
bool TcpServer::sendData(char *pBuffer, int nLen)
{
if(m_pSocket != Q_NULLPTR)
{
if(m_pSocket->write(pBuffer,nLen) <= 0)
{
qDebug()<<"send failed!";
return false;
}
}
return true;
}
void TcpServer::getBuffer(char *pBuffer)
{
pBuffer = m_pBuffer;
}
int TcpServer::getBufferSize()
{
return m_nBuffSize;
}
QString TcpServer::getClientIp()
{
return m_clientIp;
}
unsigned short TcpServer::getCliengPort()
{
return m_usClientPort;
}
QString TcpServer::getClientHost()
{
return m_clientName;
}
void TcpServer::init()
{
m_pServer = new QTcpServer(this);
m_pServer->listen(QHostAddress::AnyIPv4,5000); //监听所以Ipv4,Any监听所有的Ip,
connect(m_pServer,&QTcpServer::newConnection,this,&TcpServer::slotNewConnection);//当客户端链接时触发信号
qDebug()<<"listening......";
}
void TcpServer::slotNewConnection()
{
if(m_pServer->hasPendingConnections())
{
m_pSocket = m_pServer->nextPendingConnection(); //获取练级套接字
if(m_pSocket != Q_NULLPTR)
{
m_clientIp = m_pSocket->peerAddress().toString();
m_usClientPort = m_pSocket->peerPort();
m_clientName = m_pSocket->peerName();
qDebug()<<m_pSocket->peerAddress().toString();
connect(m_pSocket,&QTcpSocket::readyRead,this,&TcpServer::slotReadyRead);
connect(m_pSocket,&QTcpSocket::disconnected,this,&TcpServer::slotDisconnected);
connect(m_pSocket,&QTcpSocket::connected,this,&TcpServer::slotConnected);
}
}
}
//收到客户端发送过来的消息
void TcpServer::slotReadyRead()
{
if(m_pSocket != Q_NULLPTR)
{
QByteArray array = m_pSocket->readAll();
if(!array.isEmpty())
{
m_nBuffSize = array.size();
m_pBuffer = array.data();
emit sigRecvData(array.data(),array.size());
emit sigArray(array);
}
}
}
void TcpServer::slotConnected()
{
qDebug()<<"The client is connected. Procedure";
emit sigConnected();
}
void TcpServer::slotDisconnected()
{
qDebug()<<"The client is disconnected. Procedure";
emit sigDisconnect();
}