初学remoting,仅作笔记用

本文介绍了一个使用.NET Remoting实现的简单示例,演示了如何在两个进程中进行信息传输。通过创建远程对象、注册通道和配置服务类型,客户端可以与服务端进行对话。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  实现两个进程之间的信息传输。

代码如下,远程对象:

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

namespace RemotingTest
{
    public class Class1:MarshalByRefObject
    {
        public void otherSpeak(string content)
        {
            Console.WriteLine("other: " + content);
        }

        public string selfTalk(string content)
        {
            return "self: " + content;
            //Console.WriteLine("other: " + content);
        }
    }
}

服务端:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using RemotingTest;

namespace server
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("开始对话: ");
           
            //Create the server channel.
            HttpChannel serverChannel = new HttpChannel(8085);

            //Register the server channel.
            ChannelServices.RegisterChannel(serverChannel,false);

            //Expose an object for remote calls.
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(Class1), "serverTalk", WellKnownObjectMode.SingleCall);


            Class1 cominucationC = (Class1)Activator.GetObject(typeof(Class1), "http://localhost:8086/clientTalk");
           
            while(true)
            {
                string content=Console.ReadLine();
                cominucationC.otherSpeak(content);
                Console.WriteLine( cominucationC.selfTalk(content));

           
            }
        }
    }
}

客户端:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RemotingTest;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels;



namespace client
{
    class Program
    {
        static void Main(string[] args)
        {
           
            HttpChannel clientChannel = new HttpChannel(8086);
            ChannelServices.RegisterChannel(clientChannel,false);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(Class1), "clientTalk", WellKnownObjectMode.SingleCall);

            //Console.Write("输入你要对话的IP: ");
            //string IP = Console.ReadLine();
            //Class1 cominucationS = (Class1)Activator.GetObject(typeof(Class1), "http://" + IP + ":8085/serverTalk");

            Class1 cominucationS = (Class1)Activator.GetObject(typeof(Class1), "http://localhost:8085/serverTalk");

            Console.WriteLine("开始对话: ");
            while (true)
            {
                string s = Console.ReadLine();
                cominucationS.otherSpeak(s);
                Console.WriteLine( cominucationS.selfTalk(s));
            }
        }
    }
}

参见:http://www.cnblogs.com/qianlifeng/archive/2011/03/10/1980113.html

转载于:https://www.cnblogs.com/nikaihua/archive/2011/05/08/2040355.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值