Beispiel #1
0
        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 run()
        {
            try{
                sock = new UTF8Socket(ip);
            }
            catch (SocketException e) {
                gui.DisplayMessage("Error: could not connect to server. Please restart program.");
                return;
            }

            String response;

            sock.SendMessage(chatroom);
            response = sock.ReceiveMessage();
            if (response != "ACCEPTED")
            {
                Console.WriteLine("chatroom rejected.");
                throw new RoomNameException("Room " + chatroom + " does not exist. Please restart the program");
            }

            sock.SendMessage(username);
            response = sock.ReceiveMessage();

            if (response != "ACCEPTED")
            {
                Console.WriteLine("username rejected");
                throw new UserNameException("Username " + username + " is already taken. Please restart the program");
            }

            String message;

            Console.WriteLine("About to enter main loop");
            Console.WriteLine("Just entered main loop");

            // The main loop
            while (true)
            {
                try
                {
                    message = sock.ReceiveMessage();
                }
                catch (Exception e)
                {
                    gui.DisplayMessage("Connection to server has been broken. Please make sure server is running and restart this program.");
                    return;
                }
                Console.WriteLine("Just received message " + message);
                gui.DisplayMessage(message);
            }
        }
Beispiel #3
0
        public void run()
        {
            String message;

            while (true)
            {
                try{
                    message = sock.ReceiveMessage();
                    room.AddMessage(name, name + ": " + message);
                }
                catch (Exception e) {
                    Console.WriteLine("About to call room.RemoveClient for " + name);
                    room.RemoveClient(name);
                    return;
                }
            }
        }