Exemplo n.º 1
0
        public static void Main(string[] arg)
        {
            if (arg.Length != 2)
            {
                Console.WriteLine("Usage: ChatServer <port> <num_room>");
                return;
            }

            try
            {
                ChatServer cs = new ChatServer(int.Parse(arg[0]), int.Parse(arg[1]));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            while (true)
            {
                Console.Write("Your alias: ");
                _name = Console.ReadLine();

                if (string.IsNullOrEmpty(_name))
                {
                    Console.WriteLine("Your name can not be empty.");
                }
                else
                {
                    break;
                }
            }

            var choice = -1;

            while (true)
            {
                Console.Write("Are you a (1)client or a (2)server? ");

                var input = Console.ReadLine();

                if (!int.TryParse(input, out choice))
                {
                    Console.WriteLine("Invalid choice, try again.");

                    continue;
                }

                break;
            }

            switch (choice)
            {
            case 1:                     // Is a client
            {
                while (true)
                {
                    string ip;

                    Console.Write("IP: ");
                    ip = Console.ReadLine();

                    try
                    {
                        _meTcpClient = new TcpClient(ip, 5555);
                        _me          = new ChatClient(_meTcpClient, _name);

                        break;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }

                new Thread(delegate()
                    {
                        while (true)
                        {
                            _me.ReadMessage();
                        }
                    }).Start();

                while (true)
                {
                    Console.Write("Say: ");
                    var msg = _me.Name + "\n" + Console.ReadLine();

                    var bMsg = Encoding.ASCII.GetBytes(msg);

                    _meTcpClient.GetStream().Write(bMsg, 0, bMsg.Count());
                }

                break;
            }

            case 2:                     // Is a server
            {
                var server = new ChatServer(5555);
                server.StartListening();

                while (server.Listening)
                {
                    server.Receive();
                    server.Send();
                }

                break;
            }
            }
        }
Exemplo n.º 3
0
        // Occures when a new client is accepted
        private void AcceptClient()
        {
            srReceiver = new System.IO.StreamReader(tcpClient.GetStream());
            swSender   = new System.IO.StreamWriter(tcpClient.GetStream());

            // Read the account information from the client
            currUser = srReceiver.ReadLine();

            // We got a response from the client
            if (currUser != "")
            {
                // Store the user name in the hash table
                if (ChatServer.htUsers.Contains(currUser) == true)
                {
                    // 0 means not connected
                    swSender.WriteLine("0|This username already exists.");
                    swSender.Flush();
                    CloseConnection();
                    return;
                }
                else if (currUser == "Administrator")
                {
                    // 0 means not connected
                    swSender.WriteLine("0|This username is reserved.");
                    swSender.Flush();
                    CloseConnection();
                    return;
                }
                else
                {
                    // 1 means connected successfully
                    swSender.WriteLine("1");
                    swSender.Flush();

                    // Add the user to the hash tables and start listening for messages from him
                    ChatServer.AddUser(tcpClient, currUser);
                }
            }
            else
            {
                CloseConnection();
                return;
            }

            try
            {
                // Keep waiting for a message from the user
                while ((strResponse = srReceiver.ReadLine()) != "")
                {
                    // If it's invalid, remove the user
                    if (strResponse == null)
                    {
                        ChatServer.RemoveUser(tcpClient);
                    }
                    else
                    {
                        // Otherwise send the message to all the other users
                        ChatServer.SendMessage(currUser, strResponse);
                    }
                }
            }
            catch
            {
                // If anything went wrong with this user, disconnect him
                ChatServer.RemoveUser(tcpClient);
            }
        }
Exemplo n.º 4
0
 public ChatServer()
 {
     _instance = this;
 }
Exemplo n.º 5
0
 //Constructor to initialized reference server and client connection
 public AuthenticationServer(ChatServer s, TcpClient c)
 {
     chatserver = s;
     client     = c;
 }
Exemplo n.º 6
0
        static void Main()
        {
            Console.WriteLine("Application start...[ThreadID:{0}]", Thread.CurrentThread.ManagedThreadId);

            string app_identifier = "chat";
            string server_ip      = "127.0.0.1";
            int    server_port    = 11515;
            int    max_client     = 100;

            ChatClient client = new ChatClient();

            client.Init(app_identifier);

            ChatServer server = new ChatServer();

            server.Init(app_identifier, server_port, max_client);

            while (true)
            {
                Console.Write("command> ");
                string line = Console.ReadLine();
                if (line == "quit")
                {
                    client.Close();
                    server.Stop();
                    break;
                }
                else if (line == "start server")
                {
                    server.Start();
                }
                else if (line == "connect")
                {
                    client.Connect(server_ip, server_port);
                }
                else if (line.IndexOf("login") >= 0)
                {
                    string[] token = line.Split(' ');
                    if (token.Length >= 2)
                    {
                        client.Login(token[1]);
                    }
                    else
                    {
                        Console.WriteLine("Invalid command.");
                    }
                }
                else if (line.IndexOf("logout") >= 0)
                {
                    client.Logout();
                }
                else if (line.IndexOf("send") >= 0)
                {
                    string[] token = line.Split(' ');
                    if (token.Length == 3)
                    {
                        client.Send(token[1], token[2]);
                    }
                    else
                    {
                        Console.WriteLine("Invalid command.");
                    }
                }
                else
                {
                    if (line.Length > 0 && client.IsLogin())
                    {
                        client.SendToAll(line);
                    }
                }
            }

            Console.WriteLine("Application is quitted!");
            Console.Read();
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            while (true)
            {
                Console.Write("Your alias: ");
                _name = Console.ReadLine();

                if (string.IsNullOrEmpty(_name))
                {
                    Console.WriteLine("Your name can not be empty.");
                }
                else
                {
                    break;
                }
            }

            var choice = -1;

            while (true)
            {
                Console.Write("Are you a (1)client or a (2)server? ");

                var input = Console.ReadLine();

                if (!int.TryParse(input, out choice))
                {
                    Console.WriteLine("Invalid choice, try again.");

                    continue;
                }

                break;
            }

            switch (choice)
            {
                case 1: // Is a client
                    {
                        while (true)
                        {
                            string ip;

                            Console.Write("IP: ");
                            ip = Console.ReadLine();

                            try
                            {
                                _meTcpClient = new TcpClient(ip, 5555);
                                _me = new ChatClient(_meTcpClient, _name);

                                break;
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e.Message);
                            }
                        }

                        new Thread(delegate()
                                   	{
                                        while (true)
                                        {
                                            _me.ReadMessage();
                                        }
                                   	}).Start();

                        while (true)
                        {
                            Console.Write("Say: ");
                            var msg = _me.Name + "\n" + Console.ReadLine();

                            var bMsg = Encoding.ASCII.GetBytes(msg);

                            _meTcpClient.GetStream().Write(bMsg, 0, bMsg.Count());
                        }

                        break;
                    }
                case 2: // Is a server
                    {
                        var server = new ChatServer(5555);
                        server.StartListening();

                        while (server.Listening)
                        {
                            server.Receive();
                            server.Send();
                        }

                        break;
                    }
            }
        }
Exemplo n.º 8
0
        static void Main()
        {
            Console.WriteLine("Application start...[ThreadID:{0}]", Thread.CurrentThread.ManagedThreadId);

            string app_identifier = "chat";
            string server_ip = "127.0.0.1";
            int server_port = 11515;
            int max_client = 100;

            ChatClient client = new ChatClient();
            client.Init(app_identifier);

            ChatServer server = new ChatServer();
            server.Init(app_identifier, server_port, max_client);
            
            while (true)
            {
                Console.Write("command> ");
                string line = Console.ReadLine();
                if (line == "quit")
                {
                    client.Close();
                    server.Stop();
                    break;
                }
                else if (line == "start server")
                {
                    server.Start();
                }
                else if (line == "connect")
                {
                    client.Connect(server_ip, server_port);
                }
                else if (line.IndexOf("login") >= 0)
                {
                    string[] token = line.Split(' ');
                    if (token.Length >= 2)
                    {
                        client.Login(token[1]);
                    }
                    else
                    {
                        Console.WriteLine("Invalid command.");
                    }
                }
                else if (line.IndexOf("logout") >= 0)
                {
                    client.Logout();
                }
                else if (line.IndexOf("send") >= 0)
                {
                    string[] token = line.Split(' ');
                    if (token.Length == 3)
                    {
                        client.Send(token[1], token[2]);
                    }
                    else
                    {
                        Console.WriteLine("Invalid command.");
                    }
                }
                else
                {
                    if (line.Length > 0 && client.IsLogin())
                    {
                        client.SendToAll(line);
                    }
                }
            }

            Console.WriteLine("Application is quitted!");
            Console.Read();
        }