一个关于Remoting的简单例子
前言
今天在整理以前寫的一些demo,看到一個關于remoting的例子。好久不用remoting了,果斷記錄一下。
什么是Remoting
簡單回顧下:
1.Remoting是一種遠程通信,或者說跨應用程序(域)通信的技術,在C/S架構的程序中應用較多。
2.支持協議:TCP和HTTP。
3.激活方式:服務器端激活(WellKnow)和客戶端激活。其中服務端激活又包含了SingleTon模式和SingleCall模式
HelloRemoting示例
一個完整的遠程通信程序基本上包括以下幾部分:
1.定義遠程對象。Remoting傳遞的對象是以引用的方式,因此所傳遞的遠程對象類必須繼承MarshalByRefObject;
2.服務端,主要是注冊通道、注冊遠程對象、注銷通道;
3.客戶端,主要完成注冊通道、獲取遠程對象。
第一步:
創建一個solution(HelloRemoting.sln)。然后按照上面描述的分別建立三個project:RemoteClient,RemoteObject,RemoteServer,如下圖所示:
說明:我這里的使用的VS2008創建的,其中Client和Server是Winform程序,RemoteObject是類庫。
第二步:
定義遠程對象。Remoting的遠程對象必須繼承自:MarshalByRefObject。我們這里創建一個HelloRemote類,包含有求和運算。
View Source /// <summary>/// 建立遠程調用對象/// </summary>public class HelloRemote:MarshalByRefObject{public HelloRemote(){//構造函數}~HelloRemote(){ //析構函數}/// <summary>/// 求兩數和/// </summary>/// <param name="a"></param>/// <param name="b"></param>/// <returns>和</returns>public int Sum(int a, int b){return a + b;}}第三步:
創建服務,注冊通道、遠程對象。本示例使用服務端激活,TCP通道注冊遠程對象。具體如下:添加一個Form窗體,簡單設置UI如下:
分別添加啟動服務和關閉服務的按鈕點擊事件。代碼如下(代碼中已經有詳細注釋):我們把服務注冊到10086端口上
View Source/// <summary>/// 啟動服務/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnStartUp_Click(object sender, EventArgs e){try{//創建一個TCP通道TcpServerChannel channel = new TcpServerChannel(10086);//注冊通道ChannelServices.RegisterChannel(channel);//使用WellKnown激活方式中的SingleCall模式注冊遠程對象RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject.HelloRemote), "HelloRemote", WellKnownObjectMode.SingleCall);MessageBox.Show("啟動成功", "提示");lblMsg.Text = "(服務已啟動)";this.btnStartUp.Visible = false;//啟動服務后隱藏啟動按鈕btnClose.Visible = true;//顯示關閉服務按鈕}catch (Exception ex){MessageBox.Show(ex.ToString(), "提示");}}/// <summary>/// 關閉服務/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnClose_Click(object sender, EventArgs e){//獲取當前已注冊的通道IChannel[] registeredChannels = ChannelServices.RegisteredChannels;if (registeredChannels == null || registeredChannels.Length <= 0){MessageBox.Show("沒有注冊任何通道!", "提示");return;}foreach (IChannel channel in registeredChannels){//循環已經注冊的通道if (channel.ChannelName.ToLower() == "tcp"){TcpServerChannel tcpchannel = (TcpServerChannel)channel;tcpchannel.StopListening(null);//關閉監聽ChannelServices.UnregisterChannel(tcpchannel);//注銷通道MessageBox.Show("服務關閉成功!", "提示");btnClose.Visible = false;btnStartUp.Visible = true;}}}?第四步:
創建客戶端(Client),獲取遠程對象。同Server一樣,在RemoteClient中添加一個Form如下:
在【點擊計算】按鈕上添加點擊事件,完成注冊通道,獲取遠程對象,以及調用遠程對象計算并返回結果。代碼如下:
View Source/// <summary>/// 計算兩數的和/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnSumbit_Click(object sender, EventArgs e){//獲取遠程對象其實是創建一個遠程對象在客戶端的代理HelloRemote obj = (HelloRemote)Activator.GetObject(typeof(HelloRemote), "tcp://localhost:10086/HelloRemote");if (obj == null){Console.WriteLine("Could not local Server");return;}try{//獲取需要就算得兩個數并轉化為int型int firNum = Convert.ToInt32(txtFirst.Text.Trim());int secNum = Convert.ToInt32(txtSecond.Text.Trim());//調用遠程對象int sum = obj.Sum(firNum, secNum);txtResult.Text = sum.ToString();}catch (Exception ex){MessageBox.Show(ex.ToString(), "提示");}}?
通過上面的幾步我們就創建了一個遠程通信的示例,我們看一下運行結果,分別啟動Server和Client,并輸入10,20點擊提交:
服務端:
客戶端:
結果為30,說明我們成功的創建了一個使用Remoting的分布式應用程序。
源碼如下:HelloRemoting.rar
轉載于:https://www.cnblogs.com/pszw/archive/2012/05/10/2495102.html
總結
以上是生活随笔為你收集整理的一个关于Remoting的简单例子的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 无法访问工作组计算机修复工具,局域网共享
- 下一篇: 向量点积(Dot Product)