读书人

Remoting在客户端服务器如何实现

发布时间: 2012-06-16 20:34:32 作者: rapoo

Remoting在客户端服务器怎么实现?
一道面试题原题!

[解决办法]
第一步:建议server端程序:

1 项目—新建—控制台程序--

using System;

using System.Runtime.Remoting;

using System.Runtime.Remoting.Channels;

using System.Runtime.Remoting.Channels.Tcp;

using System.Security.Permissions;

public class Server

{

[SecurityPermission(SecurityAction.Demand)]

public static void Main(string[] args)

{

TcpChannel serverChannel = new TcpChannel(8081);// 8081为为端口号

ChannelServices.RegisterChannel(serverChannel, true);//注册通道

//注册远程对象

RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteTest), "proname", WellKnownObjectMode.Singleton);



Console.WriteLine("Press any KEY to exit the server and stop the channel.");

Console.ReadLine();

serverChannel.StopListening(null); //停止监听

ChannelServices.UnregisterChannel(serverChannel); //注销通道

}

}

第二步:建议client端程序:

项目—新建—控制台程序---

using System;

using System.Runtime.Remoting;

using System.Runtime.Remoting.Channels;

using System.Runtime.Remoting.Channels.Tcp;

using System.Security.Permissions;

public class Client

{

[SecurityPermission(SecurityAction.Demand)]

public static void Main(string[] args)

{



TcpChannel clientChannel = new TcpChannel(); //创建通道



ChannelServices.RegisterChannel(clientChannel,true);//注册通道

//获取远程对象

//(1) WellKnown激活模式调用RemotingConfiguration的静态方法RegisterActivatedClientType()。

/*WellKnownClientTypeEntry remoteType = new WellKnownClientTypeEntry(typeof(RemoteTest), "tcp://192.168.168.165:9090/RemoteObject.rem");

RemotingConfiguration.RegisterWellKnownClientType(RemoteTest);



RemoteTest obj2 = new RemoteTest(); */

//客户端激活模式()调用Activator进程的GetObject()方法

RemoteTest obj2 = (RemoteTest)Activator.GetObject(typeof(RemoteTest), "tcp://192.168.168.165:8081/ proname ");



person m = new person("TempUser", "men", "19");

m = obj2.getInfo(m);

Console.WriteLine(m.name);



Console.Read();

}

}

注意看上面代码,两段程序代码里面反复出现RemoteTest,person等字眼,一看就知道这两个是大家自己定义的类,马上我们建立它,“tcp://192.168.168.165:8081/proname”,其中,tcp代表的tcp协议,如果是http当然就应该上http协议了,后面是IP地址和端口号,即服务器端的地址和端口号,后面的proname叫做远程对象服务名,我们暂且不记它,只要保持一致就可以了,其实在程序中对应ApplicationName属性的内容。

回到主题上来,刚才的RemoteTest,person等类都没有,直接编译肯定出错,怎么办,马上解决。



第二步:建议远程服务对象:

项目—新建—控制台程序

注意,这次建立的不需要运行,直接编译成dll文件即可,然后在服务器端和客户端分别添加引用即可,代码如下:

using System;

using System.Runtime.Remoting;

// Remote object.

public class RemoteTest : MarshalByRefObject

{

private int clientCount = 0;

public int ReCount(int i)

{

clientCount = (clientCount++)+i;

return (clientCount);

}

public string HelloWorld(string name)

{

Console.WriteLine("GOOD MORNING!");

return "Hi," + name;

}

public person getInfo(person m)

{

Console.WriteLine("THERE HAVE ONE PERSON INFOMATION HAS RESIVED!");



Console.WriteLine(m.name+"-"+m.age+"-"+m.sex);

person p = new person(m.name, m.sex, m.age);



return p;

}

}

[Serializable] //必须进行序列化

public class person

{

public person(string name, string sex, string age)

{

this.name = name;

this.sex = sex;

this.age = age;

Console.WriteLine("--------------------------------------");



}

public string name;

public string sex;

public string age;

}

文件中有很多我们没有办法知道的字眼和,我们也不知道干什么的,不过毕竟是例子,暂且不管.

以后你会知道,RemoteTest是服务器端和客户端都要用的远程对象,person是我们传输对象的一个例子而已。

因为没有做捕获异常等处理,先运行服务器端,服务器端显示

Press any KEY to exit the server and stop the channel.

THERE HAVE ONE PERSON INFOMATION HAS RESIVED!

//下面两行是客户端运行后服务器端增加显示的内容

TempUser-19-men

--------------------------------------

再运行客户端。客户端显示

--------------------------------------

TempUser

以上是一个简单的remoting通讯的例子,我自定义了一个person类,两边分别传递,

在Remoting中,对于要传递的对象,设计者除了需要了解通道的类型和端口号之外,无需再了解数据包的格式。但必须注意的是,客户端在获取服务器端对象时,并不是获得实际的服务端对象,而是获得它的引用。这既保证了客户端和服务器端有关对象的松散耦合,同时也优化了通信的性能。

今天首先记住几个重要步骤:

a 必须添加如下引用

using System.Runtime.Remoting;

using System.Runtime.Remoting.Channels;

using System.Runtime.Remoting.Channels.Tcp;

using System.Security.Permissions;

b 服务器端

1 注册通道 :

TcpChannel serverChannel = new TcpChannel(8081);

ChannelServices.RegisterChannel(clientChannel,true);

2 注册远程对象 :

RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteTest), "proname", WellKnownObjectMode.Singleton);

3 注销通道:

serverChannel.StopListening(null); //停止监听

ChannelServices.UnregisterChannel(serverChannel); //注销通道



C 客户端

1 注册通道

TcpChannel clientChannel = new TcpChannel(); //创建通道



ChannelServices.RegisterChannel(clientChannel,true);//注册通道

2 获得远程对象

RemoteTest obj2 = (RemoteTest)Activator.GetObject(typeof(RemoteTest), "tcp://192.168.168.165:8081/ proname ");

还有也可以如下方式获得:

RegisterActivatedClientType()。

/*WellKnownClientTypeEntry remoteType = new WellKnownClientTypeEntry(typeof(RemoteTest), "tcp://192.168.168.165:9090/RemoteObject.rem");

RemotingConfiguration.RegisterWellKnownClientType(RemoteTest);



RemoteTest obj2 = new RemoteTest(); */

即WellKnown激活模式调用RemotingConfiguration的静态方法,

d 远程对象

编译成dll,分别在服务器和客户端添加引用,注意传输的对象必须[Serializable]序列化,远程对象必须继承MarshalByRefObject

以上就是我们对remoting的一个初步讲解,至于后期如何响应事件啊,如何激活远程对象和remoting的原理和分解


http://www.cnblogs.com/hocylan/archive/2008/01/02/1023237.html
有我写的第篇remoting的DEMO
测试过,可以通过!

读书人网 >C#

热点推荐