public static void Main(string[] args) { chatrooms = new List<Chatroom> (); users = new List<User> (); Console.WriteLine ("Please enter the port for the server: "); string port = Console.ReadLine (); TcpListener serverSocket = new TcpListener (IPAddress.Loopback, Convert.ToInt32 (port)); try { serverSocket.Start (); Console.WriteLine (String.Format ("[Server started on 127.0.0.1 {0}]", port)); } catch { Console.WriteLine ("[Server failed to start]"); return; } int clientIds = 1; // create a new client when a new connection comes in, and create user entry for that client connection while (true) { TcpClient client = serverSocket.AcceptTcpClient (); User user = new User (client, clientIds); users.Add (user); clientIds++; Thread t = new Thread (new ParameterizedThreadStart (HandleClient)); t.Start (user); } }
/// <summary> /// Add a message to the chatroom, and send it to all users /// </summary> /// <param name="s">S: the message to be added to the chatroom</param> public void AddMessage(User u, string s) { StreamWriter w = new StreamWriter (u.Client.GetStream (), Encoding.ASCII); // if the user is not actually in this chatroom, they can't add a message to it if (!Users.Contains(u)) { w.WriteLine (" warning: cannot send message, you must join a chatroom to send a message"); w.Flush (); return; } String message = String.Format ("[{0}] <{1}> {2}", this.Name, u.Name, s); Messages.Add(message); Date = DateTime.Now; foreach (User r in Users) { if (r.Id == u.Id) { continue; } w = new StreamWriter (r.Client.GetStream (), Encoding.ASCII); w.WriteLine (message); w.Flush (); } }
/// <summary> /// Sends a message to the specified chatroom /// </summary> /// <param name="user">User.</param> /// <param name="chatroom">Chatroom.</param> /// <param name="message">Message.</param> private static void SendMessage(User user, string chatroom, string message) { chatroom = chatroom.Substring (2); try { Chatroom c = chatrooms.First (r => r.Name.CompareTo (chatroom) == 0) as Chatroom; c.AddMessage(user, message); } catch { StreamWriter w = new StreamWriter (user.Client.GetStream (), Encoding.ASCII); w.WriteLine (" error: cannot send message, the specified chatroom cannot be found"); w.Flush (); } }
/// <summary> /// Remove the specified user from the specified chatroom /// </summary> /// <param name="user">User.</param> /// <param name="chatroom">Chatroom.</param> private static void LeaveChatroom(User user, string chatroom) { StreamWriter w = new StreamWriter (user.Client.GetStream (), Encoding.ASCII); try { Chatroom c = chatrooms.First (r => r.Name.CompareTo (chatroom) == 0) as Chatroom; c.LeaveChatroom(user); } catch { w.WriteLine (" error: cannot leave chatroom, the specified chatroom cannot be found"); w.Flush (); } }
/// <summary> /// Add the specified user to the list of users currently in this chatroom /// </summary> /// <param name="u">U.</param> public void JoinChatroom(User u) { StreamWriter w = new StreamWriter (u.Client.GetStream (), Encoding.ASCII); // if the user is actually in this chatroom, don't re-add them if (Users.Contains(u)) { return; } Users.Add (u); w.WriteLine (String.Format (" [{0}] joined", this.Name)); w.Flush (); foreach (string s in Messages) { w.WriteLine (s); w.Flush (); } }
/// <summary> /// Remove the specified user from the list of users /// </summary> /// <param name="u">U.</param> public void LeaveChatroom(User u) { // if the user is not actually in this chatroom, we don't have to do anything if (!Users.Contains(u)) { return; } Users.Remove (u); StreamWriter w = new StreamWriter (u.Client.GetStream (), Encoding.ASCII); w.WriteLine (String.Format (" [{0}] left", this.Name)); w.Flush (); }