using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test
{
class LibraryClass : MarshalByRefObject
{
public LibraryClass()
: base()
{
Console.WriteLine("远程对象被创建!");
}
{
class LibraryClass : MarshalByRefObject
{
public LibraryClass()
: base()
{
Console.WriteLine("远程对象被创建!");
}
~LibraryClass()
{
Console.WriteLine("远程对象被析构!");
}
{
Console.WriteLine("远程对象被析构!");
}
public void SayHello(string name)
{
Console.WriteLine("你好,{0}", name);
}
{
Console.WriteLine("你好,{0}", name);
}
public int GetSomthing(string s)
{
if (s != null)
{
Console.WriteLine("我执行了!");
return s.Length;
}
return -1;
}
{
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;
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"可替换为实例对象名
}
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;
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
{
{
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");
{
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();
}
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
{
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();
}
}
}
RemotingClient.main();
Console.ReadKey();
}
}
}