惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
N
News | PayPal Newsroom
S
Security @ Cisco Blogs
Schneier on Security
Schneier on Security
V
V2EX - 技术
S
Secure Thoughts
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
S
Securelist
S
Security Archives - TechRepublic
Know Your Adversary
Know Your Adversary
V
Vulnerabilities – Threatpost
Security Latest
Security Latest
Recent Commits to openclaw:main
Recent Commits to openclaw:main
G
GRAHAM CLULEY
H
Hacker News: Front Page
Microsoft Azure Blog
Microsoft Azure Blog
I
Intezer
Google Online Security Blog
Google Online Security Blog
美团技术团队
阮一峰的网络日志
阮一峰的网络日志
T
The Exploit Database - CXSecurity.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Webroot Blog
Webroot Blog
Jina AI
Jina AI
Engineering at Meta
Engineering at Meta
P
Proofpoint News Feed
The Cloudflare Blog
I
InfoQ
L
LangChain Blog
U
Unit 42
P
Proofpoint News Feed
S
Schneier on Security
S
Security Affairs
Y
Y Combinator Blog
T
Tenable Blog
N
News and Events Feed by Topic
MyScale Blog
MyScale Blog
量子位
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
博客园 - 聂微东
D
Darknet – Hacking Tools, Hacker News & Cyber Security
GbyAI
GbyAI
AWS News Blog
AWS News Blog

博客园 - liuym

TB 编程整理 三 ICE开发初级研究 一 VC2008环境中ICE的配置 无法定位序数 3109 于LIBEAY32.dll - liuym 转:Virtual List的使用方法 转:VC++编程之CListCtrl控件的使用 2 转:VC++编程之CListCtrl控件的使用 好用 转:删除,修改注册表中需要设置权限的项 转:软件能够修复硬盘吗?―硬盘损坏全分析 转:CWnd 对象怎么和 HWND 窗口句柄相互转化 VC 多文档窗口 子窗口最大化时切换窗口 窗口没有最大化显示的问题 转:Windows VC6编译安装Boost库 转:送给那些一心想要传送文件的朋友(TCP协议).cpp-from CSDN 转:如何去了解、熟悉一个已经开发完的项目 进行维护、二次开发或者升级 递归创建文件夹 DLL中资源和主程序资源冲突 - liuym - 博客园 转:一种巧妙的删除程序自己的方法 转:双缓冲图形刷新技术 和 WGF双缓冲绘图框架 软件保护建议(转)
二 示例程序一 - liuym - 博客园
liuym · 2010-12-17 · via 博客园 - liuym

示例程序一

1)

ICE文件 Printer.ice

module demo

{

    interface Printer

    {

        void printString(string s);

    };

};

2)

Server端开发

选择Win32控制台空白项目 添加Iced.lib IceUtild.lib

增加Server.cpp,内容:

#include "Printer.h"

#include <Ice/Application.h>

#include <Ice/Ice.h>

using namespace std;

using namespace demo;

class PrinterI : public Printer 

{

    public:

    // 重载实现接口逻辑

    virtual void printString(const string& s,const Ice::Current&);

};

void PrinterI::printString(const string& s, const Ice::Current&)

{

    cout << s << endl;

}

int  main(int argc, char* argv[])

{

    int status = 0;

    Ice::CommunicatorPtr ic;    // ICE运行环境句柄

    try 

    {

        // 调用Ice::initialize,初始化Ice run time

// initialize 调用返回的是一个智能指针,指向一个Ice::Communicator对象,这个指针是Ice run time 的主句柄

                ic = Ice::initialize(argc, argv);

// 调用Communicator 实例上的createObjectAdapterWithEndpoints,创建一个对象适配器。

// 是"SimplePrinterAdapter" (适配器的名字)

// "default -p 10000",后者是要适配器用缺省协议(TCP/IP)在端10000 处侦听到来的请求。

                Ice::ObjectAdapterPtr adapter= ic->createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -p 10000");

// 我们实例化一个PrinterI 对象,为我们的Printer 接口创建一个servant。

                Ice::ObjectPtr object = new PrinterI;

// 调用适配器的add,告诉它有了一个新的servant ;传给add 的参数是我们刚才实例化的servant,再加上一个标识符。

// 在这里,"SimplePrinter" 串是servant 的名字

                adapter->add(object,ic->stringToIdentity("SimplePrinter"));

                // 如果您按网上盛传的文章写成adapter->add(object,ICE::stringToIdentity("SimplePrinter"));您就死翘翘了

                // 就这个问题缠了我3天,后来发现官方文档是ic->stringToIdentity("SimplePrinter"),总算解围了,否则还得死绕

// 调用适配器的activate 方法激活适配器

// (适配器一开始是在扣留(holding)状态创建的;这种做法在下面这样的情况下很有用:

// 我们有多个servant,它们共享同一个适配器,而在所有servant实例化之前我们不想处理请求)。

// 一旦适配器被激活,服务器就会开始处理来自客户的请求。

                adapter->activate();

// 调用waitForShutdown。这个方法挂起发出调用的线程,直到服务器实现终止为止

                ic->waitForShutdown();

    } 

catch (const Ice::Exception& e) 

    {

        cerr << e << endl;

        status = 1;

    } 

    catch (const char* msg) 

    {

        cerr << msg << endl;

        status = 1;

    }

    if (ic) 

    {

        try 

        {

            ic->destroy();

        } 

        catch (const Ice::Exception& e) 

        {

            cerr << e << endl;

            status = 1;

        }

    }

    return status;

}

3)

Client端开发

选择Win32控制台空白项目 添加Iced.lib IceUtild.lib

增加Client.cpp,内容:

#include <Ice/Ice.h>

#include "Printer.h"

using namespace std;

using namespace demo;

int main(int argc, char * argv[])

{

    int status = 0;

    Ice::CommunicatorPtr ic;

    try 

    {

        // 调用Ice::initialize 初始化Ice runtime。

                ic = Ice::initialize(argc, argv);

// 调用通信器的stringToProxy创建一个代理,所用参数是"SimplePrinter:default -p 10000"。

// 注意,这个串包含的是对象标识和服务器所用的端口号

                Ice::ObjectPrx base = ic->stringToProxy("SimplePrinter:default -p 10000");

// stringToProxy 返回的代理的类型是Ice::ObjectPrx,这种类型位于接口和类的继承树的根部。

// 但要实际与我们的打印机交谈,我们需要的是Printer 接口、而不是Object 接口的代理。

// 为此,我们需要调用PrinterPrx::checkedCast 进行向下转换。这个方法会发送一条消息给服务器,

// 实际询问“这是Printer 接口的代理吗?”如果是,这个调用就会返回Printer 的一个代理;

// 如果代理代表的是其他类型的接口,这个调用就会返回一个空代理。

                PrinterPrx printer = PrinterPrx::checkedCast(base);

// 测试向下转换是否成功,如果不成功,就抛出出错消息,终止客户。

                if (!printer) throw "Invalid proxy";

// 调用代理的printString 方法,把"Hello World!" 串传给它。

// 服务器会在它的终端上打印这个串。

                printer->printString("Hello World!");

    } 

catch (const Ice::Exception & ex) 

    {

        cerr << ex << endl;

        status = 1;

    } 

catch (const char * msg) 

    {

        cerr << msg << endl;

        status = 1;

    }

    if (ic) 

    {

        try 

        {

            ic->destroy();

        } 

        catch (const Ice::Exception& e) 

        {

            cerr << e << endl;

            status = 1;

        }

    }

    return status;

}