Remoting學習(一)—-用Reomoting 實現資訊發送功能

來源:互聯網
上載者:User

Remoting的優缺點?

優點:

1、能方便的構建大範圍分布式應用程式

2、Tcp通道的Remoting速度非常快

3、雖然是遠端的,但是非常接近於本地調用物件

4、可以做到保持物件的狀態

5、沒有應用程式限制,可以是控制臺,winform,iis,windows服務承載遠端物件

缺點:

1、非標準的應用因此有平臺限制

2、脫離iis的話需要有自己的安全機制

Remoting的開發三步曲

1、  創建遠程對象:此物件要繼承MarshalByRefObject

2、  創建一個“宿主”程式,接受客戶請求:註冊通道、註冊服務器啟動的遠端對象(Singleton,SingleCall)

3、  創建用戶端調用遠端對象:註冊通道,根據URL獲得代理,通過代理獲得對象

Remoting的開發實例(Reomoting 實現資訊發送功能)

      1﹑建立類別庫專案﹐RemoteObject,輸入以下代碼﹕

 public class RemoteObject:MarshalByRefObject 
    {
        public RemoteObject() { }
        public static void Bullet(String str)
        {
            MessageBox.Show(str, "Remoting Message", MessageBoxButtons.OK);
        }
    }

 只有直接或間接的繼承 MarshalByRefObject 的類才能被遠程啟用

            2﹑建立一WinForm服務器端程式專案﹐以接受用戶請求。

A﹑進行相關屬性設置﹐將Form1改為frmServer

B﹑點擊該專案新增一個應用程式組態檔App.Config.該檔案是一個標準的XML檔案格式﹐輸入以下代碼﹕

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.runtime.remoting>
        <application name ="RemoteServer">
            <service>
                <activated type="RemoteObject.RemoteObject,RemoteObject"/>
            </service>
            <channels>
                <channel ref="tcp server" port="8080"/>
            </channels>
        </application>
    </system.runtime.remoting>
</configuration>

              C﹑在frmServer中引用命名空間

using System.Runtime.Remoting;

            在建構方法中加入以下一句﹕

RemotingConfiguration.Configure("RemoteServer.exe.config", false);

        3﹑建立立一WinForm客戶端程式專案﹐能調用遠程對象

A﹑進行相關屬性設置﹐將Form1改為frmClient

B﹑在工具箱中拖一個label進來﹐將text屬性改為Enter Message,Name屬性設為lblMessage,拖一textbox,name設為tbxMessage,再拖buttons,設name為btnSend,text設為Send

C﹑點擊該專案新增一個應用程式組態檔App.Config.輸入以下代碼﹕

<?xml version="1.0" encoding="utf-8" ?>
<configuration>    
    <system.runtime.remoting>
        <application name="RemoteClient">
            <client url="tcp://localhost:8080/RemoteServer">
                <activated type="RemoteObject.RemoteObject,RemoteObject"/>
            </client>
            <channels>
                <channel ref="tcp client"/>
            </channels>
        </application>
    </system.runtime.remoting>
</configuration>

                D﹑雙擊窗體的空白部分﹐在Load中輸入代碼﹐btnSend上雙擊輪入代碼﹐整個代碼如下﹕

private void frmClient_Load(object sender, EventArgs e)
        {
            //Listen the Client request,config the client infomation
            RemotingConfiguration.Configure("RemoteClient.exe.config", false);
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            RemoteObject.RemoteObject.Bullet(this.tbxMessage.Text);
        }

 

        4﹑其實應該不能算一步了。如果這時你編譯以上的專案﹐在client端肯定會報錯﹐原因很簡單:缺少引用。沒錯﹐這一步就是在client端和server端加入遠程對象類的引用。

        5﹑編譯整個方案﹐先運行server端一個實例﹐再運行client端的實例﹐在窗體的textbox中輸入文字﹐就將彈出你輸入的文字的MessageBox啦。測試OK。

        6﹑本Remoting Source現提供下載﹗(注﹕本人也是初學Remoting,歡迎交流討論.QQ:52433739)﹐本文參考了LoveCherry的<<一步一步學Remoting>>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.