参考博客:http://tgstdj.blog.163.com/blog/static/74820040201243072238780/
1. 建立一个QLocalServer实例 m_server2. 进行listen :
m_server->listen("servername")
3. 当有连接到来时,QLocalServer会发送netConnection()信号,所以进行信号连接,在init()函数中:
connect(m_server, SIGNAL(newConnection()),this, SLOT(newConnection()));
4.在newConnection()函数中取得连接客户端的QLocalSocket
5.当QLocalSocket可以读数据时它会发送readyRead()信号,所以对newsocket进行信号连接,在newConnection()函数中:
connect(newsocket,SIGNAL(readyRead()), this, SLOT(readyRead()));
6.在readyRead()函数中读取数据:
// 取得是哪个localsocket可以读数据了
QLocalSocket *local = static_cast(sender());
if (!local)
return;
QTextStream in(local);
QString readMsg;
// 读出数据
全部代码, QT新建一个项目,主窗口mainwindow。
server.h:(尖括号替换为了引号,因为新浪里尖括号无法直接打出)
#ifndefSERVER_H
#define SERVER_H
#include "QObject"
#include "QtNetwork/QLocalServer"
#include "QtNetwork/QLocalSocket"
#include "QString"
#include "QTextStream"
class Server : public QObject
{
Q_OBJECT
public:
explicit Server(QObject *parent = 0);
~Server();
int init(const QString & servername);
private:
int isServerRun(const QString & servername);
QLocalServer *m_server;
signals:
void newMessage(const QString &msg);
public slots:
void newConnection();
void readyRead();
};
#endif // SERVER_H
server.cpp:
#include "server.h"
Server::Server(QObject *parent) :
QObject(parent)
{
m_server = 0;
}
Server::~Server()
{
if (m_server)
delete m_server;
}
int Server::init(const QString &servername)
{
//已经有实例在运行,返回0
if (isServerRun(servername))
return 0;
m_server = new QLocalServer;
//先移除原来存在的,如果不移除,如果//servername已经存在就会listen失败 QLocalServer::removeServer(servername);
//监听
m_server->listen(servername); connect(m_server,SIGNAL(newConnection()),this,SLOT(newConnection()));
return 1;
}
void Server::newConnection()
{
//有新连接来了
QLocalSocket *newsocket =m_server->nextPendingConnection();connect(newsocket,SIGNAL(readyRead()),this,SLOT(readyRead()));
}
void Server::readyRead()
{
//取得是哪个localsocket可以读数据了
QLocalSocket *local =static_cast(sender());
if (!local)
return ;
QTextStream in(local);
QString readMsg;
//读出数据
readMsg = in.readAll();
//发送收到数据信号
emit newMessage(readMsg);
}
int Server::isServerRun(const QString &servername)
{
//判断是否有一个同名的服务器在运行
QLocalSocket ls;
ls.connectToServer(servername);
if (ls.waitForConnected(1000))
{
//已经在运行
ls.disconnectFromServer()
; ls.close();
return 1;
}
return 0;
}
main.cpp:
#include"mainwindow.h"
#include "QApplication"
#include "QLabel"
#include "QMessageBox"
#include "server.h"
#include "QObject"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLabel text("test");
Server s;
if (!s.init("localserver-test"))
{
//初始化失败,说明已经有一个服务在运行
QMessageBox::information(&text,"info","Thereis already exist one!");
return 1;
}
QObject::connect(&s,SIGNAL(newMessage(const QString&)),&text,SLOT(setText(const QString &)));text.show();
return a.exec();
}
工程结构:
#include "mainwindow.h"
1. 建立一个QLocalServer实例 m_server2. 进行listen :
m_server->listen("servername")
3. 当有连接到来时,QLocalServer会发送netConnection()信号,所以进行信号连接,在init()函数中:
connect(m_server, SIGNAL(newConnection()),this, SLOT(newConnection()));
4.在newConnection()函数中取得连接客户端的QLocalSocket
QLocalSocket *newsocket =m_server->nextPendingConnection();
connect(newsocket,SIGNAL(readyRead()), this, SLOT(readyRead()));
6.在readyRead()函数中读取数据:
// 取得是哪个localsocket可以读数据了
QLocalSocket *local = static_cast(sender());
if (!local)
return;
QTextStream in(local);
QString readMsg;
// 读出数据
readMsg = in.readAll();
全部代码, QT新建一个项目,主窗口mainwindow。
server.h:(尖括号替换为了引号,因为新浪里尖括号无法直接打出)
#ifndefSERVER_H
#define SERVER_H
#include "QObject"
#include "QtNetwork/QLocalServer"
#include "QtNetwork/QLocalSocket"
#include "QString"
#include "QTextStream"
class Server : public QObject
{
Q_OBJECT
public:
explicit Server(QObject *parent = 0);
~Server();
int init(const QString & servername);
private:
int isServerRun(const QString & servername);
QLocalServer *m_server;
signals:
void newMessage(const QString &msg);
public slots:
void newConnection();
void readyRead();
};
#endif // SERVER_H
server.cpp:
#include "server.h"
Server::Server(QObject *parent) :
QObject(parent)
{
m_server = 0;
}
Server::~Server()
{
if (m_server)
delete m_server;
}
int Server::init(const QString &servername)
{
//已经有实例在运行,返回0
if (isServerRun(servername))
return 0;
m_server = new QLocalServer;
//先移除原来存在的,如果不移除,如果//servername已经存在就会listen失败 QLocalServer::removeServer(servername);
//监听
m_server->listen(servername); connect(m_server,SIGNAL(newConnection()),this,SLOT(newConnection()));
return 1;
}
void Server::newConnection()
{
//有新连接来了
QLocalSocket *newsocket =m_server->nextPendingConnection();connect(newsocket,SIGNAL(readyRead()),this,SLOT(readyRead()));
}
void Server::readyRead()
{
//取得是哪个localsocket可以读数据了
QLocalSocket *local =static_cast(sender());
if (!local)
return ;
QTextStream in(local);
QString readMsg;
//读出数据
readMsg = in.readAll();
//发送收到数据信号
emit newMessage(readMsg);
}
int Server::isServerRun(const QString &servername)
{
//判断是否有一个同名的服务器在运行
QLocalSocket ls;
ls.connectToServer(servername);
if (ls.waitForConnected(1000))
{
//已经在运行
ls.disconnectFromServer()
; ls.close();
return 1;
}
return 0;
}
main.cpp:
#include"mainwindow.h"
#include "QApplication"
#include "QLabel"
#include "QMessageBox"
#include "server.h"
#include "QObject"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLabel text("test");
Server s;
if (!s.init("localserver-test"))
{
//初始化失败,说明已经有一个服务在运行
QMessageBox::information(&text,"info","Thereis already exist one!");
return 1;
}
QObject::connect(&s,SIGNAL(newMessage(const QString&)),&text,SLOT(setText(const QString &)));text.show();
return a.exec();
}
工程结构:
客户端测试:
main.cpp:
#include "mainwindow.h"
#include "QApplication"
#include "QtNetwork/QLocalSocket"
#include "stdlib.h"
#include "unistd.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLocalSocket ls;
ls.connectToServer("localserver-test");
srand(1000);
if (ls.waitForConnected())
{
while(1)
{
QTextStream ts(&ls);
ts<<QString::number(rand()) + "\nTTTTTTTTTTT" + "\nXXXXXXXXXXX";
ts.flush();
ls.waitForBytesWritten();
sleep(1);
}
}
MainWindow w;
w.show();
return a.exec();
}