在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
从客户端到服务端的远程网络通讯,发送消息并返回远程消息。
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; } } } RemoteTest组件中的 TestLoader 类继承了 System.MarshalByRefObject 类,便让客户端能访问该远程对象了。
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(); } } } 注:引用添加组件: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); } } } 这里对new 管道声明的那两行已略写。
程序调试:启动Server后,再启动Client。输入值后关闭Client,值便在Server中显示。 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论