日期:2014-05-18  浏览次数:20876 次

关于使用remoting实现分布式程序
程序使用winform形式,分别放在不同的机器,一个服务器端,多个客户端。使用remoting技术又谁做过?提供思路或者简单的程序,怎么来使用remoting。

------解决方案--------------------
http://blog.csdn.net/zhoufoxcn/archive/2007/06/12/1649776.aspx
《简述WebService与.NET Remoting的区别及适应场合 》

先了解一些概念性的东西吧。
------解决方案--------------------
你不用急着去做,只简单的实现一个remoting的例子再扩展去做,完全可以实现你功能。
------解决方案--------------------
刚刚才写的一个!

服务器和客户端需要引用“类文件” 和System.Runtime.Remoting;

类文件
--------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;


namespace db
{
public class DBAccess:MarshalByRefObject
{
private SqlConnection con;
private SqlDataAdapter da;
private DataSet ds;
public DBAccess()
{
Console.Write( "构造方法 ");
}

public DataSet GetDateSet(string strSql,string tabName)
{
con = new SqlConnection( "server=(local); " + "integrated security=SSPI; " + "database=henry ");
da = new SqlDataAdapter(strSql, con);
SqlCommandBuilder builder = new SqlCommandBuilder(da);
ds = new DataSet();
da.Fill(ds,tabName);
return ds;
}

public override object InitializeLifetimeService()
{
return null;
}
}
}
--------------------------------------

服务器文件
--------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;


namespace db
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private DBAccess obj;
private void Form1_Load(object sender, EventArgs e)
{
TcpServerChannel channel = new TcpServerChannel(8888);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(DBAccess), "Hi ", WellKnownObjectMode.Singleton);

}
}
}

------------------------------------------------------

客户端代码 form1
---------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;


namespace db
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private DBAccess obj;
private void Form1_Load(object sender, EventArgs e)
{
TcpClientChannel channle = new TcpClientChannel();
obj = (DBAccess)Activator.GetObject(typeof(DBAccess), "tcp:/服务器的ip地址:8888/Hi &q