예제 #1
0
        static void Main(string[] args)
        {
            List <User>   Clients = new List <User>();
            List <String> History = new List <String>();
            UDP           udp     = new UDP();
            TCP           tcp     = new TCP();
            bool          Chat    = true;
            string        Message;
            Thread        UdpListenThread = null;
            Thread        TcpListenThread = null;

            try
            {
                Console.Write("Имя пользователя: ");
                Login = Console.ReadLine();

                udp.Connect(Login, UdpPort, ref History);
                UdpListenThread = new Thread(() => { udp.Receive(ref Clients, ref History, Login, TcpPort, UdpPort); });
                UdpListenThread.Start();

                TcpListenThread = new Thread(() => { tcp.Listen(ref Clients, ref History, TcpPort); });
                TcpListenThread.Start();
                Thread.Sleep(5000);

                if (Clients.Count != 0)
                {
                    tcp.SendHistoryRequest(Clients[0]);
                }

                while (Chat)
                {
                    Message = Console.ReadLine();
                    if (Message != "0")
                    {
                        tcp.SendMessage(ref Clients, Message);
                        History.Add(Login + " (" + DateTime.Now.ToLongTimeString() + ")" + ": " + Message + "\n");
                    }
                    else
                    {
                        TcpListenThread.IsBackground = true;
                        UdpListenThread.IsBackground = true;
                        Console.WriteLine("you left");
                        Chat = false;
                    }
                }
            }
            catch
            {
                Console.WriteLine("Ошибка сети!");
            }
            Console.ReadKey();
        }
예제 #2
0
        static void Main(string[] args)
        {
            List <Client> Clients      = new List <Client>();
            List <String> History      = new List <String>();
            UDP           Udp          = new UDP();
            TCP           Tcp          = new TCP();
            bool          continueChat = true;
            string        Message;
            Thread        UdpListenThread = null;
            Thread        TcpListenThread = null;

            try
            {
                Login = GetLoginFromUser();
                Udp.ConnectToChat(Login, UdpPort, ref History);
                UdpListenThread = new Thread(() => { Udp.Receive(ref Clients, ref History, Login, TcpPort, UdpPort); });
                UdpListenThread.Start();
                TcpListenThread = new Thread(() => { Tcp.ListenTCP(ref Clients, ref History, TcpPort); });
                TcpListenThread.Start();
                Thread.Sleep(1000);
                if (Clients.Count != 0)
                {
                    Tcp.SendHistoryRequest(Clients[0]); //запрос истории у первого клиента из списка
                }
                while (continueChat)
                {
                    Message = Console.ReadLine();
                    if (Message != "exit")
                    {
                        Tcp.SendMessage(ref Clients, Message);//отправление сообщения
                        History.Add(Login + ": " + Message + "\n");
                    }
                    else
                    {
                        TcpListenThread.IsBackground = true;
                        UdpListenThread.IsBackground = true;
                        Console.WriteLine("You sucsesfully left this chat");
                        continueChat = false;
                    }
                }
            }
            catch
            {
                Console.WriteLine("Error! Restart your program please");
            }
            Console.ReadKey();
        }