Remoting技术初探--实现简单服务器客户机通信

来源:互联网 发布:sqlserver安装polybase 编辑:程序博客网 时间:2024/05/22 00:09
最近因工作需要研究了一下Remoting,因为以前一直在做Delphi开发,没有接触到这块,今天做了个小Demo,算是对Remoting有了一点浅显的认识,现把代码发布出来,希望大家指点。
A建立远程对象:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Lifetime;

namespace RemotingClass
{
    
public class PublicCls : MarshalByRefObject//一定要继承这个对象
    {  
        
public string Start;

        
//构造
        public PublicCls()
        
{
            
this.Start = "Welcome to Remoting Exemple!";
        }


        
//一个计算算法,用于测试
        public int calculate(int nI)
        
{
            
if (nI < 1)
            
{
                
return 0;
            }

            
if (nI > 1 && nI <= 2)
            
{
                
return 1;
            }

            
else
            
{
                
return calculate(nI - 1+ calculate(nI - 2);
            }

        }

    }

}

B建立服务器程序
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting;

namespace RemotingServer
{
    
public partial class FrmServer : Form
    
{
        
public FrmServer()
        
{
            InitializeComponent();
        }


        
private void btn_StartServer_Click(object sender, EventArgs e)
        
{
            
try
            
{
                RemotingConfiguration.Configure(
                    AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
//读取配置文件内信息
                this.lab_State.Text = "数据服务已开启!";
            }

            
catch (Exception ex)
            
{
                
string str = ex.Message;
                
this.lab_State.Text = str;
            }

        }

    }

}
服务器端配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
<system.runtime.remoting>
    
<application>
      
<server>
        
<wellknown  
          
mode ="Singleton"
          type 
="RemotingClass.PublicCls,RemotingClass"
          objectUri 
="publiccls"/>
      
</server>
      
<channels>
        
<channel port="8086" ref="http"/>
      
</channels>
      
<!--leaseManagerPollTime="7S"-->
      
<lifetime
         
leaseTime ="7M"
         sponsorshipTimeout
="7M"
         renewOnCallTime
="7M"/>
    
</application>
  
</system.runtime.remoting>
</configuration>
C建立客户机程序
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using RemotingClass;

namespace RemotingClient
{
    
public partial class FrmClient : Form
    
{
        
public FrmClient()
        
{
            InitializeComponent();
        }


        
private PublicCls pcls = new PublicCls();//实例化一个远程对象

        
private void FrmClient_Load(object sender, EventArgs e)
        
{
            RemotingConfiguration.Configure(
@"client.exe.config",false);//读取配置文件
            
            
if (pcls == null//如果对象不存在
            {
                
this.lab_isConncetion.Text = "Cann't conncetion the Server!";
            }

            
else {
                
this.lab_isConncetion.Text = "Conncetioned the Server!";
            }

        }


        
private void btn_Cal_Click(object sender, EventArgs e)
        
{
            
try
            
{
                
//使用远程对象进行计算
                int i = pcls.calculate(Convert.ToInt32(this.txt_Num.Text.Trim()));
                
this.lab_Cal.Text = i.ToString();
            }

            
catch(FormatException ex)
            
{
                
string str = ex.Message;
                
this.lab_Cal.Text = "输入的不是数字!";
            }

        }

    }

}
客户机配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
<system.runtime.remoting>
    
<application>
      
<client>
        
<wellknown type ="RemotingClass.PublicCls,RemotingClass"
                   url 
="http://192.168.0.117:8086/publiccls"/>
      
</client>
      
<channels>
        
<channel ref="http" port="0"></channel>
      
</channels>
    
</application>
  
</system.runtime.remoting>
</configuration>
原创粉丝点击