本站首页    管理页面    写新日志    退出

The Neurotic Fishbowl

[学习公共库]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;            }        }    }}

阅读全文(5662) | 回复(0) | 编辑 | 精华

 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)
 

The Neurotic Fishbowl

.: 公告

fighting for the work!


Bloginess

«September 2025»
123456
78910111213
14151617181920
21222324252627
282930

.: 我的分类(专题)

首页(449)
文字感悟(103)
学习公共库(61)
生活体会(100)
资源收集(14)
.NET-Winform(27)
音乐文字(15)
心情小记(119)
SQL问题解决(10)


In the Bowl

.: 最新日志

C#打印代码
你看到的我是蓝色的
即将逝去的3月
这个假期
两个月过去
表名存在其他表时获取数据
条码打印
Remoting的用法
INI文件用法


.: 最新回复

回复:条码打印
回复:条码打印
回复:即将逝去的3月
回复:cookie对象使用
回复:条码打印
回复:JSP学习(字符串比较)
回复:应收应付核销规则及常见问题(续)
回复:两个月过去
回复:两个月过去


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



Text Me

.: 留言板

签写新留言

链接完成
回:loseVC
来喽~
转转......
新年啦
对自己说
祝福
回:佑手
WO


Other Fish in the Sea

.: 链接


loseVC's BLOG
Collapsar_feel
布衣加针
C#and.Net
Kinogam Web




站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.062 second(s), page refreshed 144806337 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号