使用 Visual C# .NET 按值將對象封送到遠程服務器

建立C#類庫項目DLL

實現代碼:

using System;

namespace DLL
{
 /// <summary>
 /// test 的摘要說明。
 /// </summary>
 [System.Serializable]
 public class test
 {
  public test()
  {
   //
   // TODO: 在此處添加構造函數邏輯
   //
  }

  public string Time
  {
   get
   {
    return DateTime.Now.ToString();
   }
  }
 }
}

using System;
using System.IO;

namespace DLL
{
 /// <summary>
 /// api 的摘要說明。
 /// </summary>
 public class api : System.MarshalByRefObject
 {
  public api()
  {
   //
   // TODO: 在此處添加構造函數邏輯
   //
  }

  public string GetTime()
  {
   return new test().Time;
  }

  public string[] GetDirectories(string path)
  {
   return Directory.GetDirectories(path);
  }

  public string[] GetFiles(string path)
  {
   return Directory.GetFiles(path);
  }
 }
}
添加C#windows應用程序項目server

實現代碼:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;


using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

using DLL;

namespace Server
{
 /// <summary>
 /// Form1 的摘要說明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  /// <summary>
  /// 必需的設計器變量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗體設計器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
   //
  }

  /// <summary>
  /// 清理所有正在使用的資源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗體設計器生成的代碼
  /// <summary>
  /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  /// </summary>
  private void InitializeComponent()
  {
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(208, 150);
   this.Name = "Form1";
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);

  }
  #endregion

  /// <summary>
  /// 應用程序的主入口點。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
   TcpChannel chan = new TcpChannel(7777);
   ChannelServices.RegisterChannel(chan);
   RemotingConfiguration.RegisterWellKnownServiceType(
    Type.GetType("DLL.api, Dll"),
    "test",
    WellKnownObjectMode.SingleCall);

  }
 }
}
添加c#windows應用程序項目client

global.cs代碼:

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

using DLL;
namespace Client
{
 /// <summary>
 /// Global 的摘要說明。
 /// </summary>
 public class Global
 {
  public Global()
  {
   //
   // TODO: 在此處添加構造函數邏輯
   //
  }
  public static DLL.api api
  {
   get
   {
    return (DLL.api)Activator.GetObject(typeof(DLL.api), "tcp://"+ Config.Host + ":" + Config.Port + "/test");
   }
  }
 }
}

主窗體關鍵代碼:

  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
   ChannelServices.RegisterChannel(new TcpChannel());

  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
   string[] files = Global.api.GetFiles(@"c:/");//取得服務器C盤文件
  }
參考文獻:http://support.microsoft.com/default.aspx?scid=kb;zh-cn;307546

發佈了80 篇原創文章 · 獲贊 0 · 訪問量 31萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章