简单的.NETRemoting

来源:互联网 发布:linux apt get 修改源 编辑:程序博客网 时间:2024/05/23 12:52

创建一类库:其中写构造函数和服务器端真正要调用的方法,这就是remoting中的代理

每个文件都是一单独的工程

文件一:

using System;
using System.Collections.Generic;
using System.Text;

namespace InterRemoting
{
    public class RemotingClass:MarshalByRefObject
    {
        public RemotingClass()
        {
             System.Console.WriteLine("New Referance Added!");
        }
       public int sum(int a, int b)
       {
           return a + b;
       }

    }
}

 

创建一控制台程序:

用来注册信道和具体方法--服务器端

文件二:

 

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using InterRemoting;

namespace RemotingClient
{
    class Program
    {
        static void Main(string[] args)
        {
            ChannelServices.RegisterChannel(new TcpClientChannel(),false);
            RemotingClass remoteobj = (RemotingClass)Activator.GetObject(typeof(RemotingClass), "tcp://localhost:6666/myRemoteObject");
            Console.WriteLine("1 + 2 = " + remoteobj.sum(1,2).ToString());
            Console.ReadLine();

        }
    }
}

 

创建一控制台程序:

用来来调用服务器端端口的方法--客户端

文件三:

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using InterRemoting;

namespace RemotingClient
{
    class Program
    {
        static void Main(string[] args)
        {
            ChannelServices.RegisterChannel(new TcpClientChannel(),false);
            RemotingClass remoteobj = (RemotingClass)Activator.GetObject(typeof(RemotingClass), "tcp://localhost:6666/myRemoteObject");
            Console.WriteLine("1 + 2 = " + remoteobj.sum(1,2).ToString());
            Console.ReadLine();

        }
    }
}


 

 

 

 


 

原创粉丝点击