Server
1using System;
2using System.Runtime.Remoting;
3using System.Runtime.Remoting.Channels;
4using System.Runtime.Remoting.Channels.Tcp;
5
6namespace RemotingItem
7{
8 public class Server
9 {
10 /////////////////////////////////////////////////////////////////////////////
11 ///constructor
12 public Server()
13 {
14 }
15 /////////////////////////////////////////////////////////////////////////////
16 ///main method
17 public static int Main(string [] args)
18 {
19 Console.WriteLine("Server Activated!!!!!!!!!!!!!!!!!!");
20 //select channel to communicate
21 TcpChannel chan = new TcpChannel(18085);
22 Console.WriteLine("Port:18085");
23 ChannelServices.RegisterChannel(chan); //register channel
24 //register remote object
25 RemotingConfiguration.RegisterWellKnownServiceType(
26 typeof(RemoteObject),
27 "RemotingServer",
28 WellKnownObjectMode.Singleton );
29 //inform console
30
31 Console.WriteLine ("Waiting");
32 Console.ReadLine (); //等待执行下一命令
33 return 0;
34 }
35 }
36}
37
RemoteObject.dll2using System.Runtime.Remoting;
3using System.Runtime.Remoting.Channels;
4using System.Runtime.Remoting.Channels.Tcp;
5
6namespace RemotingItem
7{
8 public class Server
9 {
10 /////////////////////////////////////////////////////////////////////////////
11 ///constructor
12 public Server()
13 {
14 }
15 /////////////////////////////////////////////////////////////////////////////
16 ///main method
17 public static int Main(string [] args)
18 {
19 Console.WriteLine("Server Activated!!!!!!!!!!!!!!!!!!");
20 //select channel to communicate
21 TcpChannel chan = new TcpChannel(18085);
22 Console.WriteLine("Port:18085");
23 ChannelServices.RegisterChannel(chan); //register channel
24 //register remote object
25 RemotingConfiguration.RegisterWellKnownServiceType(
26 typeof(RemoteObject),
27 "RemotingServer",
28 WellKnownObjectMode.Singleton );
29 //inform console
30
31 Console.WriteLine ("Waiting");
32 Console.ReadLine (); //等待执行下一命令
33 return 0;
34 }
35 }
36}
37
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;
namespace RemotingItem
{
public class RemoteObject : MarshalByRefObject
{
//////////////////////////////////////////////////////////////////////////////
///constructor
public RemoteObject()
{
Console.WriteLine("Remote object activated");
}
//////////////////////////////////////////////////////////////////////////////
///return message reply 测试服务端是否激活
public String ReplyMessage(String msg)
{
Console.WriteLine("Client : "+msg);//print given message on console
return "Server : Yeah! I'm here";
}
/// <summary>
///
/// </summary>
/// <param name="WorkingDirectory">目标目录,可以为空</param>
/// <param name="CmdStr">命令组合,以逗号分隔</param>
/// <param name="ResultDirectory">结果存放目录,可以为空,为空时不输出日志</param>
/// <returns></returns>
public string DosCommand(String WorkingDirectory, String CmdStr,String ResultDirectory)
{
try
{
string output="";
if (WorkingDirectory.Substring(WorkingDirectory.Length-1,1) != @"\")
{
WorkingDirectory =WorkingDirectory + @"\";
}
//调用CMD
ProcessStartInfo psi = new ProcessStartInfo("cmd");
if (WorkingDirectory.Length >1)
psi.WorkingDirectory=@WorkingDirectory;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
string [] command = CmdStr.Split(',');
foreach (string s in command)
{
p.StandardInput.WriteLine(@s);
}
p.StandardInput.WriteLine(@"exit");
if (ResultDirectory.Length >0)
{
output = p.StandardOutput.ReadToEnd();
WriteFile(ResultDirectory,output);
}
p.WaitForExit();
Console.WriteLine(output);
return "true";
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
return ex.Message.ToString();
}
}
private void WriteFile(String srcPath, String text)
{
if (srcPath.Substring(srcPath.Length-1,1) == @"\")
{
srcPath =srcPath.Substring(0,srcPath.Length-1);
}
string OutputFile = srcPath + @"\Log.htm";
if (File.Exists(OutputFile))
{
File.Delete(OutputFile);
}
try
{
FileStream fsWriter = new FileStream(OutputFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
StreamWriter fileWrite = new StreamWriter(fsWriter, System.Text.Encoding.Default);
fileWrite.Write(text);
fileWrite.Close();
fileWrite = null;
fsWriter.Close();
fsWriter = null;
}
catch (Exception)
{
}
}
}
}
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Diagnostics;
using System.Threading;
using System.IO;
namespace RemotingItem
{
public class RemoteObject : MarshalByRefObject
{
//////////////////////////////////////////////////////////////////////////////
///constructor
public RemoteObject()
{
Console.WriteLine("Remote object activated");
}
//////////////////////////////////////////////////////////////////////////////
///return message reply 测试服务端是否激活
public String ReplyMessage(String msg)
{
Console.WriteLine("Client : "+msg);//print given message on console
return "Server : Yeah! I'm here";
}
/// <summary>
///
/// </summary>
/// <param name="WorkingDirectory">目标目录,可以为空</param>
/// <param name="CmdStr">命令组合,以逗号分隔</param>
/// <param name="ResultDirectory">结果存放目录,可以为空,为空时不输出日志</param>
/// <returns></returns>
public string DosCommand(String WorkingDirectory, String CmdStr,String ResultDirectory)
{
try
{
string output="";
if (WorkingDirectory.Substring(WorkingDirectory.Length-1,1) != @"\")
{
WorkingDirectory =WorkingDirectory + @"\";
}
//调用CMD
ProcessStartInfo psi = new ProcessStartInfo("cmd");
if (WorkingDirectory.Length >1)
psi.WorkingDirectory=@WorkingDirectory;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
string [] command = CmdStr.Split(',');
foreach (string s in command)
{
p.StandardInput.WriteLine(@s);
}
p.StandardInput.WriteLine(@"exit");
if (ResultDirectory.Length >0)
{
output = p.StandardOutput.ReadToEnd();
WriteFile(ResultDirectory,output);
}
p.WaitForExit();
Console.WriteLine(output);
return "true";
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
return ex.Message.ToString();
}
}
private void WriteFile(String srcPath, String text)
{
if (srcPath.Substring(srcPath.Length-1,1) == @"\")
{
srcPath =srcPath.Substring(0,srcPath.Length-1);
}
string OutputFile = srcPath + @"\Log.htm";
if (File.Exists(OutputFile))
{
File.Delete(OutputFile);
}
try
{
FileStream fsWriter = new FileStream(OutputFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
StreamWriter fileWrite = new StreamWriter(fsWriter, System.Text.Encoding.Default);
fileWrite.Write(text);
fileWrite.Close();
fileWrite = null;
fsWriter.Close();
fsWriter = null;
}
catch (Exception)
{
}
}
}
}