public void WorkIntoThread(object state) { NewConnection User = (NewConnection)state; NetworkStream _stream = User.Stream; while (User.Client.Connected) { try { string get = ""; byte[] mass = new byte[1000000]; int message_length = 0; message_length = _stream.Read(mass, 0, mass.Length); if (mass.Length != 0) { get = Encoding.Default.GetString(mass, 0, message_length); string toClient = @"^(\w+):(.+)?:(\w+)$"; string toServer = @"^(\w+):(\w+)(:IMG:(.+))*$"; Regex toC = new Regex(toClient, RegexOptions.Multiline); Regex toS = new Regex(toServer); if (toS.IsMatch(get))// повідомлення типу КЛІЄНТ-СЕРВЕР? { Match matchS = toS.Match(get); if (matchS.Groups[2].Value == "EXIT") { if (UserDisconnect != null) { UserDisconnect(this, new UserEventArgs(User.GetName())); } GroupOfConnection.Remove(User); foreach (NewConnection con in GroupOfConnection) { if (con != User) { con.Stream.Write(mass, 0, message_length); } } } if (matchS.Groups[2].Value == "CONNECT") { string[] sm = get.Split(':'); User.SetName(sm[0]); User.Ava = sm[3]; if (NewUser != null) { NewUser(this, new UserEventArgs(User.GetName())); } GroupOfConnection[GroupOfConnection.Count - 1] = User; foreach (NewConnection con in GroupOfConnection) { if (con != User) { con.Stream.Write(mass, 0, message_length); } } foreach (NewConnection con in GroupOfConnection) { if (con != User) { string sms = con.GetName() + ":" + "CONNECT:IMG:" + con.Ava; byte[] mess = Encoding.Default.GetBytes(sms); User.Stream.Write(mess, 0, mess.Length); Thread.Sleep(1000); } } } } else if (toC.IsMatch(get))// повідомлення типу КЛІЄНТ-КЛІЄНТ? { Match m = toC.Match(get); if (SendMessege != null) { SendMessege(this, new MessageEventArgs(User.GetName(), ": відправив повідомлення користувачу " + m.Groups[3].Value + "(" + m.Groups[2].Value + ")" + "\n")); } if (GetMessege != null) { GetMessege(this, new MessageEventArgs(m.Groups[3].Value, ": отримав повідомлення від " + User.GetName() + "(" + m.Groups[2].Value + ")" + "\n")); } foreach (NewConnection con in GroupOfConnection) { if (con != User & con.GetName() == m.Groups[3].Value) { con.Stream.Write(mass, 0, message_length); break; } } } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } }