国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

使用C#編寫Ice應用程序

2024-07-21 02:18:44
字體:
來源:轉載
供稿:網友

最大的網站源碼資源下載站,

ice是一種優秀的分布式網絡中間件,比起corba好上許多,也更簡潔。優點我在這里也不便多說了,有專文介紹,也不是今天的主題。有興趣的可以查一下,《程序員》雜志好像有專題。
下面主要介紹一下怎樣使用c#編寫基于ice的網絡應用程序。
環境:windows server 2003 enterprise, visual studio.net 2003(.net framework 1.1)
先到http://www.zeroc.com/download.html下載ice的安裝包,windows下用msi的。為方便起見,我下載的是vs.net2003的專用包。如下:


注意:ice-1.5.1-vc71.msi 安裝包是必需的。 安裝完畢后,將安裝目錄下的bin目錄加入環境變量的path路徑,然后就可以在vs.net中開發ice應用了。

首先,我們編寫一個slice定義文件(相當于corba里面的idl文件)。文件內容很簡單,因為我們要從一個“hello world”程序開始。
命名為printer.ice:
interface printer
{
void printstring(string s);
};
用下面的命令編譯:slice2cs.exe printer.ice(如果找不到命令,表示環境變量沒有設置成功,可以使用bin目錄的全路徑)
這條命令會在當前目錄下產生printer.cs文件:


恭喜你,初戰告捷!繼續加油!
我用vs.net建了一個空的解決方案icetest,然后添加了一個空的項目icetest,加入printer.cs文件,最后建立server.cs文件(不用說你也猜到了,現在是編寫服務端)。文件目錄如下:



在server.cs中添加如下代碼:

using system;

namespace icetest
{
/**//// <summary>
/// summary description for server.
/// </summary>
public class server
{
public server()
{
//
// todo: add constructor logic here
//
}

public static void main(string[] args)
{
int status = 0;
ice.communicator ic = null;
try
{
ic = ice.util.initialize(ref args);
ice.objectadapter adapter
= ic.createobjectadapterwithendpoints(
"simpleprinteradapter", "default -p 10000");
ice.object obj = new printeri();
adapter.add(
obj,
ice.util.stringtoidentity("simpleprinter"));
adapter.activate();
ic.waitforshutdown();
}
catch (exception e)
{
console.error.writeline(e);
status = 1;
}
finally
{
if (ic != null)
ic.destroy();
}
environment.exit(status);
}
}

public class printeri : _printerdisp
{
public override void printstring(string s, ice.current current)
{
console.writeline(s);
}
}
}
前面的代碼都是例行公事,public class printeri : _printerdisp的代碼才是我們需要的(簡單吧!)
按照同樣的方法,我們建立iceclienttest項目,先添加printer.cs文件,然后編寫cient.cs文件,具體內容如下:

using system;

namespace iceclienttest
{
/**//// <summary>
/// summary description for client.
/// </summary>
public class client
{
public client()
{
//
// todo: add constructor logic here
//
}

public static void main(string[] args)
{
int status = 0;
ice.communicator ic = null;
try
{
ic = ice.util.initialize(ref args);
ice.objectprx obj = ic.stringtoproxy(
"simpleprinter:default -p 10000");
printerprx printer
= printerprxhelper.checkedcast(obj);
if (printer == null)
throw new applicationexception("invalid proxy");
printer.printstring("hello world!");
}
catch (exception e)
{
console.error.writeline(e);
status = 1;
}
finally
{
if (ic != null)
ic.destroy();
}
environment.exit(status);
}
}
}


同樣,里面大部分都是例行公事,只有printer.printstring("hello world!");一句才是關鍵。
現在我們可以build解決方案了,成功后便產生了icetest.exe 和iceclienttest.exe 文件。如果沒有成功,記得在項目中添加引用,加入icecs.dll(也在安裝的bin目錄下)。
應該可以了,下面看看運行結果:


運行結果很簡單,就是服務端輸出客戶端輸入的“hello world”字符串。比corba簡單吧?
最后貼出服務端的uml圖,客戶端的類似就不貼了。


以上是我參考《distributed programming with ice》(官方網站可以下載,是ice軟件包的官方文檔)一書做的練習,內容很簡單,請大家指教!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 华安县| 宣汉县| 西吉县| 安义县| 吉木萨尔县| 花莲市| 简阳市| 灵宝市| 安多县| 满城县| 铜山县| 文登市| 广东省| 岳阳市| 莒南县| 雅江县| 竹北市| 会昌县| 格尔木市| 隆回县| 宁乡县| 山阳县| 方正县| 廉江市| 赫章县| 肇州县| 远安县| 余江县| 广东省| 九龙县| 古丈县| 阳东县| 平阳县| 海林市| 衡水市| 喀什市| 汉源县| 迭部县| 平陆县| 醴陵市| 绵竹市|