/// <summary> /// Handle messaging with the server. /// </summary> /// <param name="o"></param> static void ConnectionListener() { NetworkStream stream = client.GetStream(); byte[] buffer = new byte[client.ReceiveBufferSize]; try { while (!exit) { int bytesRead = stream.Read(buffer, 0, client.ReceiveBufferSize); if (bytesRead == 0) { break; } Message message = Message.FromBytes(buffer); Libs.DisplayMessage(message); } } catch (IOException e) { Libs.StatusMessage(e.Message, StatusType.FAILURE); } stream.Close(); Libs.StatusMessage("Connection lost with the server."); }
/// <summary> /// Set the color of the users messages. /// </summary> /// <param name="args"></param> static void CommandColor(string[] args) { if (args.Length > 0) { int newColor; if (int.TryParse(args[0], out newColor)) { if (newColor >= 0 && newColor < 16) { color = newColor; } else { Libs.StatusMessage("Color has to be between 0-15", StatusType.FAILURE); } } else { Libs.StatusMessage("Invalid argument: " + args[0], StatusType.FAILURE); } } else { Libs.StatusMessage("Invalid number of arguments!", StatusType.FAILURE); } }
/// <summary> /// Handle messaging with a single client. /// </summary> /// <param name="o"></param> static void ConnectionListener(object o) { int id = (int)o; TcpClient client; lock (_lock) client = clients[id]; NetworkStream stream = client.GetStream(); byte[] buffer = new byte[client.ReceiveBufferSize]; try { while (!exit) { int bytesRead = stream.Read(buffer, 0, client.ReceiveBufferSize); if (bytesRead == 0) { break; } ReceiveMessage(id, Message.FromBytes(buffer)); } } catch (IOException e) { Libs.StatusMessage(e.Message, StatusType.FAILURE); } Libs.StatusMessage("Lost connection with the client."); }
static void Main(string[] args) { Console.ForegroundColor = Libs.DEFAULT_COLOR; Console.WriteLine("CONSOLECHAT DEDICATED SERVER"); commands = new Dictionary <string, Command>(); commands.Add("exit", new Command(CommandExit)); listener = new TcpListener(IPAddress.Any, Libs.PORT); listener.Start(); Libs.StatusMessage("Server started."); inputThread = new Thread(InputHandler); inputThread.Start(); int count = 0; try { while (!exit) { TcpClient client = listener.AcceptTcpClient(); lock (_lock) clients.Add(count, client); Libs.StatusMessage("New connection from " + ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString()); Thread t = new Thread(ConnectionListener); listeners.Add(count, t); t.Start(count++); } } catch (SocketException e) { } }
/// <summary> /// Thread for handling inputs by the host. /// </summary> /// <param name="o"></param> static void InputHandler() { while (!exit) { InputParser(Console.ReadLine()); } Libs.StatusMessage("Input thread stopped."); }
/// <summary> /// Disconnect from the current server. /// </summary> /// <param name="args"></param> static void CommandDisconnect(string[] args) { if (!client.Connected) { Libs.StatusMessage("Not connected to any server!", StatusType.FAILURE); return; } Disconnect(); }
/// <summary> /// Disconnect a client from this server. /// </summary> /// <param name="client"></param> static void DisconnectClient(int id) { TcpClient client = clients[id]; lock (_lock) clients.Remove(id); client.Client.Shutdown(SocketShutdown.Both); listeners[id].Join(); client.Close(); Libs.StatusMessage("Client disconnected!"); }
/// <summary> /// Disconnect from the current server. /// </summary> static void Disconnect() { if (client == null || !client.Connected) { return; } client.Client.Shutdown(SocketShutdown.Send); client.Close(); Libs.StatusMessage("Disconnected!"); }
/// <summary> /// Close the server. /// </summary> static void Shutdown() { foreach (int id in clients.Keys) { DisconnectClient(id); } exit = true; listener.Stop(); Libs.StatusMessage("Shutting down..."); }
/// <summary> /// Tries to connect to a server. /// </summary> /// <param name="args"></param> static void CommandConnect(string[] args) { if (args.Length > 0) { string address = args[0]; Connect(address); } else { Libs.StatusMessage("Invalid number of arguments!", StatusType.FAILURE); } }
/// <summary> /// Try to connect to a server. /// </summary> /// <param name="address"></param> static void Connect(string address) { Libs.StatusMessage("Trying to connect to " + address + "..."); client = new TcpClient(); try { client.Connect(address, Libs.PORT); listener = new Thread(ConnectionListener); listener.Start(); Libs.StatusMessage("Connected!", StatusType.SUCCESS); } catch (Exception e) { Libs.StatusMessage("Connection failed!", StatusType.FAILURE); } }
/// <summary> /// Set the name of the user. /// </summary> /// <param name="args"></param> static void CommandName(string[] args) { if (args.Length > 0) { if (args[0].Length > 0) { name = args[0]; } else { Libs.StatusMessage("Name cannot be empty!", StatusType.FAILURE); } } else { Libs.StatusMessage("Invalid number of arguments!", StatusType.FAILURE); } }
static void Main(string[] args) { Console.ForegroundColor = Libs.DEFAULT_COLOR; Console.WriteLine("CONSOLECHAT CLIENT"); commands = new Dictionary <string, Command>(); commands.Add("exit", new Command(CommandExit)); commands.Add("connect", new Command(CommandConnect)); commands.Add("disconnect", new Command(CommandDisconnect)); commands.Add("name", new Command(CommandName)); commands.Add("color", new Command(CommandColor)); while (!exit) { InputParser(Console.ReadLine()); } Libs.StatusMessage("Shutting down..."); }