最简单的c#Remoting编程
生活随笔
收集整理的這篇文章主要介紹了
最简单的c#Remoting编程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.建立一個類庫項目:P1
a.類繼承于System.MarshalByRefObject
b.定義要公有訪問的類A1
2.建立一個服務器項目
a.增加System.Runtime.Remototing和類庫項目的引用
b.增加引用:
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using P1;
c.啟動服務
TcpServerChannel channel=new TcpServerChannel(8086);
ChannelServices.RegisterChannel(channel);//注冊服務器通道
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello),"Hi",WellKnownObjectMode.SingleCall);//注冊遠程服務對象類型
System.Console.WriteLine("hit to exit");
System.Console.ReadLine(); //等待
3.建立一個客戶端項目
a.增加System.Runtime.Remototing和類庫項目的引用
b.增加引用:
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using P1;
c.調用服務
ChannelServices.RegisterChannel(new TcpClientChannel());//注冊客戶通道
Hello obj=(Hello)Activator.GetObject(typeof(Hello),"tcp://localhost:8086/Hi");//使用透明代理與服務器通信
if(obj==null)//檢查通信是否成功
{
?Console.WriteLine("Can not locate server!");
?return;
}
for(int i=0;i<5;i++)//調用服務器上提供的函數
{
Console.WriteLine(obj.Greeting("glf"));
}
Console.WriteLine("hit to exit");
Console.ReadLine();
a.類繼承于System.MarshalByRefObject
b.定義要公有訪問的類A1
2.建立一個服務器項目
a.增加System.Runtime.Remototing和類庫項目的引用
b.增加引用:
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using P1;
c.啟動服務
TcpServerChannel channel=new TcpServerChannel(8086);
ChannelServices.RegisterChannel(channel);//注冊服務器通道
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello),"Hi",WellKnownObjectMode.SingleCall);//注冊遠程服務對象類型
System.Console.WriteLine("hit to exit");
System.Console.ReadLine(); //等待
3.建立一個客戶端項目
a.增加System.Runtime.Remototing和類庫項目的引用
b.增加引用:
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using P1;
c.調用服務
ChannelServices.RegisterChannel(new TcpClientChannel());//注冊客戶通道
Hello obj=(Hello)Activator.GetObject(typeof(Hello),"tcp://localhost:8086/Hi");//使用透明代理與服務器通信
if(obj==null)//檢查通信是否成功
{
?Console.WriteLine("Can not locate server!");
?return;
}
for(int i=0;i<5;i++)//調用服務器上提供的函數
{
Console.WriteLine(obj.Greeting("glf"));
}
Console.WriteLine("hit to exit");
Console.ReadLine();
總結
以上是生活随笔為你收集整理的最简单的c#Remoting编程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WCF中的web.config配置文件
- 下一篇: c# char unsigned_dll