.net remoting

来源:互联网 发布:python instanceof 编辑:程序博客网 时间:2024/04/29 00:07

Share Library:

 

 

using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime;

 

namespace DotNetRemoteTest

{

    public class ResumeLoader : System.MarshalByRefObject

    {

        public ResumeLoader()

        {

            System.Console.WriteLine("Hello,it 's a dot net remoting test");

        }

 

        public Resume GetResumeByUserID(decimal userID)

        {

            Resume resume = new Resume(userID);

 

            resume.UserID = userID;

 

            resume.Title = "BackHam";

 

            resume.ResumeID = 1001;

 

        //    resume.Body = "M U";

 

            return resume;

        }

 

    }

}

 

 

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace DotNetRemoteTest

{

    [Serializable]

    public class Resume

    {

        private decimal resumeID, userID;

 

        private String body, title;

 

        public Resume(decimal resumeID)

        {

            this.ResumeID = resumeID;

 

            this.UserID = 1;

 

            this.Body = "This is the default body of the resume";

 

            this.Title = "This is the default Title";

        }

 

        public decimal ResumeID

        {

            get { return resumeID; }

 

            set { this.resumeID = value; }

        }

 

        public decimal UserID

        {

            get { return userID; }

 

            set { this.userID = value; }

        }

 

        public String Body

        {

            get { return body; }

 

            set

            {

                this.body = value;

            }

        }

 

        public String Title

        {

            get { return title; }

            set

            { this.title = value; }

        }

    }

}

Server:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using DotNetRemoteTest;
namespace ResumeServerServer
{
    public class ResumeSuperServer
    {
        public static void Main(String[] args)
        {
            TcpServerChannel channel = new TcpServerChannel(9932);
            ChannelServices.RegisterChannel(channel,false);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(ResumeLoader),"ResumeLoader",WellKnownObjectMode.SingleCall);
            System.Console.WriteLine("press any key");
            System.Console.ReadLine();
        }
    }
}
Client:

 

 

using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime;

using System.Runtime.Remoting;

using System.Runtime.Remoting.Channels;

using System.Runtime.Remoting.Channels.Tcp;

using DotNetRemoteTest;

using System.IO;

 

namespace ResumeClient

{

    public class ResumeClient

    {

        public static void Main(String[] args)

        {

            try

            {

                ChannelServices.RegisterChannel(new TcpClientChannel(), false);

 

                ResumeLoader loader = (ResumeLoader)Activator.GetObject(typeof(ResumeLoader), "tcp://10.167.28.158:9932/ResumeLoader");

 

                if (loader == null)

                {

                    Console.WriteLine("unable get remote referance");

                }

                else

                {

                    Resume resume = loader.GetResumeByUserID(1002);

 

                    Console.WriteLine("resumeid = " + resume.ResumeID);

 

                    Console.WriteLine("userid = " + resume.UserID);

 

                    Console.WriteLine("title = " + resume.Title);

 

                    Console.WriteLine("body = " + resume.Body);

                }

                Console.ReadLine();

            }

            catch (Exception e)

           {

                StreamWriter sw;

 

                sw = File.AppendText("log.txt");

 

                sw.WriteLine("["+ System.DateTime .Now .ToLongTimeString() + "] " );

 

                sw.WriteLine(e.ToString() + "/n");

 

                sw.Flush();

 

                sw.Close();

 

                StreamReader reader = File.OpenText("log.txt");

 

                String line;

 

                while ((line = reader.ReadLine()) != null)

                {

                    Console.WriteLine(line);

                }

 

                reader.Close();

 

                Console.ReadLine();

 

            }

        }

    }

}

 

原创粉丝点击