程序使用winform形式,分别放在不同的机器,一个服务器端,多个客户端。服务器端启动时会将数据读到内存,客户端要使用服务器端读到内存中的数据。请问:如何在客户端也能改变服务器端中内存中的数据?使用remoting....先谢了。

解决方案 »

  1.   

    --RemotingClass:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Data.SqlClient;
    namespace RemotingClass
    {
        public class FarClass 
        {
            public static int i;
            SqlConnection conn;
            public FarClass()
            {
                conn = new SqlConnection();
                conn.ConnectionString = "Data Source=localhost;Database=test;User ID=sa;Password=";
            }        public DataTable GetDataTable(string TableName)
            {
                SqlDataAdapter sda = new SqlDataAdapter("select * from  " + TableName, conn);
                DataSet ds = new DataSet();
                sda.Fill(ds,TableName);
                return ds.Tables[TableName];
            }
                
    }
    }
    remoting Server:
    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.Services;
    using System.Runtime.Remoting.Channels.Tcp;
    using RemotingClass;
    namespace RemotingServer
    {
        public partial class RemotingServerForm : Form
        {
            public RemotingServerForm()
            {
                InitializeComponent();
            }        private void RemotingServerForm_Load(object sender, EventArgs e)
            {
                TcpChannel chan = new TcpChannel(9999);
                ChannelServices.RegisterChannel(chan);
                FarClass fc = new FarClass();
                ObjRef obj = RemotingServices.Marshal(fc, "Tcpservice");
                RemotingServices.Unmarshal(obj);
                //RemotingConfiguration.Configure("RemotingServer.exe.Config", false);
                this.label1.Text = "服务端已启动";
            }
        }
    }
    Remoting Client:
    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;
    using System.Reflection;
    using System.Diagnostics;
    using RemotingClass;
    using System.IO;
    namespace RemotingClient
    {
        public partial class RemotingClientForm : Form
        {
            FarClass fc;
            DataTable table;
            public RemotingClientForm()
            {
                InitializeComponent();
            }        private void RemotingClientForm_Load(object sender, EventArgs e)
            {
                ChannelServices.RegisterChannel(new TcpChannel());
                WellKnownClientTypeEntry RemotingConfing = new WellKnownClientTypeEntry(typeof(FarClass), "tcp:localhost:9999/TcpService");
                RemotingConfiguration.RegisterWellKnownClientType(RemotingConfing);
                //RemotingConfiguration.Configure("RemotingClient.exe.Config",false);
                fc = new FarClass();
            }        private void button4_Click(object sender, EventArgs e)
            {
                table = new DataTable();
                table = fc.GetDataTable("student");
                this.dataGridView1.DataSource = table;
            }
           }
    }
      

  2.   

    我的问题是这样的,我要在服务器端把数据读到内存,然后几个客户端使用这个内存中的同一个数据,以便在任一个客户端改动数据时,服务器内存中的数据会随时同步给客户端。可我的操作是,客户端好像自己new了一个实例,与服务端的那个实例不是一个。所以客户端的数据改动与服务器端无法同步。
      

  3.   

    用remoting能否使所有客户端使用服务端的同一个实例呢?书上都说是可以访问服务器上的对象。可我想访问同一个实例,不知道同学们有做过的没有?