客户端抽象类封装
#ifndef VSIPC_H
#define VSIPC_H
#include <QObject>
class VSIPCClient
{
public:
VSIPCClient() {}
~VSIPCClient() {}
protected:
virtual void initConnect()=0;
virtual void sendmsg(const QByteArray &msg)=0;
virtual void recvmsg(QByteArray &msg)=0;
};
#endif // VSIPC_H
实例1 利用QLocalSocket实现接口
#ifndef VSSOCKETCLIENT_H
#define VSSOCKETCLIENT_H
#include <QObject>
#include <QLocalSocket>
#include "vsipc.h"
class VSSocketClient : public QObject, public VSIPCClient
{
Q_OBJECT
public:
VSSocketClient();
~VSSocketClient();
protected:
virtual void initConnect();
virtual void sendmsg(const QByteArray &msg);
virtual void recvmsg(QByteArray &msg);
private slots:
void handleConnect();
void handleDisconnect();
void handleError(QLocalSocket::LocalSocketError error);
private:
QLocalSocket *m_socket;
bool m_bConnect;
};
#endif // VSSOCKETCLIENT_H
#include "vssocketclient.h"
VSSocketClient::VSSocketClient()
: m_bConnect(false)
, m_socket(NULL)
{
m_socket = new QLocalSocket();
connect(m_socket, SIGNAL(connected()), SLOT(handleConnect()));
connect(m_socket, SIGNAL(disconnected()), SLOT(handleDisconnect()));
connect(m_socket, SIGNAL(error(QLocalSocket::LocalSocketError)), SLOT(handleError(QLocalSocket::LocalSocketError)));
}
VSSocketClient::~VSSocketClient()
{
m_socket->disconnectFromServer();
delete m_socket;
}
void VSSocketClient::initConnect()
{
m_socket->connectToServer("servername"); //server name, server need listen it
if(!m_socket->waitForConnected(1000))
{
//do something
return;
}
}
void VSSocketClient::sendmsg(const QByteArray &msg)
{
if(m_bConnect)
{
m_socket->write(msg);
if(!m_socket->flush()) //write buffer to socket without blocking
{
//do something
}
}
else
{
//do something
}
}
void VSSocketClient::recvmsg(QByteArray &msg)
{
if(m_bConnect)
{
if(0 == m_socket->bytesAvailable())
{
m_socket->waitForReadyRead(); //signal readyRead()
}
QTextStream stream(m_socket);
QString data = stream.readAll();
msg = data.toStdString().c_str();
}
else
{
//do something
}
}
void VSSocketClient::handleConnect()
{
qDebug()<<"connected.";
m_bConnect = true;
}
void VSSocketClient::handleDisconnect()
{
qDebug()<<"disconnected.";
m_bConnect = false;
}
void VSSocketClient::handleError(QLocalSocket::LocalSocketError error)
{
qWarning()<<error;
m_bConnect = false;
}
实例2. 利用QTcpSocket实现接口
#ifndef VSTCPSOCKETCLIENT_H
#define VSTCPSOCKETCLIENT_H
#include "vsipc.h"
#include <QObject>
#include <QTcpSocket>
#include <QHostAddress>
class VSTcpSocketClient : public QObject, public VSIPCClient
{
Q_OBJECT
public:
VSTcpSocketClient();
~VSTcpSocketClient();
protected:
virtual void initConnect();
virtual void sendmsg(const QByteArray &msg);
virtual void recvmsg(QByteArray &msg);
private slots:
void handleConnect();
void handleDisconnect();
void handleError(QAbstractSocket::SocketError error);
void handleReadyRead();
private:
QTcpSocket *m_socket;
bool m_bConnect;
bool m_bReadyRead;
};
#endif // VSTCPSOCKETCLIENT_H
#include "vstcpsocketclient.h"
#define IPHOST 192.168.21.207
#define PORTHOST 8888
VSTcpSocketClient::VSTcpSocketClient()
: m_bConnect(false)
, m_bReadyRead(false)
, m_socket(NULL)
{
m_socket = new QTcpSocket();
connect(m_socket, SIGNAL(connected()), SLOT(handleConnect()));
connect(m_socket, SIGNAL(disconnected()), SLOT(handleDisconnect()));
connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(handleError(QAbstractSocket::SocketError)));
connect(m_socket, SIGNAL(readyRead()), SLOT(handleReadyRead()));
}
VSTcpSocketClient::~VSTcpSocketClient()
{
m_socket->disconnectFromHost();
delete m_socket;
}
void VSTcpSocketClient::initConnect()
{
QString ip; // = QString::fromStdString(IPHOST);
quint16 port = PORTHOST;
m_socket->connectToHost(QHostAddress(ip), port); //server ip and port
}
void VSTcpSocketClient::sendmsg(const QByteArray &msg)
{
if(m_bConnect)
{
QTextStream in(m_socket);
in << msg;
}
else
{
//do something
}
}
void VSTcpSocketClient::recvmsg(QByteArray &msg)
{
if(m_bConnect && m_bReadyRead)
{
QByteArray bytearray;
bytearray.resize(m_socket->bytesAvailable());
m_socket->read(bytearray.data(), bytearray.size());
}
else
{
//do something
}
}
void VSTcpSocketClient::handleConnect()
{
qDebug()<<"connected.";
m_bConnect = true;
}
void VSTcpSocketClient::handleDisconnect()
{
qDebug()<<"disconnected.";
m_bConnect = false;
m_bReadyRead = false;
}
void VSTcpSocketClient::handleError(QAbstractSocket::SocketError error)
{
qWarning()<<error;
m_bConnect = false;
m_bReadyRead = false;
}
void VSTcpSocketClient::handleReadyRead()
{
qDebug()<<"ready read.";
m_bReadyRead = true;
}