Esempio n. 1
0
        private void ReceiveUdpMessage()
        {
            IPEndPoint remoteIp = null;

            try
            {
                while (true)
                {
                    byte[]     rcvData = UdpReceiver.Receive(ref remoteIp); // получаем данные
                    UdpMessage msg     = new UdpMessage(rcvData);
                    if (msg.CheckMessage())
                    {
                        string userName = msg.GetName();

                        Console.WriteLine("User {0}[{1}] joined chat", userName, remoteIp.Address.ToString());
                        HisoryWriter.WriteLine("User {0}[{1}] joined chat", userName, remoteIp.Address.ToString());

                        if (remoteIp.Address.Equals(LocalAddress))
                        {
                            continue;
                        }
                        try                 //новый пользователь в сети
                        {
                            UsersAddress.Add(remoteIp.Address);
                            TcpClient client = new TcpClient();
                            client.Connect(remoteIp.Address.ToString(), TcpPort);
                            ConnectedUser user = new ConnectedUser(client, this);
                            user.Name = userName;
                            Connected.Add(user);
                            Thread getMessageThread = new Thread(new ThreadStart(user.GetMessage));
                            getMessageThread.Start();
                            user.SendMessage(new TcpMessage(0, Name).GetBytes());
                            Thread.Sleep(100);
                            user.SendMessage(new TcpMessage(ConnectionTime.Day, ConnectionTime.Hour, ConnectionTime.Minute, ConnectionTime.Second).GetBytes());

                            //отправка сообщения с указанием имени новому пользователю
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Closed UDP Receiver");
            }
            finally
            {
                if (UdpReceiver != null)
                {
                    UdpReceiver.Close();
                }
            }
        }
Esempio n. 2
0
 public void Working()
 {
     try
     {
         Thread receiveThread = new Thread(new ThreadStart(ReceiveUdpMessage));
         receiveThread.Start();
         Thread listenThread = new Thread(new ThreadStart(ListenTcp));
         listenThread.Start();
         SendUdpMessage(Name);
         Console.WriteLine("Wait 5 seconds...");
         Thread.Sleep(5000);
         if (Connected.Count > 0)
         {
             ConnectedUser user = Connected[0];
             for (int i = 1; i < Connected.Count; i++)
             {
                 if (user.UserAge > Connected[i].UserAge)
                 {
                     user = Connected[i];
                 }
             }
             if (user.UserAge < ConnectionTime)
             {
                 user.SendMessage((new TcpMessage(4, "")).GetBytes());
             }
         }
         SendTcpMessage();
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }