Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        public void DoConnect()
        {
            try
            {
                byte[] bytes = new byte[256];
                string data  = null;

                IPAddress localAddr = IPAddress.Parse(LocalHost);
                server = new TcpListener(localAddr, QuPort);
                server.Start();
                while (true)
                {
                    Status = "Waiting for a connection... ";

                    TcpClient client = server.AcceptTcpClient();
                    Status = "Connected!";

                    data   = null;
                    stream = client.GetStream();

                    int i;
                    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        var command = Commands["DoActiveSensed"];
                        if (ActiveSense.Check4Message(bytes, i) && command.CanExecute(null))
                        {
                            MessageReceived = "Received: DoActiveSensed";
                            command.Execute(null);
                            break;
                        }
                        command = Commands["DoReplyGetSystemState"];
                        if (MsgGetSystemState.Check4Request(bytes, i) && command.CanExecute(null))
                        {
                            MessageReceived = "Received: GetSystemState";
                            command.Execute(null);
                            break;
                        }
                        else
                        {
                            data            = Encoding.ASCII.GetString(bytes, 0, i);
                            MessageReceived = string.Format("Received: {0}", data);

                            data = data.ToUpper();

                            byte[] msg = Encoding.ASCII.GetBytes(data);

                            stream.Write(msg, 0, msg.Length);
                            MessageSent = string.Format("Sent: {0}", data);
                        }
                    }
                    client.Close();
                }
            }
            catch (SocketException e)
            {
                Status = string.Format("SocketException: {0}", e);
            }
            finally
            {
                // Stop listening for new clients.
                server.Stop();
            }
        }
Exemplo n.º 2
0
 public void MessageGetSystemState_Check4Message()
 {
     Assert.IsFalse(MsgGetSystemState.Check4Request(new byte[] { 0xF0, 0x00, 0x00, 0x1A, 0x50, 0x11, 0x01, 0x00, 0x7F, 0x10, 0x01 }, 11));
     Assert.IsFalse(MsgGetSystemState.Check4Request(new byte[] { 0xF0, 0x00, 0x00, 0x1A, 0x50, 0x11, 0x01, 0x00, 0x7F, 0x10, 0x00, 0xF7 }, 12));
     Assert.IsTrue(MsgGetSystemState.Check4Request(new byte[] { 0xF0, 0x00, 0x00, 0x1A, 0x50, 0x11, 0x01, 0x00, 0x7F, 0x10, 0x01, 0xF7 }, 12));
 }