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

    Default [SOLVED] Sending a message to clients using RMI

    Hey guys.. me again.. lol

    I am having trouble sending messages to clients when they are received on the server.

    I need to be able to send a specific message to a certain channel, Hopefully to make it easier i have given my channels numbers i.e 1, 2, 3, 4 and it will send the message and the channel number the user is in.

    This is the code for getting the message and storing it in a tempmsg.
    Java Code:
    public void GetMsgsFromClient(String UserMsg, int ChannelNum) throws java.rmi.RemoteException
    {
        int MsgI = 1;
        while (MsgI < LoggedInCount)
        {
            if (LoggedChannels[MsgI] == ChannelNum)
            {
                tempmsg = UserMsg;
            }
        MsgI++;
        }
    
    }
    Just to let you know the LoggedChannels[] is actually an array that has all the users channel numbers in it so it should be able to search through and pick out the numbers it needs to send it to. (I think lmao)

    I actually think this could cause a problem if two messages was sent at the exact same time or would it just wait until its finished with the first one?

    Anyway from the temp message i am then using this code:
    Java Code:
        public String GetMsgsFromServer() throws java.rmi.RemoteException
        {
            return(tempmsg);
        }
    that goes through the RemoteServer interface and what i thought back to the client, unfortunately it didn't. lol

    Anyway any help would be great, maybe i have just confused you! lol

    --
    Chris

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

    Default

    Just to add I have actually made it somehow relay messages that the user had sent. For example Test would send a message of Hello and it would come back [Test]: Hello on Test's client but not on any of the others :(

    I really hope someone can help me with this because i have been trying different things for about 3 hours.

    Thanks :)
    -----
    Chris

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Honestly, I can't quite see what you are doing from the code snippets. Are you sending messages to the clients whenever they log in?

    Regardless, a socket or a Web service approach seem like a better fit. Also, a server does not typically push messages to a client, the client retrieves them from the server.

    If you would clarify your "business" goals, then we might be able to help you with your implementation...

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

    Default

    Sorry about that :p

    This is part of a chat room so them messages are being sent from other clients and what i hoped for was being broadcast to everyone in the channels.

    That bit of code is on the server and when it gets messages from any client it will get the channel number and then try to send the message to everyone in that channel using a loop.

    Would i be best making the client get the messages instead? And how would i make this work with different channels?

    Thanks :)
    -----
    Chris

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

    Default

    I have actually managed to sort of get this working, now the only problem i am having is that it will not get the messages from the server if its still waiting for user input. i.e. the users next messages.

    For example if i open a client that can send messages to the server and comment out the part that allows a user to send a message and open a client it will receive the messages i send from the first client.

    But it will not work if i let the users enter a message because it is waiting for the user input before it moves onto getting the messages from the server.

    I bet i have just confused you all, if i have i'm sorry :p

    Maybe i could solve this with a press enter to talk type of thing?

    Thanks,
    Chris
    -----
    Chris

  6. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Sounds like you are close to getting this working. I understand why you are using RMI now.

    I don't know how you are creating your clients, but if you are using Swing, then the event dispatcher thread displays the screen and responds to user input. Honestly, I don't know how RMI works, but I would expect it to somehow have its own thread. You should then use EventQueue.invokeLater() to update your UI from that thread.

    I'm not sure that will help, but...

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

    Default

    Thanks,

    I tried using threads but unfortunately it produces similar results.

    Java Code:
    public static String LastMsg = null;
    public static class GetMessages extends Thread
    {
        
            @Override
        public void run() {
                try {
                    String [] MsgBuffer = CS.GetMsgsFromServer();
                    if (MsgBuffer[CurrentChanNum] != null) {
                        if (MsgBuffer[CurrentChanNum].equals(UserMsg)){
                        //Makes it so messages aren't repeated back to the user who sent them.
                        } else {
                            if (MsgBuffer[CurrentChanNum].equals(LastMsg))
                            {
                            //Checks to make sure the message hasn't already been sent.
                            } else {
                                LastMsg = MsgBuffer[CurrentChanNum];
                                System.out.println(MsgBuffer[CurrentChanNum]);
                            }
                        }
                    }
                    
                } catch (IOException ex) {
                    Logger.getLogger(RMIClient.class.getName()).log(Level.SEVERE, null, ex);
                }
        }
    }
    
    public static class GetMsgFromUser extends Thread
    {
        @Override
        public void  run() {
                try {
                    System.out.println("Message To Channel: ");
                    BufferedReader Msg = new BufferedReader(new InputStreamReader(System.in));
    
                    UserMsg = Msg.readLine();
                    if (UserMsg.contains("/Join #") || UserMsg.contains("/join #"))
                    {
                        NxtCmd = UserMsg;
                        SCommands(NxtCmd);
                    } else if (UserMsg.equals("/ChannelList") || UserMsg.equals(("/channellist"))) {
                        NxtCmd = UserMsg;
                        SCommands(NxtCmd);
                    } else if (UserMsg.equals("/Quit") || UserMsg.equals("/quit")) {
                        NxtCmd = UserMsg;
                        SCommands(NxtCmd);
                    } else {
                    UserMsg = "[" + LoginName + "]:" + UserMsg;
    
                    CS.GetMsgsFromClient(UserMsg, CurrentChanNum);
                    }
                } catch (IOException ex) {
                    Logger.getLogger(RMIClient.class.getName()).log(Level.SEVERE, null, ex);
                }
        }
    }
    
    
    public static void main(String[] args) {
        
            try
            {
    
                CS=(RemoteServer) Naming.lookup("rmi://192.168.1.103/ChatServer");
    
                GetUsername();
                
                while(GetCmd == 1) {
                    GetUserCommand();
                }
    
                GetMsgFromUser GMFU = new GetMsgFromUser();
                GetMessages GetMsgsFS = new GetMessages();
                    while (GetMsgs == 1)
                        {
                            GetMsgsFS.run();
                            GMFU.run();
                        }
            }
            catch ( Exception e )
            {
                System.out.println("Exception: " + e.getMessage());
            }
        }
    That is the code for sending and receiving messages, However when i run it, it will just sit on the "Message to Channel:" even if another client is sending messages to the server :(

    Any ideas? Maybe I am working the Threads wrong and that's why it won't work?

    But yeah i want to thank all you guys/girls who have replied to me over the last week on this forum, id have never got this far without you. :D
    -----
    Chris

  8. #8
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    BufferedReader Msg = new BufferedReader(new InputStreamReader(System.in));
    You are reading from System.in, which I *think* takes input from the console. Unless you type something, that method will just loop, unless the read blocks. If the read blocks, then it will just sit.

    On the server, you need to poll your channels for input (a loop with Thread.sleep() that checks each channel every time around.) Every time you find input, you need to write that back out to everyone on that channel.

    I guess I'm confused on how the clients are sending you messages. System.in seems less than likely.

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

    Default

    Quote Originally Posted by Steve11235 View Post
    On the server, you need to poll your channels for input (a loop with Thread.sleep() that checks each channel every time around.) Every time you find input, you need to write that back out to everyone on that channel.
    I really don't know what you mean by that, I am completely new to java. I have not been taught any of this by my tutor and they basically expected us to know it.

    At the moment I have my client requesting the latest messages from the server which if I open a client to send messages and then comment out the sending part and run a second client i can send messages using the first client and they will be picked up on the second one.. But unfortunately the second one cannot send them.

    I am really lost with this one :confused:
    -----
    Chris

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

    Default

    It doesn't matter now :)

    I have run into my deadline for this work so I have had to just submit it like it is. 90% of the application works so I should be ok.

    Thanks to everyone who has helped me over the past week.
    -----
    Chris

  11. #11
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default Start with a piece of paper

    This is the most valuable lesson I have learned in 20 years of programming, no matter what language. Get a pad of paper (don't use a UML tool), preferably graph paper, and start by making a list of nouns in your system: clients, channel, message, etc. These will become your classes in a object oriented design.

    Draw boxes for the main nouns on a piece of paper and draw lines to represent relationships.

    Write each noun on a piece of paper and make two lists, one of attributes of that thing and a second list of what that thing can do. They become the class fields and methods.

    Don't go into too much detail. The goal is to really understand what you are doing. If this takes more than an hour, you probably have a complicated project, and then you *really* need to take the time to figure it out.

    Get used to creating a lot of small classes. One big class ends up creating big confusion. A lot of small classes let you get each one right and then move on.

    If you are not using an IDE (Netbeans or Eclipse), I *strongly* suggest doing so. It helps you keep everything organized, it will show you problems, even as you type (at least, Eclipse does), it will show you the structure of your classes, it will even make sure a change in on class is reflected in all the others, it will compile your code, it let you debug it, ...

    One last thing. Java requires a lot of learning. Once you know it, you can do little things very quickly, and you can take on very involved projects. Your chat project will require TCP/IP socket connections between the clients and the server, which you will have to manage. Polling means the server will have to check each client connection to see if the client has sent anything on an on-going basis. Java has some new channel objects that allow you to do this in an elegant fashion, but all communications programming is beyond the beginning level.

    Don't give up, but take the time to do a lot of reading on-line, starting with the API reference.

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

    Default

    Thanks very much, I think i have another module that will be using java on this semester coming up. So all the advice above I will try and do on my next project. :)
    -----
    Chris

Similar Threads

  1. multi clients connect to udp server
    By bachma7 in forum Networking
    Replies: 5
    Last Post: 12-12-2008, 08:56 PM
  2. SSL Server with authentication of clients
    By zoltan in forum Networking
    Replies: 0
    Last Post: 10-14-2008, 11:27 PM
  3. blocked between server and clients
    By ibtehal in forum Networking
    Replies: 6
    Last Post: 07-17-2008, 12:30 AM
  4. sending the message from trigger to mailid
    By geeta_ravikanti in forum JDBC
    Replies: 0
    Last Post: 04-04-2008, 11:09 AM
  5. Multple Clients
    By samson in forum Networking
    Replies: 1
    Last Post: 04-04-2007, 06:37 AM

Posting Permissions

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