namespace RemoteHello
{
/// <summary>
/// HELLO类
/// </summary>
public class Hello
{
public Hello()
{
}
/// <summary>
/// 定义一个接口
/// </summary>
public interface IServerOject
{
void SendMsg(string hostname, string msg);
} }
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using RemoteHello;namespace HelloServer
{
/// <summary>
/// 服务器端程序
/// </summary>
class Program:System.MarshalByRefObject,RemoteHello.Hello.IServerOject
{
static void Main(string[] args)
{
//System.Runtime.Remoting.Channels.Tcp.TcpServerChannel为远程调用,实现,使用TCP协议,传输消息的服务器信道,3698是信道侦听的端口
TcpChannel channel = new TcpChannel(3698);
//HttpChannel channel = new HttpChannel(3698);
//System.Runtime.Remoting.Channels.ChannelServices提供帮助进行,远程处理信道注册\解析和URL发现的静态方法,无法继承此类.
ChannelServices.RegisterChannel(channel,true);//该方法向信道服务,注册信道
//System.Runtime.Remoting.RemotingConfiguration提供多种,配置远程处理结构,的静态方法
//RegisterWellKnownServiceType方法将在服务器端提供的System.Runtime.Remoting.WellKnownServiceTypeEntry中记录的对象System.Type注册为已知类型
//WellKnownObjectMode.SingleCall正在被注册的已知对象类型的激活方式
RemotingConfiguration.RegisterWellKnownServiceType(typeof(HelloServer.Program), "test", WellKnownObjectMode.SingleCall);
Console.ReadLine();
}
/// <summary>
/// 实现接口方法
/// </summary>
/// <param name="hostname"></param>
/// <param name="msg"></param>
public void SendMsg(string hostname, string msg)
{
Console.WriteLine("主机:{0}发送了消息:{1}", hostname, msg);
}
}
}
//这个是客户端程序(asp.net网页的后台程序)
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.Channels;
using System.Runtime.Remoting;
using RemoteHello;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
//System.Activator:包含特定的方法,用以在本地或从远程创建对象类型,或获取对现有远程对象的引用.无法继承此类.
RemoteHello.Hello.IServerOject client = (RemoteHello.Hello.IServerOject)Activator.GetObject(typeof(RemoteHello.Hello.IServerOject), "tcp://192.168.1.101:3698/test");
client.SendMsg("buddha",this.TextBox1.Text);
}
}
运行的话要报错。忘高手指点。。
{
/// <summary>
/// HELLO类
/// </summary>
public class Hello
{
public Hello()
{
}
/// <summary>
/// 定义一个接口
/// </summary>
public interface IServerOject
{
void SendMsg(string hostname, string msg);
} }
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using RemoteHello;namespace HelloServer
{
/// <summary>
/// 服务器端程序
/// </summary>
class Program:System.MarshalByRefObject,RemoteHello.Hello.IServerOject
{
static void Main(string[] args)
{
//System.Runtime.Remoting.Channels.Tcp.TcpServerChannel为远程调用,实现,使用TCP协议,传输消息的服务器信道,3698是信道侦听的端口
TcpChannel channel = new TcpChannel(3698);
//HttpChannel channel = new HttpChannel(3698);
//System.Runtime.Remoting.Channels.ChannelServices提供帮助进行,远程处理信道注册\解析和URL发现的静态方法,无法继承此类.
ChannelServices.RegisterChannel(channel,true);//该方法向信道服务,注册信道
//System.Runtime.Remoting.RemotingConfiguration提供多种,配置远程处理结构,的静态方法
//RegisterWellKnownServiceType方法将在服务器端提供的System.Runtime.Remoting.WellKnownServiceTypeEntry中记录的对象System.Type注册为已知类型
//WellKnownObjectMode.SingleCall正在被注册的已知对象类型的激活方式
RemotingConfiguration.RegisterWellKnownServiceType(typeof(HelloServer.Program), "test", WellKnownObjectMode.SingleCall);
Console.ReadLine();
}
/// <summary>
/// 实现接口方法
/// </summary>
/// <param name="hostname"></param>
/// <param name="msg"></param>
public void SendMsg(string hostname, string msg)
{
Console.WriteLine("主机:{0}发送了消息:{1}", hostname, msg);
}
}
}
//这个是客户端程序(asp.net网页的后台程序)
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.Channels;
using System.Runtime.Remoting;
using RemoteHello;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
//System.Activator:包含特定的方法,用以在本地或从远程创建对象类型,或获取对现有远程对象的引用.无法继承此类.
RemoteHello.Hello.IServerOject client = (RemoteHello.Hello.IServerOject)Activator.GetObject(typeof(RemoteHello.Hello.IServerOject), "tcp://192.168.1.101:3698/test");
client.SendMsg("buddha",this.TextBox1.Text);
}
}
运行的话要报错。忘高手指点。。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货