標籤:.sh hal sys file 語言 rem href tor partial
.Net中Remoting通訊機制前言:
本程式例子實現一個簡單的Remoting通訊案例
本程式採用語言:c#
編譯工具:vs2013工程檔案
編譯環境:.net 4.0
程式模組:
- Test測試
- Talker
- Server端
- Client端
- 原始碼工程檔案下載
Test測試程式:
Talker類:
1 public class Talker : MarshalByRefObject2 {3 public void Talk(string word)4 {5 System.Console.WriteLine(word);6 }7 8 }
Server端:
1 //註冊通道2 TcpServerChannel channel = new TcpServerChannel("TalkChannel",8090);3 ChannelServices.RegisterChannel(channel,true);4 5 //註冊遠程對象6 RemotingConfiguration.RegisterWellKnownServiceType(7 typeof(Talker),8 "Talker",9 WellKnownObjectMode.SingleCall);
Client端:
1 public partial class Form1 : Form 2 { 3 private Talker _talk = null; 4 public Form1() 5 { 6 InitializeComponent(); 7 } 8 9 private void btnSend_Click(object sender, EventArgs e)10 {11 if (btnSend.Text.Equals("開始"))12 {13 timer1.Enabled = true;14 btnSend.Text = "結束";15 }16 else17 {18 timer1.Enabled = false;19 btnSend.Text = "開始";20 }21 }22 23 private void sendMsg(string msg)24 {25 try26 {27 //操作遠程對象28 _talk.Talk(msg);29 string newline = msg + Environment.NewLine;30 txtContent.Text = txtContent.Text.Insert(0, newline);31 }32 catch (Exception ex)33 {34 MessageBox.Show(ex.Message);35 }36 }37 38 private void Form1_Load(object sender, EventArgs e)39 {40 try41 {42 timer1.Interval = 1000;43 //註冊通道44 TcpClientChannel channel = new TcpClientChannel();45 ChannelServices.RegisterChannel(channel, true);46 //擷取遠程對象47 _talk = (Talker)Activator.GetObject(typeof(Talker), "TCP://localhost:8090/Talker");48 }49 catch (Exception ex)50 {51 MessageBox.Show(ex.Message);52 }53 }54 55 private void timer1_Tick(object sender, EventArgs e)56 {57 sendMsg(txtWord.Text.Trim());58 }
原始碼工程檔案下載:
原始碼工程檔案下載 http://files.cnblogs.com/files/JiYF/RemotingSolution.rar
.Net中Remoting通訊機制簡單一實例