예제 #1
0
파일: Program.cs 프로젝트: Tourie/ChatApp
        public static void Main(string[] args)
        {
            SessionMessages = new();

            try
            {
                Console.WriteLine("Input your local port: ");
                int localPort = int.Parse(Console.ReadLine() ?? throw new ArgumentException());
                Console.WriteLine("Input remote port: ");
                int remotePort = int.Parse(Console.ReadLine() ?? throw new ArgumentException());

                IPEndPoint localIpEndPoint  = new IPEndPoint(IPAddress.Parse(IpAddress), localPort);
                IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Parse(IpAddress), remotePort);

                string uid = Guid.NewGuid().ToString();
                CurPerson = new Person(uid, localIpEndPoint, remoteIpEndPoint);

                ListeningSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                ListeningSocket.Bind(CurPerson.LocalIp);

                Console.WriteLine($"Hi! Your unique identifier: {CurPerson.Id}, and endpoint: {CurPerson.LocalIp}. RemoteEndPoint: {CurPerson.RemoteIp}");

                Task listening = new Task(TryConnect);
                listening.Start();
                Task checker = new Task(Check);
                checker.Start();

                while (true)
                {
                    string message = Console.ReadLine();
                    Console.WriteLine();
                    if (message == "exit")
                    {
                        break;
                    }
                    message = CurPerson.Id + " - " + message + "\n";
                    SessionMessages.Add(message);
                    Send(message);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }
            finally
            {
                Close();
            }
        }
예제 #2
0
파일: Program.cs 프로젝트: Tourie/ChatApp
        static void Listening()
        {
            try
            {
                string msg = CurPerson.Id + " joined!\n";
                Send(msg);

                while (true)
                {
                    StringBuilder builder = new StringBuilder();
                    int           bytes   = 0;
                    byte[]        data    = new byte[256];

                    EndPoint remoteIp = new IPEndPoint(IPAddress.Any, CurPerson.RemoteIp.Port);
                    do
                    {
                        bytes = ListeningSocket.ReceiveFrom(data, ref remoteIp);
                        builder.Append(Encoding.Unicode.GetString(data, 0, bytes));
                    } while (ListeningSocket.Available > 0);

                    if (State == State.pending)
                    {
                        State = State.active;
                        RecreateHistory();
                    }

                    IPEndPoint fullEndPoint = remoteIp as IPEndPoint;
                    if (fullEndPoint?.Port == CurPerson.RemoteIp.Port && builder.ToString() != String.Empty)
                    {
                        SessionMessages.Add(builder.ToString());
                        Console.WriteLine(builder.ToString());
                    }
                }
            }
            catch (Exception)
            {
                State = State.pending;
                //Console.WriteLine("Client is off line, trying to connect...");
            }
        }