Beispiel #1
0
        public void Process()
        {
            NetworkStream stream = null;

            try
            {
                stream = Client.GetStream();
                byte[] data = new byte[1024];
                while (true)
                {
                    var builder = new StringBuilder();
                    int bytes   = 0;
                    do
                    {
                        bytes = stream.Read(data, 0, data.Length);
                        builder.Append(Encoding.Unicode.GetString(data, 0, bytes));
                    }while (stream.DataAvailable);
                    Console.WriteLine(builder.ToString());
                    string message = ServerCommands.Decrypt(builder.ToString());
                    data = Encoding.Unicode.GetBytes(message);
                    stream.Write(data, 0, data.Length);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (!Client.Connected)
                {
                    if (stream != null)
                    {
                        stream.Close();
                    }
                    if (Client != null)
                    {
                        Client.Close();
                    }
                }
            }
        }
        static void Main(string[] args)
        {
#if LOCALHOST
            string serverip = "127.0.0.1";
#else
            string serverip = "25.148.88.117";
#endif

            int port = 5000;
            if (args.Length >= 1)
            {
                serverip = args[0];
                port     = Convert.ToInt32(args[1]);
            }

            server = new Server(serverip, port);
            server.OnClientConnected += async(ChatUser user) =>
            {
                NetworkStream nwStream = user.Client.GetNetworkStream();
                user.Client.ConnectionInfo.Status = ConnectionInfo.ConnectionStatus.Connected;

                Logger.Instance.WriteLine($"Client [ID: {user.Client.ConnectionInfo.ID} Name: {user.Username}] connected...");
                server.Clients.Add(user);

                string      msg         = $"User {user.Username} joined the server.";
                ChatMessage chatMessage = new ChatMessage(server.ServerUser, msg)
                {
                    MessageType = ChatMessageType.Connection
                };

                await server.SendMessageToAllClients(chatMessage);

                await server.ListenTo(user); //listen to messages from this client
            };

            server.OnClientDisconnected += async(ChatUser user) =>
            {
                string msg = null;
                if (user.Client.ConnectionInfo.Status == ConnectionInfo.ConnectionStatus.Kicked)
                {
                    msg = $"User {user.Username} was kicked by the server.";
                }
                else
                {
                    msg = $"User {user.Username} left the server.";
                }

                ChatMessage chatMessage = new ChatMessage(server.ServerUser, msg)
                {
                    MessageType = ChatMessageType.Connection
                };

                Logger.Instance.WriteLine($"Client [ID: {user.Client.ConnectionInfo.ID} Name: {user.Username}] disconnected...");
                server.Clients.Remove(user);
                user.Client.TcpClient.Dispose();
                user = null;

                try
                {
                    await server.SendMessageToAllClients(chatMessage);
                }
                catch (Exception e)
                {
                    Logger.Instance.WriteLine(e.Message);
                }
            };
            Logger.Instance.WriteLine($"Starting server at {serverip}:{port}");
            server.StartListening();
            AcceptClients();
            while (true)
            {
                string text = Console.ReadLine();
                ServerCommands.ExecuteCommand(text);
            }
        }