[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.
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:
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
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.