示例#1
0
文件: ServerTcp.cs 项目: satnam83/IoT
        private async void Listener_ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
        {
            _socket = args.Socket;
            var reader = new DataReader(args.Socket.InputStream);

            try
            {
                //If Close
                while (!_cancel.IsCancellationRequested)
                {
                    //Wait recive some data
                    byte[] data = await ReciveData(reader);

                    IBuffer buffer = data.AsBuffer();

                    //Decrypt message
                    string text = Cryptographic.Decrypt(buffer, "123");

                    //Invoke event when message Recive
                    InvokeOnDataRecive(text);
                }
            }
            catch (Exception e)
            {
                InvokeOnError(e.Message);
            }
        }
示例#2
0
文件: ServerTcp.cs 项目: satnam83/IoT
        public async Task SendAsync(string text)
        {
            try
            {
                // DataWriter to send message to client
                var writer = new DataWriter(_socket.OutputStream);
                //Encrypt message
                byte[] data = Cryptographic.Encrypt(text, "123");

                //Write Lenght message in buffer
                writer.WriteInt32(data.Length);
                //Write message in buffer
                writer.WriteBytes(data);

                //Send buffer
                await writer.StoreAsync();

                //Clear buffer
                await writer.FlushAsync();
            }
            catch (Exception e)
            {
                InvokeOnError(e.Message);
            }
        }