public void run() { String UserName; String RoomName; ClientThread client; UTF8Socket sock; Socket connection; Socket server; Thread thread; server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint address = new IPEndPoint(IPAddress.Parse(ipAddress), 9001); server.Bind(address); server.Listen(10); while (true) { connection = server.Accept(); sock = new UTF8Socket(connection); RoomName = sock.ReceiveMessage(); Console.WriteLine("Roomname = " + RoomName); if (!RoomMap.ContainsKey(RoomName)) { sock.SendMessage("REJECTED"); sock.close(); continue; } else { sock.SendMessage("ACCEPTED"); } UserName = sock.ReceiveMessage(); Console.WriteLine("UserName = "******"REJECTED"); sock.close(); continue; } else { sock.SendMessage("ACCEPTED"); client = new ClientThread(room, sock, UserName); room.AddClient(client); thread = new Thread(client.run); thread.Start(); room.AddMessage("server", "<" + UserName + " has entered the room>"); } } }
public void AddClient(ClientThread user) { Monitor.Enter(NameThreadMap); NameThreadMap.Add(user.name, user); Monitor.Exit(NameThreadMap); }