C#实现.Net Remoting服务端与客户端通信
作者:C/S框架网  发布日期:2011-06-26 22:56:05
C#实现.Net Remoting服务端与客户端通信


使用.Net Remoting技术实现服务端与客户端通信。

本文介绍:
1. 定义远程对象。继承 System.MarshalByRefObject.
2. 注册服务器端监听通道。使用TcpServerChannel,ChannelServices,RemotingConfiguration类.
3. 注册客户端通信通道。使用ChannelServices,Activator类。

Activator.GetObject()方法是为指定类型、URL 和通道数据所指示的已知对象创建一个代理,
参数type:希望连接到的已知对象的类型。

我们通过Activator.GetObject()方法创建远程对象进行通信。

服务器主界面, 注册两个远程对象,分别是HelloURI和CommonServiceURI

贴图图片

启动服务器代码:


private void btnStartServer_Click(object sender, EventArgs e)

        {

            //创建通信侦听通道对象

            TcpServerChannel channel = new TcpServerChannel(8086);

 

            //注册通信侦听通道

            ChannelServices.RegisterChannel(channel, true);

 

            //注册对象URI, 注册两个远程对象

            RemotingConfiguration.RegisterWellKnownServiceType(typeof(HelloData), HelloData.ObjectURI,

            WellKnownObjectMode.SingleCall);

 

            //注册对象URI, 注册两个远程对象

            RemotingConfiguration.RegisterWellKnownServiceType(typeof(CommonService), CommonService.ObjectURI,

            WellKnownObjectMode.SingleCall);

 

            lblServer.Text = "服务器已启动!";

            label1.Text = "已启动URI:\r\n" + HelloData.ObjectURI + "\r\n" + CommonService.ObjectURI;

        }




客户端,点[Call Remote Server (Hello)]按钮调用远程对象HelloData.Greeting方法。

贴图图片

源代码:

private void btnServer_Click(object sender, EventArgs e)

        {

            try

            {

                HelloData obj = (HelloData)Activator.GetObject(typeof(HelloData),

                    txtUrl.Text + "/" + HelloData.ObjectURI);

 

                if (obj == null)

                {

                    MessageBox.Show("找不到服务器!");

                    return;

                }

 

                MessageBox.Show(obj.Greeting("易学网 www.vjsdn.com "));

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

 


调用远程对象CommonService.GetOnlineList方法获取在线用户列表,返回DataTable类型数据。

贴图图片

源代码:

  private void btnCallService_Click(object sender, EventArgs e)

        {

            try

            {

                CommonService obj = (CommonService)Activator.GetObject(typeof(CommonService),

                    txtUrl.Text + "/" + CommonService.ObjectURI);

 

                if (obj == null)

                {

                    MessageBox.Show("找不到服务器!");

                    return;

                }

 

                //从远程服务器接受DataTable类型的数据

                DataTable onlines = obj.GetOnlineList();

                dataGridView1.DataSource = onlines;

 

                MessageBox.Show("获取数据成功1");

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }




两个远程对象:

1.CommonService 对象
public class CommonService : System.MarshalByRefObject
{
   /// <summary>
   /// 服务名称,对象的URI地址
   /// </summary>
   public static string ObjectURI { get { return "CommonServiceURI"; } }
   
   /// <summary>
   /// 给客户端调用的接口
   /// </summary>
   /// <param name="name"></param>
   /// <returns></returns>
   public DataTable GetOnlineList()
   {
      //从SQL后台获取数据...省略...
      
      DataTable dt = new DataTable();
      dt.Columns.Add("UserID", typeof(string));
      dt.Rows.Add(new object[] { "易学原创" });
      dt.Rows.Add(new object[] { "小猫" });
      dt.Rows.Add(new object[] { "楚楚" });
      return dt;
   }
   
}


2.HelloData 对象

public class HelloData : System.MarshalByRefObject
{
   /// <summary>
   /// 服务名称,对象的URI地址
   /// </summary>
   public static string ObjectURI { get { return "HellURI"; } }
   
   /// <summary>
   /// 给客户端调用的接口
   /// </summary>
   /// <param name="name"></param>
   /// <returns></returns>
   public string Greeting(string name)
   {
      return "您好!" + name;
   }
}



Source Code:


点击下载附件 点击下载附件 (如下载失败,请邮件通知我们寄回给您,或QQ:23404761留言.)
上一篇 下一篇