[学习公共库]Remoting的用法 |
麦林 发表于 2008/10/20 13:46:27 | 中间类using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;using Jeez.Common.Base;
namespace RemoteObject{ //客户端获取到服务端的对象实际是一个对象的引用,必须继承:MarshalByRefObject public class DBServer : System.MarshalByRefObject { public DBServer() { }
public static string Uid; public static string Pwd; public static string AllData;
public string ReData() { return AllData; }
public string ReAll() { return Uid + Pwd; } }}说明1:在Remoting中的远程对象中,如果还要调用或传递某个对象,例如类,或者结构,则该类或结构则必须实现串行化Attribute[SerializableAttribute]:说明2:将该远程对象以类库的方式编译成Dll。这个Dll将分别放在服务器端和客户端,以添加引用说明3:在Remoting中能够传递的远程对象可以是各种类型,包括复杂的DataSet对象,只要它能够被序列化
服务端using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Net.Sockets;using System.Net;using System.Threading;using System.Runtime.Remoting;using System.Runtime.Remoting.Channels;using System.Runtime.Remoting.Channels.Tcp;using RemoteObject;
namespace Server{ public partial class RemoteServer : Form { public RemoteServer() { InitializeComponent(); }
public string ConSql; public static string DbName;
//实例通道,为tcp传输提供端口 TcpServerChannel channel = new TcpServerChannel(85); bool IsRegister = false;
private void Register() //注册 { IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName()); IPAddress ipAddr = ipHost.AddressList[0];
//可以给RemoteObject.DBServer类传递数据.......
//注册通道 ChannelServices.RegisterChannel(channel,false); IsRegister = true; //注册远程对象,即激活. RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject.DBServer), "DBServer", WellKnownObjectMode.SingleCall); }
private void CancelRegister() //取消注册 { if (IsRegister == true) { ChannelServices.UnregisterChannel(channel); IsRegister = false; } } }}
客户端using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Runtime.Remoting.Channels;using System.Runtime.Remoting.Channels.Tcp;using System.Runtime.Remoting;using RemoteObject;
namespace Client{ public partial class RemoteClient : Form { public RemoteClient() { InitializeComponent(); }
TcpClientChannel Tcc = new TcpClientChannel(); public bool IsRegister = false; private void Register(string Ip,string Port) //(Ip:服务端Ip Port:服务端端口) { ChannelServices.RegisterChannel(Tcc,false); IsRegister = true; DBServer remoteobj = (DBServer)Activator.GetObject(typeof(DBServer), "tcp://" + Ip + ":" + Port +"/DBServer"); //返回的数据 string Data = remoteobj.ReData(); string UP = remoteobj.ReAll(); }
private void OverRegister() //取消注册 { if (IsRegister == true) { ChannelServices.UnregisterChannel(Tcc); IsRegister = false; } } }}
|
|
|

.: 公告
|
« | September 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | |
|
.: 我的分类(专题)
|

.: 最新日志
.: 最新回复
|

blog名称:栗色?蓝色? 日志总数:449 评论数量:201 留言数量:37 访问次数:2260648 建立时间:2006年5月16日 |
|

.: 留言板
|

.: 链接
|

|