Re: Client Server

From: Anthony Borla (ajborla_at_bigpond.com)
Date: 12/15/03


Date: Mon, 15 Dec 2003 22:07:04 GMT


"Andrew McGinley" <andrewmc110@hotmail.com> wrote in message
news:brku13$ol1$1@kermit.esat.net...
> Hi Everyone
>
> I am trying to develop a client/server application based on an
> ATM machine
>

Sounds like a fun project :)

>
> I understand how to send data to the server. It seems to be
> fine. However when the "Withdraw button is pressed "
> items from the client such as Account Number etc must be
> sent to the server.
>

Here, the client is sending the server a request, supplying it with all
relavant data required for it to meaningfully respond [i.e. the server will
perform some sort of data lookup, and respond appropriately].

You've, presumably, tested that this two-way data exchange can occur ? An
easy way to do this is to send the server some data and have it echoed back.
Knowing this works makes any futher problem solving much easier.

>
> The server then uses account number and a password to
> verify that the account is genuine. All this data is held in
> a file called code.
>

This file is, of course, located local to the server [i.e. on the same disk,
or an URL it can access, etc] ?

>
> However how do I read this data into the server in order to
> determine if the password and account number are genuine.
>

Normally you would expect such a file to be loaded when the server is first
loaded, or when specifically initialised [normally at 'first load' time, but
some designs allow initialisation at a later time]. Either way, you might
load it into a memory area like an array [if it isn't too large (i.e. not
thousands of entries)], or possibly into a single 'StringBuffer', for easy
searching. When a client request arrives [for validation, or data retrieval
etc], you'd search this array for the required data.

Alternatively, you might only load / read the file when needing to fulfil a
client request. This isn't a terribly efficient approach, but might be
acceptable if there aren't too many client requests, or there is little data
in the file.

In either case, you'd use one [or more] of the stream or reader classes to
access file data. Which one you'd use depends in what format the data was
stored in the first place. Possible scenarios:

* Data is grouped as a series of objects e.g.

       class Account
       {
          ...
          long accountNumber;
          String customerName;
          String password;
          ...
       }

   and is written to the file as objects using 'ObjectOutputStream'.
   You would then need to read it back in using 'ObjectInputStream'

* Data is grouped as described previously, but a 'DataOutputStream'
   was used to write it to the file; obviosuly a 'DataInputStream' is
   needed to read it back in

* Data may simply have been written out as plain text, that is,
   as a series of 'lines' [a 'line' being a series of human readable
   characters terminated by a newline character]. Here you might
   use a 'FileReader' wrapped up inside a 'BufferedReader'

>
> I presume string compare is needed but really confused.
>

Now, performing the search for a matching account number / password etc,
will vary depending on whether data has been loaded into an array or
'StringBuffer', or is being processed on a line-by-line basis from a reader
stream. In all cases, though, you would be attaempting to compare the data
item the client has sent against the 'search' item. Here is a partial, mock
example using an array of 'Account' objects:

    String password = getClientPassword();

    for (int i = 0; i < accountArray.length; ++i)
        if (password.compareTo(accountArray[i].getPassword()) == 0)
          // Found it !
          ...

>
> Any help would be greatly appreciated
>

I hope this helps.

Anthony Borla