最近在因為在學習remoting,純粹只是了解一下,發現remoting確實是好東西。
我們通常有三種方式來使用remoting,一種是
第一種:publishing a public object
公開的對象創建在本地
第二種:remote creation of a public object (sao)
對象創建在客戶端請求中
第三種:remote creation of a private object (cao)
對象創建在host上,客戶端引用服務器上的對象
目次我也沒有很好理解這三種的本質區別在哪里。而這三種方式的remoting創建方式也不相同。
第一種方式
host:
channelservices.registerchannel (new tcpchannel(1500));
ctransfer trans = new ctransfer();
remotingservices.marshal (trans, "testservice");client:
ctransfer t = (ctransfer) activator.getobject(typeof(ctransfer),
"tcp://host:1500/testservice");
第二種方式
host:
channelservices.registerchannel (new tcpchannel(1500));
remotingconfiguration.registerwellknownservicetype(typeof(ctransfer),
"testservice", wellknownobjectmode.singleton);client:
ctransfer t = (ctransfer) activator.getobject(typeof(ctransfer),
"tcp://host:1500/testservice");
第三種方式
host:
channelservices.registerchannel (new tcpchannel(1500));
remotingconfiguration.registeractivatedservicetype(typeof(ctransfer));client:
object[] attr = {new urlattribute("tcp://host:1500")};
object[] args = {"sample constructor argument"};
ctransfer t = (ctransfer) activator.createinstance(typeof(ctransfer), args, attr);
如果我們需要一個對象(object)允許遠程調用處理,那么這個對象(object)需要繼承于marshalbyrefobject這個類。
如何在remoting中傳送文件呢?基本思路就是在client打開client的文件,轉換在byte[]類型之后調用host的對象。
client與host之間傳送的對象
[serializable]
public struct kaction
{
public string filename;
public byte[] context;
};打開文件,將流字節保存到context中去
stream filestream=file.open(this.transfilename.text,filemode.open);
filestream.position=0;
byte[] content = new byte[((int) filestream.length) + 1];
filestream.read(content,0,content.length) ;
在host在讀取到kaction之后,把它保存到指定文件夾下面
memorystream meoerystream=new memorystream(k_action.context);
filestream filestream=new filestream(@"d:/"+k_action.filename,filemode.create);
meoerystream.writeto(filestream);
filestream.close();
meoerystream.close();
發現不能在對象中又定義新的對象。在準備發送到host上會提示“包含潛在危險的類型”。
[serializable]
public struct kaction
{
public string filename;
public byte[] context;
public fineinfo fileinfo;//這里
};
記錄一下自己的心得。有空我會好好整理下下回做篇完整點的。
cnzc's blogs
新聞熱點
疑難解答
圖片精選