Chinaunix首页 | 论坛 | 博客
  • 博客访问: 972430
  • 博文数量: 108
  • 博客积分: 3243
  • 博客等级: 中校
  • 技术积分: 964
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-15 22:09
文章分类

全部博文(108)

文章存档

2020年(2)

2019年(1)

2018年(2)

2017年(9)

2016年(20)

2015年(1)

2013年(1)

2012年(12)

2011年(28)

2010年(27)

2009年(4)

2008年(1)

分类: C/C++

2011-08-02 11:26:55

在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)
{
}
阅读(9674) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~