1. 程式人生 > >ice外掛以及使用(VS2015)

ice外掛以及使用(VS2015)

ice操作

軟體安裝包下載

ice操作

1、新建ice檔案(以.ice為副檔名)

//Printer.ice
module MyPrinter
{
    interface Printer
	{
        void printString(string s);
    };
};

2、生成Printer.h和Printer.cpp(命令列操作,暫時沒配置好自動化版本) slice2cpp Printer.ice

3、將檔案新增到專案裡

3.1、在server原始檔中新建server.cpp。

//server.cpp
#include <Ice/Ice.h>
#include "Printer.h"
#include <iostream>

using namespace std;
using namespace MyPrinter;

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;
    try
    {
        ic = Ice::initialize(argc, argv);
        Ice::ObjectAdapterPtr adapter
            = ic->createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -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;
}


3.2、客戶端 client.cpp檔案。

//client.cpp
#include<Ice/Ice.h>
#include "Printer.h"
#include <iostream>
#include <string>

using namespace std;
using namespace MyPrinter;

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 -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)
        ic->destroy();
    return status;
    system("pause");
}

PS:先執行server,再執行client。得到Hello world!