任何繼承自MarshalByRefObject的組件都可以遠端存取!然而對於訪問已部署組件的終端使用者而言TCP管道不是最好的方法。這是因為通訊端進程需要實現伸縮性、安全性和效能,這使得組件非常龐大。HTTP管道提供了安全性、密碼選項、經檢驗的伸縮性和效能。你也可使用SOAP,它具有更好的互用性。這些組件駐留在IIS中。HTTP管道使用HTTP協議。該管道使用SOAP格式化程式,即把通訊編碼為XML。
using System;
using System.Collections;
namespace BankChat{
public class Chat : System.MarshalByRefObject
{
//聲明一個數組
static ArrayList messages = new ArrayList();
public Chat()
{
System.Console.WriteLine("歡迎光臨: SaveMyMoney !");
}
public void addMsg(string CustID,string msg)
{
//把資訊添加到數組裡
messages.Add(msg);
Console.WriteLine("From :" + CustID + ",Message :"+ msg);
}
public string getAllMessages()
{
string allmessages=null;
//迴圈
foreach(string msg1 in messages)
{
allmessages += msg1 + "n";
}
return allmessages;
}
}
}
using System;
using System.Runtime;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using BankChat;
namespace BankServer
{
public class ClassServer
{
public static void Main(String[] args)
{
//產生一個http管道
HttpChannel channel = new HttpChannel(9932); //註冊管道
ChannelServices.RegisterChannel(channel); //註冊對象
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Chat),"Chat", WellKnownObjectMode.SingleCall);
System.Console.WriteLine("Hello From Bank Server");
System.Console.ReadLine();
}
}
}
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using BankChat;
namespace Client
{
public class ClassClient
{
public static void Main(string[] args)
{
Console.WriteLine("Please enter your Customer ID");
string CustID=Console.ReadLine();
Console.WriteLine("Please enter your port number");
String strport=Console.ReadLine();
int port=Int32.Parse(strport);
ChannelServices.RegisterChannel(new HttpChannel(port));
//訪問遠程對象
Chat ChatClient= (Chat)Activator.GetObject(typeof(BankChat.Chat),"http://localhost:9932/Chat");
if(ChatClient==null)
{
Console.WriteLine("Unable to get remote reference");
}
else
{
String msg;
do
{
Console.WriteLine("Enter your message or type exit?to quit");
msg=Console.ReadLine();
ChatClient.addMsg(CustID,msg);
}while(msg.ToLower().Equals("exit")==false);
Console.WriteLine("會話結束,謝謝您的參與! n");
String allmessage = ChatClient.getAllMessages();
Console.WriteLine(allmessage);
}
Console.Read();
}
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using BankChat;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Threading;
namespace ChatClient
{
/// <SUMMARY>
/// Form1 的摘要說明。
/// </SUMMARY>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
Chat ChatClient;
private System.Windows.Forms.TextBox textBox4;
static int port;
/// <SUMMARY>
/// 必需的設計器變數。
/// </SUMMARY>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 表單設計器支援所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調用後添加任何建構函式代碼
//
}
/// <SUMMARY>
/// 清理所有正在使用的資源。
/// </SUMMARY>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 表單設計器產生的程式碼
/// <SUMMARY>
/// 設計器支援所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </SUMMARY>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(416, 232);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(96, 232);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(256, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(96, 24);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(256, 120);
this.textBox2.TabIndex = 2;
this.textBox2.Text = "textBox2";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(424, 48);
this.textBox3.Name = "textBox3";
this.textBox3.TabIndex = 3;
this.textBox3.Text = "textBox3";
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(424, 104);
this.textBox4.Name = "textBox4";
this.textBox4.TabIndex = 4;
this.textBox4.Text = "textBox4";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(640, 349);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <SUMMARY>
/// 應用程式的主進入點。
/// </SUMMARY>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
//Random r=new Random(1000);
//int port=r.Next(10000);
//MessageBox.Show(Convert.ToString(port));
//訪問遠程對象
ChannelServices.RegisterChannel(new HttpChannel(port));
ChatClient= (Chat)Activator.GetObject(typeof(BankChat.Chat),"http://localhost:9932/Chat");
Thread obj=new Thread(new ThreadStart(getMessage));
obj.Start();
}
public void getMessage()
{
Chat s=new Chat();
string allmessage;
int length=0;
int l=0;
while(true)
{
allmessage = s.getAllMessages();
while(length>=l)
{
//allmessage = ChatClient.getAllMessages();
if(allmessage!=null)
{
length=allmessage.Length;
l=allmessage.Length;
textBox2.AppendText(allmessage);
}
l++;
//allmessage = ChatClient.getAllMessages();
}
}
}
private void button1_Click(object sender, System.EventArgs e)
{
ChatClient.addMsg(textBox3.Text,textBox1.Text);
}
}
}