QLocalServer与QLocalSocket进程通讯
2015-12-11 22:36
453 查看
在Qt中,提供了多种IPC方法,作者所用的是QLocalServer和QLocalSocket。看起来好像和Socket搭上点边,实则底层是windows的name pipe。这应该是支持双工通信的。
一 QLocalServer
#ifndef VXMAINWINDOW_H
#define VXMAINWINDOW_H
#include
#include
#include
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
CVxMainWindow::CVxMainWindow(QWidget *parent)
: QWidget(parent)
{
m_pBtn_Listen = new QPushButton(QObject::tr("开始监听"), this);
m_pBtn_StopListen = new QPushButton(QObject::tr("停止监听"), 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("打开监听端口成功!"));
}
else
{
m_pEdt_Info->append(QObject::tr("打开监听端口失败!"));
}
}
else
{
m_pEdt_Info->append(QObject::tr("正在监听中...!"));
}
}
void CVxMainWindow::Btn_StopListenClickedSlot()
{
if (m_pServer->isListening())
{
m_pServer->close();
m_pEdt_Info->append(QObject::tr("停止监听!"));
}
}
void CVxMainWindow::newConnectionSlot()
{
m_pEdt_Info->append(QObject::tr("有新客户端连接到服务器"));
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("回复:") + vTemp;
if((length=m_pSocket->write(vMsgStr.toLatin1(),vMsgStr.length()))!=vMsgStr.length())
{
}
}
}
二 QLocalSocket
#ifndef VXMAINWINDOW_H
#define VXMAINWINDOW_H
#include
#include
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
CVxMainWindow::CVxMainWindow(QWidget *parent)
: QWidget(parent)
{
m_pBtn_Connect = new QPushButton(QObject::tr("连接服务器"), this);
m_pBtn_DisConnect = new QPushButton(QObject::tr("断开连接"), this);
m_pEdt_Send = new QLineEdit(this);
m_pBtn_Send = new QPushButton(QObject::tr("发送"), 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("发送信息失败:") + vMsgStr);
}
}
void CVxMainWindow::connectedSlot()
{
m_pEdt_Info->append(QObject::tr("成功连接到服务器!"));
}
void CVxMainWindow::disconnectedSlot()
{
m_pEdt_Info->append(QObject::tr("断开与服务器的连接!"));
}
void CVxMainWindow::dataReceived()
{
while(m_pSocket->bytesAvailable())
{
QString vTemp;
vTemp = m_pSocket->readLine();
m_pEdt_Info->append(vTemp);
}
}
void CVxMainWindow::displayError(QAbstractSocket::SocketError)
{
} http://blog.chinaunix.net/uid-20718335-id-1993073.html
一 QLocalServer
#ifndef VXMAINWINDOW_H
#define VXMAINWINDOW_H
#include
#include
#include
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
CVxMainWindow::CVxMainWindow(QWidget *parent)
: QWidget(parent)
{
m_pBtn_Listen = new QPushButton(QObject::tr("开始监听"), this);
m_pBtn_StopListen = new QPushButton(QObject::tr("停止监听"), 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("打开监听端口成功!"));
}
else
{
m_pEdt_Info->append(QObject::tr("打开监听端口失败!"));
}
}
else
{
m_pEdt_Info->append(QObject::tr("正在监听中...!"));
}
}
void CVxMainWindow::Btn_StopListenClickedSlot()
{
if (m_pServer->isListening())
{
m_pServer->close();
m_pEdt_Info->append(QObject::tr("停止监听!"));
}
}
void CVxMainWindow::newConnectionSlot()
{
m_pEdt_Info->append(QObject::tr("有新客户端连接到服务器"));
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("回复:") + vTemp;
if((length=m_pSocket->write(vMsgStr.toLatin1(),vMsgStr.length()))!=vMsgStr.length())
{
}
}
}
二 QLocalSocket
#ifndef VXMAINWINDOW_H
#define VXMAINWINDOW_H
#include
#include
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
CVxMainWindow::CVxMainWindow(QWidget *parent)
: QWidget(parent)
{
m_pBtn_Connect = new QPushButton(QObject::tr("连接服务器"), this);
m_pBtn_DisConnect = new QPushButton(QObject::tr("断开连接"), this);
m_pEdt_Send = new QLineEdit(this);
m_pBtn_Send = new QPushButton(QObject::tr("发送"), 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("发送信息失败:") + vMsgStr);
}
}
void CVxMainWindow::connectedSlot()
{
m_pEdt_Info->append(QObject::tr("成功连接到服务器!"));
}
void CVxMainWindow::disconnectedSlot()
{
m_pEdt_Info->append(QObject::tr("断开与服务器的连接!"));
}
void CVxMainWindow::dataReceived()
{
while(m_pSocket->bytesAvailable())
{
QString vTemp;
vTemp = m_pSocket->readLine();
m_pEdt_Info->append(vTemp);
}
}
void CVxMainWindow::displayError(QAbstractSocket::SocketError)
{
} http://blog.chinaunix.net/uid-20718335-id-1993073.html
相关文章推荐
- 【Linux程序设计】之进程间的通信
- 操作系统:死锁的产生、条件、和解锁
- UML之C++类间关系总结
- Mac Eclipse JAVAHL问题
- RHEL5中实现各种服务的准备条件:
- Spark Working with Key/Value Pairs
- QTcpServer与QTcpSocket通讯
- Eclipse中Ant的配置与测试 转
- 20135206于佳心20315236贾瑗实验五
- LA 4329 树状数组入门
- 你的能力是更适合做微商还是淘宝
- lua学习06——lua中的面向对象
- android studio启动快速调试
- java数据结构--栈
- Qt窗口屏幕居中显示(有专门的QDesktopWidget,先计算后显示)
- popen函数相关
- 在ubuntu中安装eclipse for c++
- 应用AXIS开始Web服务之旅(soap web services)——使用三种不同的语言访问创建的Web服务,分别是JAVA、VB、VC
- hdu 1046 Gridland
- bzoj2005 能量采集 数论