客户端发出读文件请求,服务端响应,并将服务端文件内容显示在客户端c#.net中Remoting实现新手,谁有具体时间代码?多谢

解决方案 »

  1.   

    一言难尽,仅供参考server:
    //注册提供服务的远程对象 
      this.Exporter.Export("Register WellKnown Service(Singlecall)...");
      RemotingConfiguration.RegisterWellKnownServiceType(typeof(Processor),        RemotingService.Remoting_Service_Value, WellKnownObjectMode.SingleCall);
      this.Exporter.ExportLine("OK");
      this.Exporter.ExportLine("WellKnown Service = " + RemotingService.Remoting_Service_Value     
    + ", type=" + typeof(Processor) + ", Port=" + RemotingService.Remoting_Port_Value);client:
      //异步调用
      this._process = (IProcess)Activator.GetObject(typeof(IProcess), this._url);
      AsyncMethodCallerA caller = new AsyncMethodCallerA(this._process.ProcessObj);
      IAsyncResult result = caller.BeginInvoke(requireObj, null, null);
      while (result.IsCompleted == false) {
               Thread.Sleep(50);
               System.Windows.Forms.Application.DoEvents();
      }
      object responseObj = caller.EndInvoke(result
      

  2.   

    //服务接口
    public interface IProcessor
    {
      object RemoteExecute(object serializableObj);//应用方法
    }//服务实现
    public class Processor : IProcessor
    {
      public object RemoteExecute(object serializableObj)
      {
         //读文件->文件数据->返回文件数据
         string[] fileData = System.IO.File.ReadAllLines("test.txt");
         return fileData;
      }
    }//服务端
    //开始服务
    IChannel channel = new TcpChannel(7001);
    ChannelServices.RegisterChannel(channel, false);//注册一个TCP通道
    RemotingConfiguration.RegisterWellKnownServiceType(typeof(Processor), "myService", WellKnownObjectMode.SingleCall);//注册服务对象//停止服务,注意关闭TcpChannel通道
    public void ReleaseChannel()
    {
        foreach (IChannel channel in channels){
          if (channel is TcpChannel){
              TcpChannel tcpChannel = (TcpChannel)channel;
              tcpChannel.StopListening(null);//关闭监听
        } 
    }//客户端
    string url = "tcp://localhost:7001/myService";//远程服务URL
    IProcess process = (IProcess)Activator.GetObject(typeof(IProcess), url);//取得远程访问代理对象
    object fileData = process.RemoteExecute(null);//远程调用