i need help

Reply

Join Date: Apr 2006
Posts: 1
Reputation: itstudent123 is an unknown quantity at this point 
Solved Threads: 0
itstudent123 itstudent123 is offline Offline
Newbie Poster

i need help

 
0
  #1
Apr 14th, 2006
please can you help me in this BankAccount code i do it but the teacher remove some my code and ask me when i see complete i complete but i when i do it i have some error can you help me i will post my code

the client code:


  1. import java.io.*;
  2. import java.net.*;
  3. public class Client{
  4. static Socket conn = null;
  5. static PrintWriter writer = null;
  6. static BufferedReader reader = null;
  7. // Method which connect the client to the service (identified by port)
  8. // provided by the server (identified by ipaddr).
  9. // If the connection is successful, it return true.
  10. public static boolean connect(String ipaddr, int port) {
  11. try { // Connect to server
  12. conn = new Socket(ipaddr, port);
  13. } catch (IOException e) {
  14. System.err.println("Cannot connect to " + ipaddr +"/"+ port);
  15. System.exit(1);
  16. }
  17. try { // Get a reading and writing channels to the connection.
  18. writer = new PrintWriter(conn.getOutputStream(), true);
  19. reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  20. } catch (IOException e) {
  21. System.err.println("Couldn't get reading/writing channels to socket");
  22. System.exit(1);
  23. }
  24. return true;
  25. }
  26. public static void main(String[] args) throws IOException {
  27. String request = null; // Used to send request messages to server
  28. String response = null;// Used to receive response messages from server
  29. // Get reading channel from standard input (i.e., keyboard)
  30. BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
  31. String command = "";
  32. boolean connected = false;
  33. while (command != null) {
  34. System.out.print("> ");
  35. command = keyboard.readLine();
  36. if (command.equals("connect")) {
  37. connected = connect(args[0], Integer.parseInt(args[1]));
  38. request = "hello";
  39. } else if (command.equals("disconnect")) {
  40.  
  41. // complete ...
  42. connected = false;
  43. } else if (command.equals("exit")) {
  44. // complete ...
  45. } else if (command.equals("balance")) {
  46. // complete ...
  47. } else if (command.startsWith("deposit")) {
  48. // complete ...
  49. } else if (command.startsWith("withdraw")) {
  50. // complete ...
  51. } else { // this is the case where the command entered is not valid
  52. // complete ...
  53. continue; // go back to the while loop. NOT to the if (connected) ...
  54. }
  55. // send request and read response (if we are connected)
  56. if (connected == true) { // do the following only if connected
  57. writer.println(request); // send the request prepared above
  58. response = reader.readLine(); // receive the response to it.
  59. System.out.println("\t"+response); // print out the response
  60. }
  61. }
  62. // close keyboard channel
  63. keyboard.close();
  64. }
  65. }

the server code:

  1. import java.net.*;
  2. import java.io.*;
  3. class Server{
  4. public static void main(String[] args) {
  5. int port = Integer.parseInt(args[0]);
  6. ServerSocket server;
  7. try {
  8. server = new ServerSocket(Integer.parseInt(args[0]));
  9. } catch (IOException ioe) {
  10. System.err.println("Couldn't run server on port " + port);
  11. return;
  12. }
  13. while(true) {
  14. try {
  15. Socket connection = server.accept();
  16. ConnectionHandler handler = new ConnectionHandler(connection);
  17. new Thread(handler).start();
  18. } catch (IOException ioe) {
  19. }
  20. }
  21. }
  22. }


the BankAccount Code:

  1. import java.net.*;
  2. import java.io.*;
  3. class Server{
  4. public static void main(String[] args) {
  5. int port = Integer.parseInt(args[0]);
  6. ServerSocket server;
  7. try {
  8. server = new ServerSocket(Integer.parseInt(args[0]));
  9. } catch (IOException ioe) {
  10. System.err.println("Couldn't run server on port " + port);
  11. return;
  12. }
  13. while(true) {
  14. try {
  15. Socket connection = server.accept();
  16. ConnectionHandler handler = new ConnectionHandler(connection);
  17. new Thread(handler).start();
  18. } catch (IOException ioe) {
  19. }
  20. }
  21. }
  22. }


the ConnectionHandler code:

  1. import java.net.*;
  2. import java.io.*;
  3.  
  4. class ConnectionHandler implements Runnable {
  5. // The connection with the client
  6. private Socket conn = null;
  7. PrintWriter writer = null;
  8. BufferedReader reader = null;
  9. String request = null;
  10. String response = null;
  11. BankAccount account = new BankAccount();
  12. public ConnectionHandler(Socket conn) {
  13. this.conn = conn;
  14. }
  15. public void run() {
  16. try {
  17. writer = new PrintWriter(conn.getOutputStream(), true);
  18. reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  19. String ipaddr = conn.getInetAddress().getHostName();
  20. int port = conn.getPort();
  21. System.out.println("Got connection from: " + ipaddr + ":" + port);
  22. while ((request = reader.readLine()) != null) {
  23. System.out.println(ipaddr + ":" + port + ": <-\t"+ request);
  24. if (request.equals("hello")) {
  25. response = "hello";
  26. } else if (request.equals("balance")) {
  27. response = Float.toString(account.balance());
  28. // complete...
  29. } else if (request.startsWith("deposit")) {
  30. // startsWith(): http://java.sun.com/j2se/1.4.2/docs/...va.lang.String)
  31. // split(): http://java.sun.com/j2se/1.4.2/docs/...va.lang.String)
  32. // trim(): http://java.sun.com/j2se/1.4.2/docs/...ring.html#trim()
  33. String amount = request.split(" ")[1].trim();
  34. // floatValue(): http://java.sun.com/j2se/1.4.2/docs/...va.lang.String)
  35. float f = Float.valueOf(amount).floatValue();
  36. response = Float.toString(account.deposit(f));
  37. } else if (request.startsWith("withdraw")) {
  38. // complete ...
  39. } else { // this is the case none of the above
  40. // complete ...
  41. }
  42. writer.println(response);
  43. System.out.println(ipaddr + ":" + port + ": ->\t"+ response);
  44. }
  45. } catch (IOException ioe) {
  46. }
  47. }
  48. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 763
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

Re: i need help

 
0
  #2
Apr 14th, 2006
Be specific in your questions. What error are you getting?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC