• 使用remoting远程控制编译机


    远程对象服务器
    using
     System;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;
    using System.Runtime.Remoting.Lifetime;
    using System.Configuration;

    namespace RemotingItem
    {
        
    public class Server
        {
            
    /////////////////////////////////////////////////////////////////////////////
            
    ///constructor
            public Server()
            {
            }
            
    /////////////////////////////////////////////////////////////////////////////
            
    ///main method
            public static int Main(string [] args)
            {
                
    //-----------------------------------------------
                
    //select channel to communicate
                
    //-----------------------------------------------
                int port = ConfigurationSettings.AppSettings["Port"== null ? 18085 : int.Parse(ConfigurationSettings.AppSettings["Port"]) ;
                TcpChannel chan 
    = new TcpChannel(port);
                Console.WriteLine(
    "Port:" + port);
                ChannelServices.RegisterChannel(chan);  

                
    //-----------------------------------------------
                
    //重新设定租约。远程对象生命周期应为租用管理器所设定的值--永久有效
                
    //-----------------------------------------------
                LifetimeServices.LeaseTime = TimeSpan.Zero;


                
    //-----------------------------------------------
                
    //register remote object
                
    //-----------------------------------------------
                RemotingConfiguration.RegisterWellKnownServiceType(
                    
    typeof(RemoteObject),
                    
    "RemotingServer",
                    WellKnownObjectMode.Singleton );

                
                
    //-----------------------------------------------
                
    //等待执行下一命令
                
    //-----------------------------------------------
                Console.WriteLine ("Waiting"); 
                Console.ReadLine ();


                
    //-----------------------------------------------
                
    //quit
                
    //-----------------------------------------------
                chan.StopListening();
                chan 
    = null;
                
    return 0;
            }
        }
    }

    远程对象:
    using System;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;
    using System.Diagnostics;
    using System.Threading;
    using System.IO;
    using System.Configuration;
    using System.Runtime.Remoting.Lifetime;

    namespace RemotingItem
    {
        
    public class RemoteObject : MarshalByRefObject
        {
            
    //////////////////////////////////////////////////////////////////////////////
            
    ///constructor
            public RemoteObject()
            {
                Console.WriteLine(
    "************************************");
                Console.WriteLine(
    "*             New Task             *");
                Console.WriteLine(
    "************************************");
            }
        
            
    //-----------------------------------------------
            
    //return message reply 测试远程对象是否激活
            
    //-----------------------------------------------
            public String ReplyMessage(String msg)
            {
                Console.WriteLine(
    "Client : "+msg);             
                
    return "Server : Yeah! I'm here";
            }


            
    //-----------------------------------------------
            
    //重新设定租约。当注释如下代码时生命周期应为租用管理器所设定的值
            
    //-----------------------------------------------
            public override object InitializeLifetimeService()
            {
                ILease lease 
    = (ILease)base.InitializeLifetimeService();
                
    if (lease.CurrentState == LeaseState.Initial)
                {
                    lease.InitialLeaseTime 
    = TimeSpan.FromMinutes(1000000);
                    lease.RenewOnCallTime 
    = TimeSpan.FromSeconds(20);
                }
                
    return lease;  
            }

            
            
    //-----------------------------------------------
            
    //执行客户端传递过来的命令
            
    //-----------------------------------------------
            public string DosCommand(String WorkingDirectory, String CmdStr,String ResultDirectory)
            {
                
                
    string output    ="";
                
    string RetVal    = "true"//返回值 
                string [] command;
                ProcessStartInfo psi;
                Process p;
                    
                
    try
                {
                    
    if (WorkingDirectory.Substring(WorkingDirectory.Length-1,1!= @"\")
                    {
                        WorkingDirectory 
    =WorkingDirectory + @"\";
                    }

                    
    //**************
                    
    //调用CMD
                    
    //**************
                    psi = new ProcessStartInfo("cmd"); 
                    
    if (WorkingDirectory.Length >1)
                        psi.WorkingDirectory
    =WorkingDirectory;
                    
    else
                        psi.WorkingDirectory
    =".";
                    psi.RedirectStandardOutput 
    = true
                    psi.RedirectStandardInput 
    = true
                    psi.UseShellExecute 
    = false
                    p 
    = Process.Start(psi); 

                    
    //**************
                    
    //执行
                    
    //**************
                    command = CmdStr.Split(',');                    
                    
    foreach (string s in command)
                    {
                        Console.WriteLine(
    "  [EXEC]"+s);
                        p.StandardInput.WriteLine(s); 
                    }        
                    p.StandardInput.WriteLine(
    "exit");                    
                    output 
    = p.StandardOutput.ReadToEnd();
                    p.WaitForExit(); 

                    
                    p.Close();
                    p        
    = null;
                    psi        
    = null;
                    output    
    = null;
                    command 
    = null;
                    
    return RetVal;
                }
                
    catch (Exception ex)
                {
                    p        
    = null;
                    psi        
    = null;
                    output    
    = null;
                    command 
    = null;
                    Console.WriteLine(ex.Message.ToString());
                    
    return ex.Message.ToString();
                }            
            }
        }
    }



  • 相关阅读:
    xcode5 ios6程序图标去掉阴影
    ios程序启动时去掉状态栏
    ios启动图标程序适配
    c语言基础:各种数据类型的输出占位符
    c语言变量作用域问题
    JS 学习笔记--11---内置对象(Global/Math)
    JS 学习笔记--10---基本包装类型
    JS 学习笔记--9---变量-作用域-内存相关
    JS 学习笔记--8---Function类型
    JS 学习笔记--6---日期和时间
  • 原文地址:https://www.cnblogs.com/linn/p/678057.html
Copyright © 2020-2023  润新知