说明:
1.QLocalServer继承自QObject。
2.QLocalServer提供了一个基于本地套接字(socket)的服务端(server)。
3.QLocalServer可以接受来自本地socket的连接。
4.server通过调用listen(),监听特定的连接。
5.newConnection()是在每次server与client连接上时所发出的信号。
6.nextPendingConnection()将等待中的连接当作一个已连接上的QLocalSocket。返回值是指向QLocalSocket的指针,这个QLocalSocket可以与client建立通信。
7.当发生错误时,serverError() 返回错误的类型,调用errorString()可以获取对错误的描述。
8.在监听过程中,通过 serverName()可以获取当前server监听的名称。
9.close()使QLocalServer停止对连接请求的监听。
10.虽然QLocalServer是为在事件循环中使用而设计出来的,但是在没有事件循环时也是可以使用的。没有事件循环时,你必须使用waitForNewConnection(),它只在以下两种情况下解除阻塞:1)有可用的连接;2)超时。
服务端:
#include <QApplication>
#include <QtNetwork>
class CServer
: public QObject
{
Q_OBJECT
public:
CServer()
: m_server(NULL)
{
m_server = new QLocalServer(this);
QObject::connect(m_server, SIGNAL(newConnection()), this, SLOT(serverNewConnectionHandler()));
}
~CServer()
{
m_server->close();
delete m_server;
}
void RunServer()
{
qDebug() << "Run Server ok";
QLocalServer::removeServer("ServerName");//先移除原来的,若不移除同时存在,listen就会失败
bool ok = m_server->listen("ServerName");//当有连接来时,QLocalServer会发生newConnection()信号,所以信号连接时在init中
if (!ok)
{
// TODO:
}
}
private slots:
void serverNewConnectionHandler()
{
qDebug() << "New Connection";
QLocalSocket* socket = m_server->nextPendingConnection();//获取客户端的QLocalServer
QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(socketReadyReadHandler()));
QObject::connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));
}
void socketReadyReadHandler()
{
QLocalSocket* socket = static_cast<QLocalSocket*>(sender());
if (socket)
{
QTextStream stream(socket);
qDebug() << "Read Data From Client:" << stream.readAll();
QString response = "Hello Client";
socket->write(response.toUtf8());
socket->flush();
}
// 返回到客户端的void sendMessage方法,m_socket->waitForReadyRead()之后的操作
}
private:
QLocalServer *m_server;
};
int main(int argc, char *argv[])
{
// 至qt4.8(以上的不知道),在window下QApplication
// 必需放到QLocalServer创建之前
// 参考地址:http://www.qtcentre.org/archive/index.php/t-43522.html?s=26444975027844d43142ce2238f4605e
QApplication app(argc, argv);
CServer server;
server.RunServer();
return app.exec();
}
#include "main.moc"
客户端:
#include <QApplication>
#include <QtNetwork>
class CClient
: public QObject
{
Q_OBJECT
public:
CClient()
: m_socket(NULL)
{
m_socket = new QLocalSocket();
QObject::connect(m_socket, SIGNAL(connected()), this, SLOT(socketConnectedHandler()));
QObject::connect(m_socket, SIGNAL(disconnected()), this, SLOT(socketDisConnectedHandler()));
QObject::connect(m_socket, SIGNAL(error(QLocalSocket::LocalSocketError)), this, SLOT(socketErrorHandler(QLocalSocket::LocalSocketError)));
}
~CClient()
{
m_socket->disconnectFromServer();
delete m_socket;
}
public:
void ConnectToServer(const QString &strServerName)
{
// 服务端的serverNewConnectionHandler成员方法将被调用
m_socket->connectToServer("ServerName");
if (m_socket->waitForConnected())
{
// TODO:
}
}
void sendMessage(const QString &msg)
{
m_socket->write(msg.toStdString().c_str());
m_socket->flush();
// waitForReadyRead将激发信号readyRead(), 我们在
// 与服务端创建连接时,已将readyRead(),绑定到了服务
// 端的槽socketReadyReadHandler成员方法
if (!m_socket->bytesAvailable())
m_socket->waitForReadyRead();
QTextStream stream(m_socket);
QString respond = stream.readAll();
qDebug() << "Read Data From Server:" << respond;
}
private slots:
void socketConnectedHandler()
{
qDebug() << "connected.";
}
void socketDisConnectedHandler()
{
qDebug() << "disconnected.";
}
void socketErrorHandler(QLocalSocket::LocalSocketError error)
{
qWarning() << error;
}
private:
QLocalSocket *m_socket;
};
int main(int argc, char *argv[])
{
// 至qt4.8(以上的不知道),在window下QApplication
// 必需放到QLocalServer创建之前
// 参考地址:http://www.qtcentre.org/archive/index.php/t-43522.html?s=26444975027844d43142ce2238f4605e
QApplication app(argc, argv);
CClient client;
client.ConnectToServer("ServerName");
client.sendMessage("Hellow Server");
return app.exec();
}
#include "main.moc"