62,046
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Text;
namespace HelloMesage
{
public class Hello : System.MarshalByRefObject
{
public Hello()
{
Console.WriteLine("Constructor called");
}
~Hello()
{
Console.WriteLine("Deconstructor called");
}
//供客户端调用的方法
public string Greeting(string name)
{
Console.WriteLine("Greeting called");
return "Hello " + name;
}
}
}
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Text;
namespace ServerConsoleApp
{
class Program
{
static void Main(string[] args)
{
TcpServerChannel channel = new TcpServerChannel(8086);//提供服务的端口
ChannelServices.RegisterChannel(channel, false);
//参数(消息的类,服务器的名称,调用的模式)
RemotingConfiguration.RegisterWellKnownServiceType(typeof(HelloMesage.Hello), "hi", WellKnownObjectMode.SingleCall);
Console.WriteLine("Press any key to exit");
Console.ReadLine();
}
}
}
using System;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Collections.Generic;
using System.Text;
namespace ClientConsoleApp
{
class Program
{
static void Main(string[] args)
{
TcpClientChannel channel = new TcpClientChannel();
ChannelServices.RegisterChannel(channel, false);
HelloMesage.Hello obj = (HelloMesage.Hello)Activator.GetObject(typeof(HelloMesage.Hello), "tcp://localhost:8086/hi");
if (obj == null)
{
Console.WriteLine("could not locate server");
}
else
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine(obj.Greeting("Christian is " + i.ToString()));//调用服务器的方法
}
}
}
}
}