Example #1
0
        private string SendCommand(string domain, string command, string payload = "")
        {
            Program.WriteLine("Try to Connect to Powersocket on " + address, false, true);
            if (address.Length == 0)
            {
                return("");
            }


            Ping      p     = new Ping();
            PingReply reply = p.Send(address);

            Program.WriteLine("Ping send", false, true);
            if (reply.Status == IPStatus.Success)
            {
                Program.WriteLine("Ping Success", false, true);
                byte[] bytes     = new byte[4096];
                Socket sender    = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                bool   connected = false;
                Program.WriteLine("Try Establish Socket Connection", false, true);
                try
                {
                    sender.Connect(address, port);
                    connected = true;
                }
                catch (Exception)
                {
                    Program.WriteLine("...failed", false, true);
                    connected = false;
                }

                if (connected)
                {
                    Program.WriteLine("...connected", false, true);
                    if (payload.Length < 2)
                    {
                        payload = "{}";
                    }

                    string msg       = "{\"" + domain + "\":{\"" + command + "\":" + payload + "}}";
                    byte[] byteMsg   = Xor.TPEncrypt(msg);
                    int    bytesSent = sender.Send(byteMsg);
                    int    bytesRec  = sender.Receive(bytes);
                    sender.Shutdown(SocketShutdown.Both);
                    sender.Close();
                    Program.WriteLine("data Received", false, true);
                    if (bytesRec > 2)
                    {
                        Program.WriteLine("parse data", false, true);
                        return(Xor.TPDecrypt(bytes, bytesRec));
                    }
                }
            }

            return("{}");
        }