远程开发分布式C#编程实例
从客户端到服务端的远程网络通讯,发送消息并返回远程消息。
远程对象(RemoteTest)C# 类库代码部分:
using System;
namespace RemoteTest
{
public class TestLoader : MarshalByRefObject
{
public TestLoader()
{
string stringWrite = "stor load.";
Console.WriteLine(stringWrite);
}
public string sayHello(string msg)
{
Console.WriteLine("Message: { 0 } ", msg);
Console.WriteLine("Welcome to Message send Systems");
string stc = "Hello from remote";
return stc;
}
}
}
namespace RemoteTest
{
public class TestLoader : MarshalByRefObject
{
public TestLoader()
{
string stringWrite = "stor load.";
Console.WriteLine(stringWrite);
}
public string sayHello(string msg)
{
Console.WriteLine("Message: { 0 } ", msg);
Console.WriteLine("Welcome to Message send Systems");
string stc = "Hello from remote";
return stc;
}
}
}
RemoteTest组件中的 TestLoader 类继承了 System.MarshalByRefObject 类,便让客户端能访问该远程对象了。
服务端(Server)命令行应用程序代码部分:
using System;
using System.Runtime;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemoteTest;
namespace Server
{
public class Program
{
public static void Main(string[] args)
{
TcpServerChannel channel = new TcpServerChannel(9932);
ChannelServ ices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(TestLoader),"TestLoader",WellKnownObjectMode.SingleCall);
Console.WriteLine("Hello From Server");
Console.ReadLine();
}
}
}
using System.Runtime;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemoteTest;
namespace Server
{
public class Program
{
public static void Main(string[] args)
{
TcpServerChannel channel = new TcpServerChannel(9932);
ChannelServ ices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(TestLoader),"TestLoader",WellKnownObjectMode.SingleCall);
Console.WriteLine("Hello From Server");
Console.ReadLine();
}
}
}
注:引用添加组件:System.Runtime.Remoting(下同)。
客户端(Client)命令行应用程序代码部分:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemoteTest;
namespace Client
{
public class ClassClient
{
public static void Main(string[] args)
{
string stringTcp = "tcp://localhost:9932/TestLoader";
TestLoader loader = (TestLoader)Activator.GetObject(typeof(RemoteTest.TestLoader),stringTcp);
string msg;
Console.Write("Enter Message: ");
msg = Console.ReadLine();
Console.WriteLine("Message: {0}" + msg);
string res = loader.sayHello(msg);
Console.WriteLine(res);
}
}
}
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemoteTest;
namespace Client
{
public class ClassClient
{
public static void Main(string[] args)
{
string stringTcp = "tcp://localhost:9932/TestLoader";
TestLoader loader = (TestLoader)Activator.GetObject(typeof(RemoteTest.TestLoader),stringTcp);
string msg;
Console.Write("Enter Message: ");
msg = Console.ReadLine();
Console.WriteLine("Message: {0}" + msg);
string res = loader.sayHello(msg);
Console.WriteLine(res);
}
}
}
这里对new 管道声明的那两行已略写。
程序调试:启动Server后,再启动Client。输入值后关闭Client,值便在Server中显示。