遠端開發分散式C#程式設計例項
從客戶端到服務端的遠端網路通訊,傳送訊息並返回遠端訊息。
遠端物件(RemoteTest)C# 類庫程式碼部分:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->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;
}
}
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->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 類,便讓客戶端能訪問該遠端物件了。
服務端(Server)命令列應用程式程式碼部分:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->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();
}
}
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->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)命令列應用程式程式碼部分:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->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);
}
}
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->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中顯示。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-541545/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- HarmonyOS遠端狀態訂閱開發例項
- 分散式設計與開發分散式
- C#開發例項大全C#
- C#多執行緒程式設計例項C#執行緒程式設計
- C# 2.0 套接字程式設計例項初探程式設計
- CSS載入遠端字型例項程式碼CSS
- 3.1.5.9 啟動遠端例項
- 多程式程式設計函式posix_spawn例項程式設計函式
- 分散式系統:遠端呼叫分散式
- 分散式系統的設計與開發分散式
- 遠端客戶端 訪問 ASM 例項客戶端ASM
- Python--物件導向程式設計--時鐘例項開發Python物件程式設計
- bbossaop遠端服務介紹-遠端服務呼叫例項
- 高併發服務端分散式系統設計概要服務端分散式
- c# winform程式設計轉例C#ORM程式設計
- BCB 客戶端 tuxedo 開發例項客戶端UX
- Jmeter beanshell程式設計例項JMeterBean程式設計
- 設計模式例項程式碼設計模式
- KafKa Java程式設計例項KafkaJava程式設計
- mysql設定指定ip遠端訪問連線例項MySql
- Spring程式設計式和宣告式事務例項講解Spring程式設計
- 分散式系統程式設計分散式程式設計
- C# 管道式程式設計C#程式設計
- GaussDB(分散式)例項故障處理分散式
- C#例項建構函式C#函式
- PostgreSQL相似搜尋分散式架構設計與實踐-dblink非同步呼叫與多機並行(遠端遊標+記錄UDF例項)SQL分散式架構非同步並行
- BCB 客戶端 tuxedo 開發例項 (轉)客戶端UX
- Shell程式設計入門例項程式設計
- Qt 中Socket程式設計例項QT程式設計
- android socket程式設計例項Android程式設計
- The MySQL C API程式設計例項MySqlAPI程式設計
- XML程式設計例項(二) (轉)XML程式設計
- Java&CORBA程式設計例項JavaORB程式設計
- corba程式設計簡單例項ORB程式設計單例
- Java XML程式設計例項解析JavaXML程式設計
- 開發寶典:基於分散式物件的網遊程式結構設計分散式物件
- 後端程式設計師必備:分散式事務基礎篇後端程式設計師分散式
- C# 併發程式設計C#程式設計