C#实现使用HTTP管道的.NET远程代码示例

任何继承自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);
  }
 }
    
}

posted @ 2007-07-09 15:29  looky  阅读(451)  评论(0编辑  收藏  举报