ZeroC—ICE中间件初步Demo

如今官方的版本已经到了3.5

我的编译环境是VS2012,并且已经安装好了IceVisualStudioAddin-3.5.0.1

注:这里我直接用的官网下载的demo,自己写的demo挂掉了,不知道为什么

首先是编写Slice定义,编写Slice就是要包含应用的接口(只是接口,实现在外部),这里就按照手册写个打印的服务。

Printer.ice:

// **********************************************************************
//
// Copyright (c) 2003-2013 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#pragma once

module Demo {
    interface Printer {
        void printString(string s);
    };
};

 

 

之后使用如下命令生成 .cpp和 .h文件

slice2cpp Printer.ice

 

接下来就是编写Server和Client端 

Server.cpp:

 

// **********************************************************************
//
// Copyright (c) 2003-2013 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <Ice/Ice.h>
#include <Printer.h>

using namespace std;
using namespace Demo;

class PrinterI : public Printer {
public:
	virtual void printString(const string &, 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;
	try {
		ic = Ice::initialize(argc, argv);
		Ice::ObjectAdapterPtr adapter =
			ic->createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -h localhost -p 10000");
		Ice::ObjectPtr object = new PrinterI;
		adapter->add(object, ic->stringToIdentity("SimplePrinter"));
		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 Ice::Exception& e) {
			cerr << e << endl;
			status = 1;
		}
	}
	return status;
}


 

 

Client.cpp:

// **********************************************************************
//
// Copyright (c) 2003-2013 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#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 {
        ic = Ice::initialize(argc, argv);
        Ice::ObjectPrx base = ic->stringToProxy("SimplePrinter:default -h localhost -p 10000");
        PrinterPrx printer = PrinterPrx::checkedCast(base);
        if(!printer) {
            throw "Invalid proxy";
        }
        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& ex) {
            cerr << ex << endl;
            status = 1;
        }
    }
    return status;
}


 

 

 

你可能感兴趣的:(ZeroC—ICE中间件初步Demo)