File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Right Direction for Server Code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Right Direction for Server Code?" Watch "Right Direction for Server Code?" New topic
Author

Right Direction for Server Code?

Sue Hunt
Ranch Hand

Joined: Jan 26, 2004
Posts: 50
Hi again.
Right. The next question for this assignment. We have been given the code for the client class. We need to write the server class. The server class will hold a set of numerical data (int)

The client is set to produce a few sets of buttons, Largest, Smallest, Average and Quit. So depending upon which button is hit, the server will return the corresponding value, also the server needs to remain open, after the client quits, to await another client.
Do I need to put the value into a hashTable?
Am I heading in the right direction by assuming that in the client code where it says

That I use "largest" within the server code.
Am I even making any sense.
Off to start coding...
Stand by if you dare... :roll:
Sue
eammon bannon
Ranch Hand

Joined: Mar 16, 2004
Posts: 140

Am I even making any sense.

Sort of - but I think we could do with a little more detail. Particularaly:
1. when you say server, what do you mean? Is this a to be a servlet? Some class accessed over RMI? Or are you just using server as a way of describing a class which holds data and your client calls methods on to get that data?
2. What's "bf"? Is this an instance of your "server", or some other class? And "readLine"?
Let us know and we'll see what we can do.
Sue Hunt
Ranch Hand

Joined: Jan 26, 2004
Posts: 50
Eammon you are always one of the quickest to reply *grin*.
I am probably using or not using (as the case may be) the correct terminology..but then that's nothing new *grin*
An excerpt from the question is "...want you to develop a server which sends back the average, largest or smallest from set of data that it stores...server should also respond from a quit message from client..."
"develop a server which interacts with client"
Is that enough info, or do you want more...
Must admit, everyone seems to have the patience of a saint in here
sorry bf is an object...Private bufferedReader bf;
Sue
[ March 19, 2004: Message edited by: Sue Hunt ]
eammon bannon
Ranch Hand

Joined: Mar 16, 2004
Posts: 140
Ok, you can use a BufferedReader to read arrays as an input stream - which is what you've got with your data array. So I'm presuming you need a class (the server) which runs all the time with an access method so clients can request it stream the data array to them? If this is right, I'd suggest you start to look at the java.io package, and in particular the Readers and Writers avaliable in there and how they are used.
Sue Hunt
Ranch Hand

Joined: Jan 26, 2004
Posts: 50
Hi.
Just wondering, if I am on the right track. I know it's messy, compiling kinda ok...
Been looking at "Big Java" and the API... Going to look at Ivor Horton's when I get home to figure out how to use anonymous arrays.

Thanks for kindness and understandings...
eammon bannon
Ranch Hand

Joined: Mar 16, 2004
Posts: 140
Looks good - and makes more sense now.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Server socket stuff is great fun. Looks like your client has logic like:

Is that close? If so, your server looks pretty close, too. You have "return sum/dataSize", tho. That will return from the main() method which probably isn't what you want (probably doesn't compile!) You have an object that you can use to send the reply back to the client. (I'm trying hard not to say it!)
And look into your average calculation. dataSize is the number of ints that we're averaging. Instead of init to zero and adding 1 with ++ see if you can get the number of items in the array from the array itself. Hmmm, I'm not sure where to look. int[] is not in the Javadoc, is it. See if your text or classwork has some examples of looping through arrays.
Hope you're having big grins while digging into this stuff. Post again if you get a little further!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Sue Hunt
Ranch Hand

Joined: Jan 26, 2004
Posts: 50
I keep saying it, but I do appreciate the help you guys give. You rightly so, make the grey matter work, instead of giving the answers away.
I've managed to compile this without any compiling errors, although I haven't tried it yet.
Does it look as though I am on the right track?

If so, I just now need to pick the smallest and largest. Would I be right in assuming that as I have sorted the array out, I just need to figure out how to call the first slot and then the last slot?
Cheers
Sue
Sue Hunt
Ranch Hand

Joined: Jan 26, 2004
Posts: 50
Well, I have it up and running. The client class is showing a window where you can ask for largest, smallest, average and quit.
However, all I keep getting back from the server is the average.
I am at a lost as to why it's doing that...

Also, after I put the first request in, the server window is telling me that I have an IndexOutofBoundArray error.
Thanks for all help.
Cheers
Sue
[ March 21, 2004: Message edited by: Sue Hunt ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
There's a very common trap in your "L" for largest method ... the length of an array is, say, 5. But the highest index you can use is 4 cause they start counting at 0. Zero based indexing started back with assembler language guys who were working with the offset from the start of an array. The first entry is 0 times entry length from the start. Good for machine language, bad for humans. I guess we just have to get used to it. Anyhow, throw a "-1" in there when you use j for an index.
Sue Hunt
Ranch Hand

Joined: Jan 26, 2004
Posts: 50
Thanks Stan.
That seemed to have solved the Out of Bound error.
When I run the client, after starting the NumberServer, the following happens.
When I click on the Average Button, it may or may not come up with the average on the first click, it may select the largest, the smallest or the average. So by clicking on it 3 times, it rotates through the different possible answers.
The same happens if I click the Smallest Button or the Largest Button.
Any ideas?
Cheers
Sue
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Right Direction for Server Code?