/// <summary> /// 执行cmd /// </summary> /// <param name="cmd"></param> public static void Cmd(XMessage xmsg) { Process pro = new Process(); pro.StartInfo.FileName = "cmd.exe"; pro.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序启动进程 pro.StartInfo.CreateNoWindow = true;//不创建窗体 //重定向标准输入 pro.StartInfo.RedirectStandardInput = true; //重定向标准输出 pro.StartInfo.RedirectStandardOutput = true; //重定向错误输出 pro.StartInfo.RedirectStandardError = true; pro.Start(); pro.StandardInput.WriteLine(xmsg.Params["Content"]); pro.StandardInput.WriteLine("exit"); pro.WaitForExit(); string output = pro.StandardOutput.ReadToEnd(); string error = pro.StandardError.ReadToEnd(); if (output != "") { Client.Msg(output); } if (error != "") { Client.Msg(error); } }
static void Main(string[] args) { //XMessage msg = new XMessage("<XMessage><Type>cmd</Type><Params><Param><Pname>from</Pname><Pvalue>Ian</Pvalue></Param><Param><Pname>msg</Pname><Pvalue>Hi!</Pvalue></Param></Params></XMessage>"); XMessage msg = new XMessage("asd"); //<XMessage><Type>cmd</Type><Params><Param><Pname>ping</Pname><Pvalue>127.0.0.1</Pvalue></Param></Params></XMessage> Console.WriteLine("{0}说:{1}",msg.Params["from"],msg.Params["msg"]); Console.ReadKey(); }
/// <summary> /// 重启 /// </summary> public static void Restart(int cid) { NetworkStream nStream = clientList[cid].GetStream(); XMessage msg = new XMessage(); msg.Type = "Restart"; //msg.AddParam("DeviceId", Config.DeviceId); byte[] buffer = Encoding.Unicode.GetBytes(msg.Raw); nStream.Write(buffer, 0, buffer.Length); }
/// <summary> /// 发送cmd /// </summary> /// <param name="cid"></param> /// <param name="cmd"></param> public static void Cmd(int cid, string cmd) { NetworkStream nStream = clientList[cid].GetStream(); XMessage msg = new XMessage(); msg.Type = "Cmd"; msg.AddParam("Content", cmd); byte[] buffer = Encoding.Unicode.GetBytes(msg.Raw); nStream.Write(buffer, 0, buffer.Length); }
/// <summary> /// 向服务端发送登录 /// </summary> public static void Login() { NetworkStream nStream = client.GetStream(); XMessage msg = new XMessage(); msg.Type = "Login"; msg.AddParam("DeviceId", Config.DeviceId); byte[] buffer = Encoding.Unicode.GetBytes(msg.Raw); nStream.Write(buffer, 0, buffer.Length); }
/// <summary> /// 向服务端发送消息 /// </summary> public static void Msg(string text) { NetworkStream nStream = client.GetStream(); XMessage msg = new XMessage(); msg.Type = "Msg"; text = Util.EncodingText(text); msg.AddParam("Text", text); byte[] buffer = Encoding.Unicode.GetBytes(msg.Raw); nStream.Write(buffer, 0, buffer.Length); }
/// <summary> /// 处理消息 /// </summary> /// <param name="msg"></param> /// <param name="cid"></param> private static void HandleMessage(string msg,int cid) { XMessage xmsg = new XMessage(msg); switch (xmsg.Type) { case "Login": HandleXMsg.Login(xmsg,cid); break; case "Msg": HandleXMsg.Msg(xmsg, cid); break; default: break; } }
public static void Msg(XMessage xmsg, int cid) { MainWin.ServerHandle.txbConsole.AppendText("------------------------------------------\n"); MainWin.ServerHandle.txbConsole.AppendText(Server.clientNameList[cid] + "回复消息:\n"); MainWin.ServerHandle.txbConsole.AppendText(Util.DecodingText(xmsg.Params["Text"]) + "\n"); }
//15111466812 /// <summary> /// 客户端登陆 /// </summary> /// <param name="xmsg"></param> public static void Login(XMessage xmsg, int cid) { MainWin.ServerHandle.lsbDevice.Items.Add(xmsg.Params["DeviceId"] + "@" + cid); Server.clientNameList.Add(cid, xmsg.Params["DeviceId"]); }
/// <summary> /// 处理消息 /// </summary> /// <param name="msg"></param> /// <param name="cid"></param> private static void HandleMessage(string msg) { XMessage xmsg = new XMessage(msg); switch (xmsg.Type) { case "Shutdown": HandleXMsg.Shutdown(); break; case "Restart": HandleXMsg.Restart(); break; case "Cmd": HandleXMsg.Cmd(xmsg); break; default: break; } }