Пример #1
0
        /// <summary>
        /// sending a message on the button click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                TcpClient client = new TcpClient();
                client.Connect(IPAddress.Parse(MainWindow.ip_adres_serser), 10000);

                string   textmessage   = this.Messagebox.Text;
                int      count_newLine = textmessage.Length / 30;
                string   newtext       = null;
                int      count         = 30;
                string[] mas           = textmessage.Split(' ');
                for (int i = 0; i < mas.Length; i++)
                {
                    if ((newtext + mas[i]).Length > count)
                    {
                        newtext += "&&" + mas[i] + " ";
                        count   += 30;
                    }
                    else
                    {
                        newtext += (mas[i] + " ");
                    }
                }
                var buf = Encoding.Unicode.GetBytes(string.Format("009{0}\\{1}\\{2}", NameChat, myid, newtext));

                NetworkStream s = client.GetStream();
                s.Write(buf, 0, buf.Length);

                buf = new byte[4096];
                var    iReadServ = s.Read(buf, 0, buf.Length);
                string str       = Encoding.Unicode.GetString(buf, 0, iReadServ);
                if (int.Parse(str) != 1)
                {
                    MessageBox.Show("Сообщение не отправилось, повторите попытку");
                }
                s.Close();
                s = null;

                ViewMessage(NameChat, myid);
                Messagebox.Clear();
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }