Thread
Print

Build distribution applications by remoting(TCP)

Build distribution applications by remoting(TCP)

Introduction

.NET Remoting provides a rich and extensible framework for objects living in different AppDomains, in different processes, and in different machines to communicate with each other seamlessly. .NET Remoting offers a powerful yet simple programming model and runtime support for making these interactions transparent. In this article we will take a look at the different building blocks of the Remoting architecture, as well as explore some of the common scenarios in which .NET Remoting can be leveraged.

In this sample describes how to build a distribution application on remoting by Tcp protocol.

using the code

Server

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

namespace RemotingSamples
{
        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("<enter> to quite...");
                        System.Console.ReadLine();

                }
        }
}

Client

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

namespace RemotingSamples
{
        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://localhost: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 RemotingSamples {
    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;
        }
    }
}

Attachment

Remoting Tcp Mode.zip (37.42 KB)

3-12-2008 15:27, Downloaded count: 83

TOP

Thread