| | |
i need help
![]() |
•
•
Join Date: Apr 2006
Posts: 1
Reputation:
Solved Threads: 0
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:
the server code:
the BankAccount Code:
the ConnectionHandler code:
the client code:
Java Syntax (Toggle Plain Text)
import java.io.*; import java.net.*; public class Client{ static Socket conn = null; static PrintWriter writer = null; static BufferedReader reader = null; // Method which connect the client to the service (identified by port) // provided by the server (identified by ipaddr). // If the connection is successful, it return true. public static boolean connect(String ipaddr, int port) { try { // Connect to server conn = new Socket(ipaddr, port); } catch (IOException e) { System.err.println("Cannot connect to " + ipaddr +"/"+ port); System.exit(1); } try { // Get a reading and writing channels to the connection. writer = new PrintWriter(conn.getOutputStream(), true); reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); } catch (IOException e) { System.err.println("Couldn't get reading/writing channels to socket"); System.exit(1); } return true; } public static void main(String[] args) throws IOException { String request = null; // Used to send request messages to server String response = null;// Used to receive response messages from server // Get reading channel from standard input (i.e., keyboard) BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); String command = ""; boolean connected = false; while (command != null) { System.out.print("> "); command = keyboard.readLine(); if (command.equals("connect")) { connected = connect(args[0], Integer.parseInt(args[1])); request = "hello"; } else if (command.equals("disconnect")) { // complete ... connected = false; } else if (command.equals("exit")) { // complete ... } else if (command.equals("balance")) { // complete ... } else if (command.startsWith("deposit")) { // complete ... } else if (command.startsWith("withdraw")) { // complete ... } else { // this is the case where the command entered is not valid // complete ... continue; // go back to the while loop. NOT to the if (connected) ... } // send request and read response (if we are connected) if (connected == true) { // do the following only if connected writer.println(request); // send the request prepared above response = reader.readLine(); // receive the response to it. System.out.println("\t"+response); // print out the response } } // close keyboard channel keyboard.close(); } }
the server code:
Java Syntax (Toggle Plain Text)
import java.net.*; import java.io.*; class Server{ public static void main(String[] args) { int port = Integer.parseInt(args[0]); ServerSocket server; try { server = new ServerSocket(Integer.parseInt(args[0])); } catch (IOException ioe) { System.err.println("Couldn't run server on port " + port); return; } while(true) { try { Socket connection = server.accept(); ConnectionHandler handler = new ConnectionHandler(connection); new Thread(handler).start(); } catch (IOException ioe) { } } } }
the BankAccount Code:
Java Syntax (Toggle Plain Text)
import java.net.*; import java.io.*; class Server{ public static void main(String[] args) { int port = Integer.parseInt(args[0]); ServerSocket server; try { server = new ServerSocket(Integer.parseInt(args[0])); } catch (IOException ioe) { System.err.println("Couldn't run server on port " + port); return; } while(true) { try { Socket connection = server.accept(); ConnectionHandler handler = new ConnectionHandler(connection); new Thread(handler).start(); } catch (IOException ioe) { } } } }
the ConnectionHandler code:
Java Syntax (Toggle Plain Text)
import java.net.*; import java.io.*; class ConnectionHandler implements Runnable { // The connection with the client private Socket conn = null; PrintWriter writer = null; BufferedReader reader = null; String request = null; String response = null; BankAccount account = new BankAccount(); public ConnectionHandler(Socket conn) { this.conn = conn; } public void run() { try { writer = new PrintWriter(conn.getOutputStream(), true); reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String ipaddr = conn.getInetAddress().getHostName(); int port = conn.getPort(); System.out.println("Got connection from: " + ipaddr + ":" + port); while ((request = reader.readLine()) != null) { System.out.println(ipaddr + ":" + port + ": <-\t"+ request); if (request.equals("hello")) { response = "hello"; } else if (request.equals("balance")) { response = Float.toString(account.balance()); // complete... } else if (request.startsWith("deposit")) { // startsWith(): http://java.sun.com/j2se/1.4.2/docs/...va.lang.String) // split(): http://java.sun.com/j2se/1.4.2/docs/...va.lang.String) // trim(): http://java.sun.com/j2se/1.4.2/docs/...ring.html#trim() String amount = request.split(" ")[1].trim(); // floatValue(): http://java.sun.com/j2se/1.4.2/docs/...va.lang.String) float f = Float.valueOf(amount).floatValue(); response = Float.toString(account.deposit(f)); } else if (request.startsWith("withdraw")) { // complete ... } else { // this is the case none of the above // complete ... } writer.println(response); System.out.println(ipaddr + ":" + port + ": ->\t"+ response); } } catch (IOException ioe) { } } }
![]() |
Other Threads in the Java Forum
- Previous Thread: connecting java to mysql database
- Next Thread: cell phone
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application arguments array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class client code color component count database derby design eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui homework html ide if_statement image integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia keyword linux list macintosh map method methods midlethttpconnection mobile netbeans newbie nullpointerexception object open-source os problem producer program programming project projectideas property read recursion reference replaysolutions ria scanner search server set size sms sort sourcelabs splash sql sqlite stop string swing threads transforms tree ui unicode validation windows





