• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Right Direction for Server Code?

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks good - and makes more sense now.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Sue Hunt
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic