Results 1 to 6 of 6
  1. #1
    ChrisMayhew is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default [SOLVED] Chat Application question

    Hello again :)

    Im still working on my chat application, So far i have managed to get it to ask the server for a list of users and a list of channels however I have run into an issue with once a user has done one command say /ShowUsers i made it ask for a second command but it will not show them. I know pretty much why and its because the if statements are above the second command.

    So my question is how do i make the client ask for a command and run the if statements then go back to asking for a command... If you get me

    I have attached my client code.. I have more than likely made it more complex than it needs to be but im very new to java to be taking on such a project. lol

    Java Code:
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.InetAddress;
    import java.rmi.*;
    //import java.rmi.server.RemoteServer;
    
    public class RMIClient {
    
        public static void main(String[] args) {
            try
            {
    
                RemoteServer CS=(RemoteServer) Naming.lookup("rmi://192.168.1.103/ChatServer");
                 
                System.out.println("Please enter a Username: ");
                BufferedReader Login = new BufferedReader(new InputStreamReader(System.in));
                String LoginName = null;
                LoginName = Login.readLine();
    
                InetAddress thisIp = InetAddress.getLocalHost();
                String IPAddress = thisIp.getHostAddress();
    
                CS.GetLoginName(LoginName, IPAddress);
    
                System.out.println("Command: ");
                BufferedReader Command = new BufferedReader(new InputStreamReader(System.in));
                String NxtCmd = null;
                NxtCmd = Command.readLine();
    
                if (NxtCmd.equals("/ShowUsers")) {
                    String [] OnlineUsers = CS.getUsersOnline();
                    int UOCount = OnlineUsers.length;
    
                    int CountUsers = 0;
                    int UserCount = 0;
                    while (CountUsers < UOCount) {
                        if (OnlineUsers[CountUsers] != null) {
                            UserCount++;
                        }
                    CountUsers++;
                    }
                    int i = 1;
                    System.out.println("Username \t Channel");
                    System.out.println("-------- \t ---------");
                    while (i <= UserCount) {
                        String [] temp = null;
                        temp = OnlineUsers[i].split(";");
                        //if (OnlineUsers[i] != null ) {
                            int ChannelI = 1;
                            int ChannelCount = CS.ChannelCount();
                            String [] ChatChannels = CS.getChannels();
    
                            int[] UserChanNum = CS.getUsersChannels();
                            String UserChan = null;
    
                            while(ChannelI <= ChannelCount) {
                                if (ChannelI == UserChanNum[ChannelI]) {
                                    UserChan = ChatChannels[ChannelI];
                                }
                            ChannelI++;
                            }
                            int NameLen = temp[0].length();
                            
                            if(UserChan == null) {
                            System.out.println(temp[0] + "\t" + "No Channel");
                            } else {
                            System.out.println(temp[0] + "\t" + UserChan);
                            }
                            UserChan = null;
                        //}
                    
                    i++;
                    }
                } else if (NxtCmd.equals("/ShowChannels")) {
                    int ChannelI = 1;
                    int ChannelCount = CS.ChannelCount();
                    String [] ChatChannels = CS.getChannels();
                    System.out.println("Channel   \t Online");
                    System.out.println("----------------   ---------");
                    while (ChannelI <= ChannelCount) {
                    System.out.println(ChatChannels[ChannelI]);
                    ChannelI ++;
                }
                }
                System.out.println("Command: ");
                NxtCmd = Command.readLine();
            }
            catch ( Exception e )
            {
                System.out.println("Exception: " + e.getMessage());
            }
        }
    
    }

    Any help would be great.
    I assume it has to go into its own class or something?

  2. #2
    ChrisMayhew is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default

    I have kinda figured out what i need to do i think. Within my client server code i created a SCommands(String NxtCmd) and copied all my if (NxtCmd) commands into that. However the CS.GetChannels etc get an error saying:

    cannot find symbol
    symbol: method getUsersOnline()
    location: class CS
    I assume this is because the code is now above the main or the CS storing the connection to the server isn't accessible to the SCommands() part.
    Any idea how i can make the CS available to everything within the code and not just main?

    Thanks

  3. #3
    ChrisMayhew is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default

    Just to update, playing about myself I have managed to make CS available to the whole code in the client so I have no separated my main functions like the commands to send to the server and getting the username from the user.

    The only thing I am stuck on if anyone can help is making it so that the server will keep asking the user for commands until the issue the command /Quit I have the code to quit already in place but I need it to keep asking the user for commands.

    Any help would be great!

    Thanks

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default while loop?

    Place the server's request code in a "while" loop?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    ChrisMayhew is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default

    Quote Originally Posted by CJSLMAN View Post
    Place the server's request code in a "while" loop?

    Luck,
    CJSL
    That worked perfect :) Thanks

    I don't know why I didn't think of that in the first place.. lol

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Any time...

    Welcome...

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. How to implement private chat application using jsp
    By PortalTeam in forum JavaServer Pages (JSP) and JSTL
    Replies: 9
    Last Post: 02-19-2010, 10:49 PM
  2. Multi Client/Server Chat Question
    By Kodak07 in forum Networking
    Replies: 3
    Last Post: 03-29-2009, 10:50 PM
  3. SCTP chat
    By rams in forum Networking
    Replies: 0
    Last Post: 10-23-2008, 09:48 AM
  4. Decentralized chat
    By KamilR in forum Networking
    Replies: 5
    Last Post: 05-05-2008, 07:27 AM
  5. Help in Chat application
    By zocky in forum Networking
    Replies: 0
    Last Post: 11-08-2007, 07:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •