c# Remoting小例子

///近最项目用到了Remoting ,自己学习了一下,在这里记一下。



//要用到的命名空间

using System.Runtime;

using System.Runtime.Remoting;

using System.Runtime.Remoting.Channels;

using System.Runtime.Remoting.Channels.Tcp;





///实体



public class RemotingLibraryClass

{

public RemotingLibraryClass()

{

    Console.WriteLine("我连接成功");

}

public void SetMes(string Str)

{Console.WriteLine("Hello :"+s);

}

}



///services服务端

public class UsersServices

{

static void Main(){

TcpServerChannels tcp = new TcpServerChannels(8500);//8500为端口号

Channels.RegisterChannels(tcp,true)// 这里的true要与客户端的保证一样

RemotingConfiguration.RegisterWellKnownServerType(typeof(RemotingLibraryClass),"abc",WellKnownObjectMode.SingleCall);

Console.WriteLing("服务启动成功!");

Console.ReadKey();

}

}

////Client客户端





public class UsersClient

{

static void Main()

{

ChannelsServer.RegisterChannels(new TcpClientChannels(),true);

RemotingLibraryClass obj = (RemotingLibraryClass)Activator.GetObject(typeof(RemotingLibraryClass),"tcp:192.168.1.108:8500/abc");

if (obj != null)

            {

                obj.SetMess("张三");

                Console.WriteLine("服务连接成功");

            }

            else

            { Console.WriteLine("与服务连接失败"); }

           

            Console.ReadKey();}

}















你可能感兴趣的:(C#)