远程对象服务器
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.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();
}
}
}
}
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();
}
}
}
}