登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Code@Pig Home

喜欢背着一袋Code傻笑的Pig .. 忧美.欢笑.记忆.忘却 .之. 角落

 
 
 

日志

 
 

[ice] hello world, again  

2010-05-29 09:32:54|  分类: net_Ice |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
在win32上,再此完了一下 hello world 程序。Ice 通过其的 idl (slice) 来定义 interface,并很容易的实现 remote call。

程序很简单,客户端调用 getName() 给服务端传过去一个int value,同时自己得到一个string。
---------------------- foo.ice -------------------------
module Demo {
    interface Foo {
        string getName(int value);
    };
};
---------------------- client.cpp -------------------------
#include <Ice/Ice.h>
#include <foo.h>

using namespace std;
using namespace Demo;

int main(int argc, char *argv[])
{
    int status = 0;
    Ice::CommunicatorPtr ic;
    try
    {
        ic = Ice::initialize(argc, argv);
        Ice::ObjectPrx base = ic->stringToProxy("SimpleFoo:default -h localhost -p 10000");
        FooPrx foo = FooPrx::checkedCast(base);
        if (!foo)
            throw "Invalid proxy";

        cout << "name: " << foo->getName(10) << endl;
    }
    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 std::string &msg)
        {
            cerr << msg << endl;
            status = 1;
        }
    }

    return status;
}
---------------------- server.cpp -------------------------
#include <Ice/Ice.h>
#include <foo.h>

using namespace std;
using namespace Demo;

class FooI : virtual public Foo
{
public:
    virtual std::string getName(Ice::Int value, const Ice::Current&);
};

std::string
FooI::getName(Ice::Int value, const Ice::Current&)
{
    cout << "value: " << value << endl;
    return "kasicass";
}

int main(int argc, char *argv[])
{
    int status = 0;
    Ice::CommunicatorPtr ic;
    try
    {
        ic = Ice::initialize(argc, argv);
        Ice::ObjectAdapterPtr adapter = ic->createObjectAdapterWithEndpoints(
           "SimpleFooAdapter", "default -h localhost -p 10000");
        Ice::ObjectPtr obj = new FooI;
        adapter->add(obj, ic->stringToIdentity("SimpleFoo"));
        adapter->activate();
        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 std::string &msg)
        {
            cerr << msg << endl;
            status = 1;
        }
    }

    return status;
}
------------------------ server/client run ----------------
D:\sandbox\ice\hello\debug>server.exe
value: 10

D:\sandbox\ice\hello\debug>client.exe
name: kasicass
D:\sandbox\ice\hello\debug>
--------------------------------------------------------------

本来是个很简的事情,但我悲剧地使用了vs2005 + debug mode,编译出来的东西直接crash了,提示 0xc0150002 错误。
后来发现是 vs2005 sp1 的问题,一定要用 sp1 且要用 release mode。release mode 编译,客户端一连接上来,继续crash。

无奈,安装了 vs2008,一切ok。然后 vs2005 再编译,kao,居然也好了。nndx


  评论这张
 
阅读(1222)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018