Esempio n. 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Enter Server IP:");
            var ipString = Console.ReadLine();

            Console.WriteLine("Enter Server Port:");
            var       hostPortString = Console.ReadLine();
            IPAddress ipAddress      = IPAddress.Parse(ipString);
            int       hostPort       = int.Parse(hostPortString);

            ChatServer mainServer = new ChatServer(ipAddress, hostPort);

            mainServer.Start();
            Console.WriteLine($"Server Started at {ipString}:{hostPortString}");
            _connected = true;

            while (_connected)
            {
            }
        }
Esempio n. 2
0
        private void KeepListening()
        {
            try
            {
                while (true)
                {
                    strAnswer = srReceiver.ReadLine();
                    Console.WriteLine("Received " + strAnswer);
                    if (strAnswer == null)
                    {
                        ChatServer.RemoveUser(this);
                        break;
                    }
                    var answer = MsgEncoding.Decode(strAnswer);
                    switch (answer.Code)
                    {
                    case MsgCode.GlobalChat:
                        var message = answer.Body;
                        ChatServer.SendMessage(UserName, message);
                        break;

                    case MsgCode.RequestConnection:
                        ChatServer.GiveConnectionInfo(answer.Body);
                        break;

                    case MsgCode.OnlineListRequest:
                        var r = "";
                        foreach (var item in ChatServer.Connections)
                        {
                            r += item.UserName + ":";
                        }
                        SendToClient(MsgCode.OnlineListRequest, r);
                        break;
                    }
                }
            }
            catch (Exception)
            {
                ChatServer.RemoveUser(this);
            }
        }