Пример #1
0
        private async void SendButton_Click(object sender, EventArgs e)
        {
            if (Client == null)
            {
                return;
            }

            if (messageToSend.Text == "")
            {
                return;
            }

            var message = new Message
            {
                Port = MyPort,
                Data = messageToSend.Text
            };

            AddMessage(message);

            var networkStream = Client.GetStream();

            using (var streamWriter = new BinaryWriter(networkStream, Encoding.Default, true))
                using (var memoryStream = new MemoryStream())
                {
                    var binaryFormatter = new BinaryFormatter();
                    binaryFormatter.Serialize(memoryStream, message);
                    streamWriter.Write(memoryStream.Length);

                    memoryStream.Position = 0;
                    await memoryStream.CopyToAsync(networkStream);

                    await networkStream.FlushAsync();

                    messageToSend.Text = null;
                }
        }
Пример #2
0
 public void AddMessage(Message message)
 {
     textBox1.Text += $"{message.Port}: {message.Data}{Environment.NewLine}";
 }