QLocalServer与QLocalSocket进程通讯
生活随笔
收集整理的這篇文章主要介紹了
QLocalServer与QLocalSocket进程通讯
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在Qt中,提供了多種IPC方法,作者所用的是QLocalServer和QLocalSocket??雌饋砗孟窈蚐ocket搭上點(diǎn)邊,實(shí)則底層是windows的name pipe。這應(yīng)該是支持雙工通信的。 一 QLocalServer #ifndef VXMAINWINDOW_H
#define VXMAINWINDOW_H #include <QWidget>
#include <QtNetwork/QLocalServer>
#include <QtNetwork/QLocalSocket> class QPushButton;
class QTextEdit; class CVxMainWindow : public QWidget
{
?Q_OBJECT public:
?CVxMainWindow(QWidget *parent=NULL);
?~CVxMainWindow();
protected:
?void resizeEvent(QResizeEvent *);
private slots:
?void Btn_ListenClickedSlot();
?void Btn_StopListenClickedSlot();
?void newConnectionSlot();
?void dataReceived();
private:
?QLocalServer *m_pServer;
?QLocalSocket *m_pSocket; QPushButton *m_pBtn_Listen;
?QPushButton *m_pBtn_StopListen;
?QTextEdit?? *m_pEdt_Info;
}; #endif // VXMAINWINDOW_H #include "VxMainWindow.h" #include <QtGui/QtGui> CVxMainWindow::CVxMainWindow(QWidget *parent)
?: QWidget(parent)
{
?m_pBtn_Listen???? = new QPushButton(QObject::tr("開始監(jiān)聽"), this);
?m_pBtn_StopListen = new QPushButton(QObject::tr("停止監(jiān)聽"), this);
?m_pEdt_Info?????? = new QTextEdit(this);
?m_pServer???????? = new QLocalServer(this); connect(m_pBtn_Listen,???? SIGNAL(clicked()), this, SLOT(Btn_ListenClickedSlot()));
?connect(m_pBtn_StopListen, SIGNAL(clicked()), this, SLOT(Btn_StopListenClickedSlot()));
?connect(m_pServer,???????? SIGNAL(newConnection()), this, SLOT(newConnectionSlot()));
} CVxMainWindow::~CVxMainWindow()
{ } void CVxMainWindow::resizeEvent(QResizeEvent *)
{
?m_pBtn_Listen->setGeometry(10, 5, 80, 20);
?m_pBtn_StopListen->setGeometry(100, 5, 80, 20);
?m_pEdt_Info->setGeometry(0, 30, width(), height() - 30);
} void CVxMainWindow::Btn_ListenClickedSlot()
{
?if (!m_pServer->isListening())
?{
??if (m_pServer->listen(QObject::tr("AAA")))?
??{
???m_pEdt_Info->append(QObject::tr("打開監(jiān)聽端口成功!"));
??}
??else
??{
???m_pEdt_Info->append(QObject::tr("打開監(jiān)聽端口失敗!"));
??}
?}
?else
?{
??m_pEdt_Info->append(QObject::tr("正在監(jiān)聽中...!"));
?}
} void CVxMainWindow::Btn_StopListenClickedSlot()
{
?if (m_pServer->isListening())
?{
??m_pServer->close();
??m_pEdt_Info->append(QObject::tr("停止監(jiān)聽!"));
?}
} void CVxMainWindow::newConnectionSlot()
{
?m_pEdt_Info->append(QObject::tr("有新客戶端連接到服務(wù)器"));
?m_pSocket = m_pServer->nextPendingConnection();
?connect(m_pSocket, SIGNAL(disconnected()), m_pSocket, SLOT(deleteLater()));
?connect(m_pSocket, SIGNAL(readyRead()),this, SLOT(dataReceived())); int length = 0;
?QString vMsgStr = QObject::tr("Welcome");
?if((length=m_pSocket->write(vMsgStr.toLatin1(),vMsgStr.length()))!=vMsgStr.length())
?{ }
} void CVxMainWindow::dataReceived()
{
?while(m_pSocket->bytesAvailable())
?{???????
??QString vTemp;
??vTemp = m_pSocket->readLine();??????????
??m_pEdt_Info->append(vTemp); int length = 0;
??QString vMsgStr = QObject::tr("回復(fù):") + vTemp;
??if((length=m_pSocket->write(vMsgStr.toLatin1(),vMsgStr.length()))!=vMsgStr.length())
??{ }
?}
} 二 QLocalSocket #ifndef VXMAINWINDOW_H
#define VXMAINWINDOW_H #include <QWidget>
#include <QtNetwork/QLocalSocket> class QPushButton;
class QTextEdit;
class QLineEdit; class CVxMainWindow : public QWidget
{
?Q_OBJECT public:
?CVxMainWindow(QWidget *parent=NULL);
?~CVxMainWindow();
protected:
?void resizeEvent(QResizeEvent *);
?private slots:
??void Btn_ConnectClickedSlot();
??void Btn_DisConnectClickedSlot();
??void Btn_SendClickedSlot();
??void connectedSlot();
??void disconnectedSlot();
??void dataReceived();
??void displayError(QAbstractSocket::SocketError);
private:
?QLocalSocket *m_pSocket; QPushButton *m_pBtn_Connect;
?QPushButton *m_pBtn_DisConnect;
?QTextEdit?? *m_pEdt_Info;
?QLineEdit?? *m_pEdt_Send;
?QPushButton *m_pBtn_Send;
}; #endif // VXMAINWINDOW_H #include "VxMainWindow.h" #include <QtGui/QtGui> CVxMainWindow::CVxMainWindow(QWidget *parent)
?: QWidget(parent)
{
?m_pBtn_Connect??? = new QPushButton(QObject::tr("連接服務(wù)器"), this);
?m_pBtn_DisConnect = new QPushButton(QObject::tr("斷開連接"), this);
?m_pEdt_Send?????? = new QLineEdit(this);
?m_pBtn_Send?????? = new QPushButton(QObject::tr("發(fā)送"), this);
?m_pEdt_Info = new QTextEdit(this);
?m_pSocket = new QLocalSocket(this); connect(m_pBtn_Connect,??? SIGNAL(clicked()), this, SLOT(Btn_ConnectClickedSlot()));
?connect(m_pBtn_DisConnect, SIGNAL(clicked()), this, SLOT(Btn_DisConnectClickedSlot()));
?connect(m_pBtn_Send,?????? SIGNAL(clicked()), this, SLOT(Btn_SendClickedSlot()));
?connect(m_pSocket, SIGNAL(connected()), this, SLOT(connectedSlot()));
?connect(m_pSocket, SIGNAL(disconnected()), this, SLOT(disconnectedSlot()));
?connect(m_pSocket, SIGNAL(readyRead()),this, SLOT(dataReceived()));
?connect(m_pSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
} CVxMainWindow::~CVxMainWindow()
{ } void CVxMainWindow::resizeEvent(QResizeEvent *)
{
?m_pBtn_Connect->setGeometry(10, 5, 80, 20);
?m_pBtn_DisConnect->setGeometry(100, 5, 80, 20);
?m_pEdt_Send->setGeometry(10, 30, 150, 20);
?m_pBtn_Send->setGeometry(170, 30, 80, 20);
?m_pEdt_Info->setGeometry(0, 60, width(), height() - 60);
} void CVxMainWindow::Btn_ConnectClickedSlot()
{
?m_pSocket->connectToServer(QObject::tr("AAA"));
} void CVxMainWindow::Btn_DisConnectClickedSlot()
{
?m_pSocket->disconnectFromServer();
} void CVxMainWindow::Btn_SendClickedSlot()
{
?int length = 0;
?QString vMsgStr = m_pEdt_Send->text();
?if((length=m_pSocket->write(vMsgStr.toLatin1(),vMsgStr.length()))!=vMsgStr.length())
?{
??m_pEdt_Info->append(QObject::tr("發(fā)送信息失敗:") + vMsgStr);
?}
} void CVxMainWindow::connectedSlot()
{
?m_pEdt_Info->append(QObject::tr("成功連接到服務(wù)器!"));
} void CVxMainWindow::disconnectedSlot()
{
?m_pEdt_Info->append(QObject::tr("斷開與服務(wù)器的連接!"));
} void CVxMainWindow::dataReceived()
{
?while(m_pSocket->bytesAvailable())
?{???????
??QString vTemp;
??vTemp = m_pSocket->readLine();??????????
??m_pEdt_Info->append(vTemp);
?}
} void CVxMainWindow::displayError(QAbstractSocket::SocketError)
{ }
#define VXMAINWINDOW_H #include <QWidget>
#include <QtNetwork/QLocalServer>
#include <QtNetwork/QLocalSocket> class QPushButton;
class QTextEdit; class CVxMainWindow : public QWidget
{
?Q_OBJECT public:
?CVxMainWindow(QWidget *parent=NULL);
?~CVxMainWindow();
protected:
?void resizeEvent(QResizeEvent *);
private slots:
?void Btn_ListenClickedSlot();
?void Btn_StopListenClickedSlot();
?void newConnectionSlot();
?void dataReceived();
private:
?QLocalServer *m_pServer;
?QLocalSocket *m_pSocket; QPushButton *m_pBtn_Listen;
?QPushButton *m_pBtn_StopListen;
?QTextEdit?? *m_pEdt_Info;
}; #endif // VXMAINWINDOW_H #include "VxMainWindow.h" #include <QtGui/QtGui> CVxMainWindow::CVxMainWindow(QWidget *parent)
?: QWidget(parent)
{
?m_pBtn_Listen???? = new QPushButton(QObject::tr("開始監(jiān)聽"), this);
?m_pBtn_StopListen = new QPushButton(QObject::tr("停止監(jiān)聽"), this);
?m_pEdt_Info?????? = new QTextEdit(this);
?m_pServer???????? = new QLocalServer(this); connect(m_pBtn_Listen,???? SIGNAL(clicked()), this, SLOT(Btn_ListenClickedSlot()));
?connect(m_pBtn_StopListen, SIGNAL(clicked()), this, SLOT(Btn_StopListenClickedSlot()));
?connect(m_pServer,???????? SIGNAL(newConnection()), this, SLOT(newConnectionSlot()));
} CVxMainWindow::~CVxMainWindow()
{ } void CVxMainWindow::resizeEvent(QResizeEvent *)
{
?m_pBtn_Listen->setGeometry(10, 5, 80, 20);
?m_pBtn_StopListen->setGeometry(100, 5, 80, 20);
?m_pEdt_Info->setGeometry(0, 30, width(), height() - 30);
} void CVxMainWindow::Btn_ListenClickedSlot()
{
?if (!m_pServer->isListening())
?{
??if (m_pServer->listen(QObject::tr("AAA")))?
??{
???m_pEdt_Info->append(QObject::tr("打開監(jiān)聽端口成功!"));
??}
??else
??{
???m_pEdt_Info->append(QObject::tr("打開監(jiān)聽端口失敗!"));
??}
?}
?else
?{
??m_pEdt_Info->append(QObject::tr("正在監(jiān)聽中...!"));
?}
} void CVxMainWindow::Btn_StopListenClickedSlot()
{
?if (m_pServer->isListening())
?{
??m_pServer->close();
??m_pEdt_Info->append(QObject::tr("停止監(jiān)聽!"));
?}
} void CVxMainWindow::newConnectionSlot()
{
?m_pEdt_Info->append(QObject::tr("有新客戶端連接到服務(wù)器"));
?m_pSocket = m_pServer->nextPendingConnection();
?connect(m_pSocket, SIGNAL(disconnected()), m_pSocket, SLOT(deleteLater()));
?connect(m_pSocket, SIGNAL(readyRead()),this, SLOT(dataReceived())); int length = 0;
?QString vMsgStr = QObject::tr("Welcome");
?if((length=m_pSocket->write(vMsgStr.toLatin1(),vMsgStr.length()))!=vMsgStr.length())
?{ }
} void CVxMainWindow::dataReceived()
{
?while(m_pSocket->bytesAvailable())
?{???????
??QString vTemp;
??vTemp = m_pSocket->readLine();??????????
??m_pEdt_Info->append(vTemp); int length = 0;
??QString vMsgStr = QObject::tr("回復(fù):") + vTemp;
??if((length=m_pSocket->write(vMsgStr.toLatin1(),vMsgStr.length()))!=vMsgStr.length())
??{ }
?}
} 二 QLocalSocket #ifndef VXMAINWINDOW_H
#define VXMAINWINDOW_H #include <QWidget>
#include <QtNetwork/QLocalSocket> class QPushButton;
class QTextEdit;
class QLineEdit; class CVxMainWindow : public QWidget
{
?Q_OBJECT public:
?CVxMainWindow(QWidget *parent=NULL);
?~CVxMainWindow();
protected:
?void resizeEvent(QResizeEvent *);
?private slots:
??void Btn_ConnectClickedSlot();
??void Btn_DisConnectClickedSlot();
??void Btn_SendClickedSlot();
??void connectedSlot();
??void disconnectedSlot();
??void dataReceived();
??void displayError(QAbstractSocket::SocketError);
private:
?QLocalSocket *m_pSocket; QPushButton *m_pBtn_Connect;
?QPushButton *m_pBtn_DisConnect;
?QTextEdit?? *m_pEdt_Info;
?QLineEdit?? *m_pEdt_Send;
?QPushButton *m_pBtn_Send;
}; #endif // VXMAINWINDOW_H #include "VxMainWindow.h" #include <QtGui/QtGui> CVxMainWindow::CVxMainWindow(QWidget *parent)
?: QWidget(parent)
{
?m_pBtn_Connect??? = new QPushButton(QObject::tr("連接服務(wù)器"), this);
?m_pBtn_DisConnect = new QPushButton(QObject::tr("斷開連接"), this);
?m_pEdt_Send?????? = new QLineEdit(this);
?m_pBtn_Send?????? = new QPushButton(QObject::tr("發(fā)送"), this);
?m_pEdt_Info = new QTextEdit(this);
?m_pSocket = new QLocalSocket(this); connect(m_pBtn_Connect,??? SIGNAL(clicked()), this, SLOT(Btn_ConnectClickedSlot()));
?connect(m_pBtn_DisConnect, SIGNAL(clicked()), this, SLOT(Btn_DisConnectClickedSlot()));
?connect(m_pBtn_Send,?????? SIGNAL(clicked()), this, SLOT(Btn_SendClickedSlot()));
?connect(m_pSocket, SIGNAL(connected()), this, SLOT(connectedSlot()));
?connect(m_pSocket, SIGNAL(disconnected()), this, SLOT(disconnectedSlot()));
?connect(m_pSocket, SIGNAL(readyRead()),this, SLOT(dataReceived()));
?connect(m_pSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
} CVxMainWindow::~CVxMainWindow()
{ } void CVxMainWindow::resizeEvent(QResizeEvent *)
{
?m_pBtn_Connect->setGeometry(10, 5, 80, 20);
?m_pBtn_DisConnect->setGeometry(100, 5, 80, 20);
?m_pEdt_Send->setGeometry(10, 30, 150, 20);
?m_pBtn_Send->setGeometry(170, 30, 80, 20);
?m_pEdt_Info->setGeometry(0, 60, width(), height() - 60);
} void CVxMainWindow::Btn_ConnectClickedSlot()
{
?m_pSocket->connectToServer(QObject::tr("AAA"));
} void CVxMainWindow::Btn_DisConnectClickedSlot()
{
?m_pSocket->disconnectFromServer();
} void CVxMainWindow::Btn_SendClickedSlot()
{
?int length = 0;
?QString vMsgStr = m_pEdt_Send->text();
?if((length=m_pSocket->write(vMsgStr.toLatin1(),vMsgStr.length()))!=vMsgStr.length())
?{
??m_pEdt_Info->append(QObject::tr("發(fā)送信息失敗:") + vMsgStr);
?}
} void CVxMainWindow::connectedSlot()
{
?m_pEdt_Info->append(QObject::tr("成功連接到服務(wù)器!"));
} void CVxMainWindow::disconnectedSlot()
{
?m_pEdt_Info->append(QObject::tr("斷開與服務(wù)器的連接!"));
} void CVxMainWindow::dataReceived()
{
?while(m_pSocket->bytesAvailable())
?{???????
??QString vTemp;
??vTemp = m_pSocket->readLine();??????????
??m_pEdt_Info->append(vTemp);
?}
} void CVxMainWindow::displayError(QAbstractSocket::SocketError)
{ }
轉(zhuǎn)載于:https://www.cnblogs.com/lvdongjie/p/3746928.html
總結(jié)
以上是生活随笔為你收集整理的QLocalServer与QLocalSocket进程通讯的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求一个好听的qq名字
- 下一篇: 搭配无能来求助了~~大家来看看这件皮衣怎