在这一节里我们建立了一个叫General的类库,在他里面主要是作为服务器端的一个函数:
他的完整代码如下:
using System;
using System.Collections.Generic;
using System.Text;
namespace General
{
public class HelloServer : MarshalByRefObject
{
public HelloServer()
{
}
public String HelloMethod(String name)
{
return "你输入的是:" + name;
}
}
}
这里需要注意的是这个类必须继承MarshalByRefObject。using System.Collections.Generic;
using System.Text;
namespace General
{
public class HelloServer : MarshalByRefObject
{
public HelloServer()
{
}
public String HelloMethod(String name)
{
return "你输入的是:" + name;
}
}
}
然后我们新建一个叫Sever的控制台应用程序,添加多刚才General的引用,在Server控制台程序里建立一个类,完整代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using General;
namespace Server
{
class Program
{
public static int Main(string[] args)
{
//注册通道代码,这里有两种方式,分别是Tcp和Http
//8087是Tcp通道使用的端口
//8090是Http通道使用的端口
TcpChannel chan1 = new TcpChannel(8087);
HttpChannel chan2 = new HttpChannel(8090);
//向通道服务注册Tcp通道
ChannelServices.RegisterChannel(chan1);
//向通道服务注册Http通道
ChannelServices.RegisterChannel(chan2);
//注册通道服务
//HelloServer是General里的一个类
//SingleCall:Remoting会为每一个客户端建立一个远程对象实例
//,至于对象实例的销毁则是由GC自动管理的
//如果设置为SingleTon激活方式,则Remoting将为所有客户端建立同一个对象实例
RemotingConfiguration.RegisterWellKnownServiceType
(
typeof(HelloServer),
"SayHello",
WellKnownObjectMode.Singleton
);
//以下代码是为了让服务一直运行着
//注册宿主的方法有三种:控制台程序、Windows服务、IIS
//下面就是使用的控制台程序的方法
System.Console.WriteLine("Press Enter key to exit");
System.Console.ReadLine();
return 0;
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using General;
namespace Server
{
class Program
{
public static int Main(string[] args)
{
//注册通道代码,这里有两种方式,分别是Tcp和Http
//8087是Tcp通道使用的端口
//8090是Http通道使用的端口
TcpChannel chan1 = new TcpChannel(8087);
HttpChannel chan2 = new HttpChannel(8090);
//向通道服务注册Tcp通道
ChannelServices.RegisterChannel(chan1);
//向通道服务注册Http通道
ChannelServices.RegisterChannel(chan2);
//注册通道服务
//HelloServer是General里的一个类
//SingleCall:Remoting会为每一个客户端建立一个远程对象实例
//,至于对象实例的销毁则是由GC自动管理的
//如果设置为SingleTon激活方式,则Remoting将为所有客户端建立同一个对象实例
RemotingConfiguration.RegisterWellKnownServiceType
(
typeof(HelloServer),
"SayHello",
WellKnownObjectMode.Singleton
);
//以下代码是为了让服务一直运行着
//注册宿主的方法有三种:控制台程序、Windows服务、IIS
//下面就是使用的控制台程序的方法
System.Console.WriteLine("Press Enter key to exit");
System.Console.ReadLine();
return 0;
}
}
}
之后建立一个WEB程序,添加General的引用,调用服务器的方法,完整代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.IO;
using General;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
////使用TCP通道得到远程对象
//使用HTTP通道得到远程对象
//获得当前已注册的通道;
IChannel[] channels = ChannelServices.RegisteredChannels;
//关闭指定名为MyTcp的通道;
foreach (IChannel eachChannel in channels)
{
if (eachChannel.ChannelName == "http")
{
HttpChannel tcpChannel = (HttpChannel)eachChannel;
//关闭监听;
tcpChannel.StopListening(null);
//注销通道;
ChannelServices.UnregisterChannel(tcpChannel);
}
}
//获得服务器的一个引用
HelloServer obj2 = (HelloServer)Activator.GetObject(
typeof(General.HelloServer),
"http://localhost:8090/SayHello");
if (obj2 != null)
{
this.TextBox2.Text = obj2.HelloMethod(this.TextBox1.Text.Trim());
}
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.IO;
using General;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
////使用TCP通道得到远程对象
//使用HTTP通道得到远程对象
//获得当前已注册的通道;
IChannel[] channels = ChannelServices.RegisteredChannels;
//关闭指定名为MyTcp的通道;
foreach (IChannel eachChannel in channels)
{
if (eachChannel.ChannelName == "http")
{
HttpChannel tcpChannel = (HttpChannel)eachChannel;
//关闭监听;
tcpChannel.StopListening(null);
//注销通道;
ChannelServices.UnregisterChannel(tcpChannel);
}
}
//获得服务器的一个引用
HelloServer obj2 = (HelloServer)Activator.GetObject(
typeof(General.HelloServer),
"http://localhost:8090/SayHello");
if (obj2 != null)
{
this.TextBox2.Text = obj2.HelloMethod(this.TextBox1.Text.Trim());
}
}
}
之后我们首先运行Sever端,接着运行客户端。
在客户端输入测试,点击查看输入,结果如下: