Remoting实例(同为客户端服务端)
生活随笔
收集整理的這篇文章主要介紹了
Remoting实例(同为客户端服务端)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace test
{
??
????????? class LibraryClass : MarshalByRefObject
??????? {
??????????? public LibraryClass()
??????????????? : base()
??????????? {
??????????????? Console.WriteLine("遠程對象被創建!");
??????????? } ~LibraryClass()
??????????? {
??????????????? Console.WriteLine("遠程對象被析構!");
??????????? } public? void SayHello(string name)
??????????? {
??????????????? Console.WriteLine("你好,{0}", name);
??????????? } public int GetSomthing(string s)
??????????? {
??????????????? if (s != null)
??????????????? {
??????????????????? Console.WriteLine("我執行了!");
??????????????????? return s.Length;
??????????????? }
??????????????? return -1;
??????????? } }
???
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime .Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
namespace test
{
??? class RemotingServer
??? {
??????????? /// <summary>
??????????? /// 服務端應用
??????????? /// </summary>
??????????? /// <param name="args"></param>
??????? public static void Entry()
??????? {
??????????? //利用TCP通道,并偵聽12345端口
??????????? TcpServerChannel channel = new TcpServerChannel(10007);
??????????? ChannelServices.RegisterChannel(channel, true);
??????????? //使用WellKnown激活方式,并且使用SingleCall模式 //“LibraryClass”可替換為??"li"????????????????????? ??? LibraryClass li=new LibraryClass();
??????????? RemotingConfiguration.RegisterWellKnownServiceType(typeof(LibraryClass ), "LibraryClass", WellKnownObjectMode.SingleCall);//"LibraryClass"可替換為實例對象名
???????????
??????? }
???????
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels; namespace test
{
??? class RemotingClient
??? { public static? void? main()
?????? {
??????????? try
??????????? {
??????????????? //使用TCP通道連接
??????????????? TcpClientChannel channel2 = new TcpClientChannel("channel2", new BinaryClientFormatterSinkProvider());
??????????????? ChannelServices.RegisterChannel(channel2, true);
??????????????? //獲取遠程對象
?????????????? LibraryClass class1 = (LibraryClass)Activator.GetObject(typeof(LibraryClass ), "tcp://localhost:10007/LibraryClass");//LibraryClass路徑可替換為實例對象名li
??????????????? //調用遠程對象的方法
??????????????? class1.SayHello("DebugLZQ");
??????????????? class1.SayHello("http://www.cnblogs.com/DebugLZQ"); int i = class1.GetSomthing("DebugLZQ");
??????????????? Console.WriteLine("輸入的長度是,{0}", i);
??????????? }
??????????? catch (Exception ex)
??????????? {
??????????????? Console.WriteLine(ex.Message);
??????????? }
??????????? Console.Read();
???????
???????
???????
??????? }
??? }
}
using System.Text;
using System.Xml;
using System.Windows.Forms;
using Atspace.DataInjection.AddFunctionLibrary;
using System.IO;
namespace test
{
??? class Program
??? { static void Main(string[] args)
??????? { RemotingServer.Entry();
??????????? RemotingClient.main();
??????????? Console.ReadKey();
??????????
??????????
?????????? }
???????
??????? }
???
}
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace test
{
??
????????? class LibraryClass : MarshalByRefObject
??????? {
??????????? public LibraryClass()
??????????????? : base()
??????????? {
??????????????? Console.WriteLine("遠程對象被創建!");
??????????? } ~LibraryClass()
??????????? {
??????????????? Console.WriteLine("遠程對象被析構!");
??????????? } public? void SayHello(string name)
??????????? {
??????????????? Console.WriteLine("你好,{0}", name);
??????????? } public int GetSomthing(string s)
??????????? {
??????????????? if (s != null)
??????????????? {
??????????????????? Console.WriteLine("我執行了!");
??????????????????? return s.Length;
??????????????? }
??????????????? return -1;
??????????? } }
???
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime .Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
namespace test
{
??? class RemotingServer
??? {
??????????? /// <summary>
??????????? /// 服務端應用
??????????? /// </summary>
??????????? /// <param name="args"></param>
??????? public static void Entry()
??????? {
??????????? //利用TCP通道,并偵聽12345端口
??????????? TcpServerChannel channel = new TcpServerChannel(10007);
??????????? ChannelServices.RegisterChannel(channel, true);
??????????? //使用WellKnown激活方式,并且使用SingleCall模式 //“LibraryClass”可替換為??"li"????????????????????? ??? LibraryClass li=new LibraryClass();
??????????? RemotingConfiguration.RegisterWellKnownServiceType(typeof(LibraryClass ), "LibraryClass", WellKnownObjectMode.SingleCall);//"LibraryClass"可替換為實例對象名
???????????
??????? }
???????
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels; namespace test
{
??? class RemotingClient
??? { public static? void? main()
?????? {
??????????? try
??????????? {
??????????????? //使用TCP通道連接
??????????????? TcpClientChannel channel2 = new TcpClientChannel("channel2", new BinaryClientFormatterSinkProvider());
??????????????? ChannelServices.RegisterChannel(channel2, true);
??????????????? //獲取遠程對象
?????????????? LibraryClass class1 = (LibraryClass)Activator.GetObject(typeof(LibraryClass ), "tcp://localhost:10007/LibraryClass");//LibraryClass路徑可替換為實例對象名li
??????????????? //調用遠程對象的方法
??????????????? class1.SayHello("DebugLZQ");
??????????????? class1.SayHello("http://www.cnblogs.com/DebugLZQ"); int i = class1.GetSomthing("DebugLZQ");
??????????????? Console.WriteLine("輸入的長度是,{0}", i);
??????????? }
??????????? catch (Exception ex)
??????????? {
??????????????? Console.WriteLine(ex.Message);
??????????? }
??????????? Console.Read();
???????
???????
???????
??????? }
??? }
}
using System.Text;
using System.Xml;
using System.Windows.Forms;
using Atspace.DataInjection.AddFunctionLibrary;
using System.IO;
namespace test
{
??? class Program
??? { static void Main(string[] args)
??????? { RemotingServer.Entry();
??????????? RemotingClient.main();
??????????? Console.ReadKey();
??????????
??????????
?????????? }
???????
??????? }
???
}
總結
以上是生活随笔為你收集整理的Remoting实例(同为客户端服务端)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .net remoting 与webse
- 下一篇: C# 制作安装包