Beispiel #1
0
        public async Task SendMessageToServerAsync(string messageToSend)
        {
            if (string.IsNullOrEmpty(messageToSend))
            {
                return;
            }

            if (tcpClient != null)
            {
                if (tcpClient.Connected)
                {
                    StreamWriter clientStreamWriter = new StreamWriter(tcpClient.GetStream(), System.Text.Encoding.Unicode);
                    clientStreamWriter.AutoFlush = true;
                    await clientStreamWriter.WriteAsync(CryptographyService.EncryptForNetwork(messageToSend));
                }
            }
        }
        public void SendToAuthenticatedClient(CommunicationMessage communicationMessage)
        {
            byte[] textMessageAsBytes;

            try
            {
                AuthenticatedTcpClient clientToSendTo;
                textMessageAsBytes = Encoding.Unicode.GetBytes(CryptographyService.EncryptForNetwork(communicationMessage.BuildCommunicationMessageText()));

                clientToSendTo = allAuthenticatedClients.First(AuthenticatedTcpClient => AuthenticatedTcpClient.ClientID == communicationMessage.RecipientID);
                clientToSendTo.tcpClient.GetStream().WriteAsync(textMessageAsBytes, 0, textMessageAsBytes.Length);

                Array.Clear(textMessageAsBytes, 0, textMessageAsBytes.Length);
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }