65.9K
CodeProject 正在变化。 阅读更多。
Home

通过远程技术(TCP/HTTP)构建分布式应用程序

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.42/5 (5投票s)

Mar 23, 2008

Ms-PL
viewsIcon

29803

downloadIcon

342

本示例描述了如何使用Tcp协议通过远程技术构建分布式应用程序。

引言

本示例描述了如何使用Tcp协议通过远程技术构建分布式应用程序。

背景

(可选) 是否有任何关于本文的背景信息可能有用,例如对所介绍的基本思想的介绍?

使用代码

关于如何使用文章或代码的简要说明。类名、方法和属性,任何技巧或窍门。

代码块应设置为“Formatted”样式,如下所示

Server

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

namespace www.treaple.com
{
        public class HelloServer 
        {

                public static void Main(string [] args) 
                {
            TcpServerChannel channel = new TcpServerChannel(8085);
                        //TcpChannel chan = new TcpChannel(8085);
                        ChannelServices.RegisterChannel(channel);
                        RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello),"Hi",WellKnownObjectMode.SingleCall);
                        System.Console.WriteLine(" to quite...");
                        System.Console.ReadLine();

                }
        }
}

Client

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

namespace www.treaple.com 
{
        public class Client
        {
                [STAThread]
                public static void Main(string [] args)
                {
                        //TcpChannel chan = new TcpChannel();
                        ChannelServices.RegisterChannel(new TcpClientChannel());
                        Hello obj = (Hello)Activator.GetObject(typeof(Hello),"tcp://:8085/Hi");
                        if (obj == null) System.Console.WriteLine("Could not find machine!");
                        else Console.WriteLine(obj.Greeting("John"));
                        //else Console.WriteLine(obj.HelloMethod("John"));

                } 
        }
}

interface:

using System;

namespace www.treaple.com{
    public interface IHello {
        String HelloMethod(String name);
    }

    public class HelloServer : MarshalByRefObject, IHello {
        public HelloServer() {
            Console.WriteLine("HelloServer actived");
        }

        public String HelloMethod(String name) {
            Console.WriteLine("Hello.HelloMethod : {0}", name);
            return "hello," + name;
        }
    }
}

		

请记得使用语言下拉菜单设置代码片段的语言。

使用“var”按钮将变量或类名包裹在<code>标签中,例如this

关注点

在编写代码的过程中,你学到了什么有趣/好玩/令人恼火的东西吗? 你做了什么特别巧妙、疯狂或异想天开的事情吗?

历史

如果您对本示例有任何疑问,请访问 http://www.treaple.com/bbs/thread-27-1-1.html 或发送电子邮件给我。

© . All rights reserved.