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(); }
static void Main(string[] args) { //IPAddress ip = IPAddress.Parse("127.0.0.1"); //int port = 5000; //TcpClient client = new TcpClient(); //client.Connect(ip, port); //Console.WriteLine("client connected!!"); //NetworkStream ns = client.GetStream(); //Thread thread = new Thread(o => ReceiveData((TcpClient)o)); //thread.Start(client); //string s; //while (!string.IsNullOrEmpty((s = Console.ReadLine()))) //{ // byte[] buffer = Encoding.ASCII.GetBytes(s); // ns.Write(buffer, 0, buffer.Length); //} //client.Client.Shutdown(SocketShutdown.Send); //thread.Join(); //ns.Close(); //client.Close(); //Console.WriteLine("disconnect from server!!"); //Console.ReadKey(); var client = new ChatClient(); client.ClientConnected += (obj, e) => Console.WriteLine($"Client Connected\nIP: {e.Client.IP}\nPort: {e.Client.Port}"); client.MessageReceived += (obj, e) => Console.WriteLine($"{e.Message} (from{e.Client.Username})"); client.ConnectToServer(IPAddress.Parse("127.0.0.1"), 5000); var username = Console.ReadLine(); var password = Console.ReadLine(); client.Login(username, password); client.Listen(); //var users = client.GetAllUsers(); var connectToUser = Console.ReadLine(); if (!string.IsNullOrEmpty(connectToUser)) { //var user = client.GetUser(connectToUser); client.ConnectToClient(connectToUser); } if (client.ConnectedClients.Count > 0) { client.SendToClient("Hi, User", client.ConnectedClients[0]); while (true) { string s = Console.ReadLine(); client.SendToClient(s, client.ConnectedClients[0]); } } //client.ListenThread.Join(); //client.SendThread.Join(); //client.ReceiveThread.Join(); //Task.WaitAll(); //Console.ReadKey(); }