Qt中应用ACE其实也是好简单的,下面就做一个简单的创建Tcp服务器的例子:
main.cpp函数如下:
#include "qaceapplication.h"
#include "mainwindow.h"
#include "ace/OS_main.h"
int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
QACEApplication app(argc, argv);
MainWindow w;
app.connect(&w, SIGNAL(closeWindow()), &app, SLOT(closeACE()));
w.show();
return app.exec();
}
main入口用ACE_TMAIN代替,这样ACE就可以初始化好,用就方便了。
QApplication也重载,这样好执行要的exec和exit动作,代码如下:
QACEApplication.h
#ifndef QACEAPPLICATION_H
#define QACEAPPLICATION_H
#include <QObject>
#include <QtWidgets/QApplication>
#include <QDebug>
class QACEApplication : public QApplication
{
Q_OBJECT
public:
typedef QApplication inherited;
public:
QACEApplication( int argc, char *argv[] );
virtual int exec();
public slots:
void closeACE();
};
#endif // QACEAPPLICATION_H
QACEApplication.cpp
#include "qaceapplication.h"
#include "ace/OS_NS_time.h"
QACEApplication::QACEApplication(int argc, char *argv[]):
QApplication (argc, argv, true) /* do not enable GUI */
{
}
int QACEApplication::exec ()
{
qDebug() << "QACEApplication exec";
return inherited::exec ();
}
void QACEApplication::closeACE()
{
qDebug() << "ACE_OS exit";
ACE_OS::exit ();
}
界面就一个MainWindow,代码如下:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void openSocket();
signals:
void closeWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ace/FILE_Addr.h"
#include "ace/FILE_Connector.h"
#include "ace/FILE_IO.h"
#include "ace/INET_Addr.h"
#include "ace/SOCK_Stream.h"
#include "ace/FILE_IO.h"
#include "ace/SOCK_Acceptor.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_stdlib.h"
#include "ace/os_include/os_netdb.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked()), this,SLOT(openSocket()));
}
MainWindow::~MainWindow()
{
emit closeWindow();
delete ui;
}
void MainWindow::openSocket()
{
qDebug() << "open socket";
ACE_INET_Addr server_addr(1234,ACE_TEXT ("127.0.0.1"),ACE_PROTOCOL_FAMILY_INET);
ACE_SOCK_ACCEPTOR acceptor_;
int rst;
rst = acceptor_.open(server_addr);
qDebug() << "rst: " << rst;
}
运行结果:
?
多谢的美美。
|