程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> Remoting應用實例 + 代碼,remoting應用實例

Remoting應用實例 + 代碼,remoting應用實例

編輯:C#入門知識

Remoting應用實例 + 代碼,remoting應用實例


下面給出了Remoting的小實例,主要功能是將客戶端的數據寫入到服務端。

分析圖:

程序代碼為2個控制台應用程序(1個客戶端,1個服務器端)和1個類庫,如下所示。

客戶端代碼:

using RemotingObjects; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using System.Text; namespace RemotingClient { class Program { static void Main(string[] args) { TcpChannel channel = new TcpChannel(); ChannelServices.RegisterChannel(channel, false); WriteFileToLocal wfobj = (WriteFileToLocal)Activator.GetObject(typeof(RemotingObjects.Process), "tcp://localhost:8085/RemotingWriteFileToLocalService"); if (wfobj == null) { Console.WriteLine("Couldn't create Remoting Object 'WriteFileToLocal'."); } else { Console.WriteLine("Please enter content:"); String name = Console.ReadLine(); try { wfobj.write(name); } catch (System.Net.Sockets.SocketException e) { Console.WriteLine(e.ToString()); } } Console.Read(); } } } RemotingClient

服務端代碼:

using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using System.Text; namespace RemotingServer { class Program { static void Main(string[] args) { TcpChannel channel = new TcpChannel(8085); ChannelServices.RegisterChannel(channel, false); RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingObjects.Process), "RemotingWriteFileToLocalService", WellKnownObjectMode.SingleCall); Console.WriteLine("Server:Press Enter key to exit"); Console.ReadLine(); } } } RemotingServer

類庫代碼:

using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace RemotingObjects { public interface WriteFileToLocal { void write(string content); } public class Process : MarshalByRefObject, WriteFileToLocal { public Process() { Console.WriteLine("Write Starting..."); } /// <summary> /// 寫文件 /// </summary> /// <param name="content">寫入文件的內容</param> public void write(string content) { using (StreamWriter sw = new StreamWriter(@"D:\remoting.txt", true, Encoding.Default)) { sw.Write(content + "\r\n"); } } } } RemotingObjects

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved