Results 1 to 2 of 2
  1. #1
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Need help with sending an object through client and server

    Hi I am having trouble completing a client/server program than send through a bank account object and then adds $100. The bank account is to have an ID that is only printed on the client end before sending and is then zeroed out. This is what i have so far and these are the errors:

    Java Code:
    public class BankAccount{
    	
    	//attributes
    	double amount;
    	String name;
    	private int id;
    	double newAmount;
    	
    	public BankAccount(double _amount, String _name, int _id){
    		amount = _amount;
    		name = _name;
    		id = _id;
    	}
    	
    	public void modifyAmount(double _newAmount){
    		newAmount = _newAmount;
    		amount += newAmount;
    	}
    	
    	public String toString(){
    		return "Name: "+ name+" ID: "+id+" Current Amount: $"+amount;
    	}
    }
    Java Code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    public class MyClient{
    	
    	private ArrayList<BankAccount> bankList = new ArrayList<BankAccount>();
    	private Socket s;
    
    	public MyClient(String[] args){
    		//create server connection
    		try{
    			BankAccount ba1 = new BankAccount(100, "Jeff", 1234);
    			BankAccount ba2 = new BankAccount(5, "Dianne", 3456);
    			BankAccount ba3 = new BankAccount(20000, "Lynne", 7890);
    		
    			bankList.add(ba1);
    			bankList.add(ba2);
    			bankList.add(ba3);
    			InetAddress host = InetAddress.getByName(args[0]);
    			int port = Integer.parseInt(args[1]);
    			Socket s = new Socket(host, port);
    			
    			//Send objects to server
    			ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
    			oos.writeObject("Sending: "+ba1.toString());
    			oos.writeObject("Sending: "+ba2.toString());
    			oos.writeObject("Sending: "+ba3.toString());
    			
    			oos.close();
    			
    			//recieve and print objects from server
    			ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
    			ois.readObject();
    			
    			ois.close();
    		}
    		catch(UnknownHostException uhe){
    			System.out.println("no host");
    		}
    		catch(ConnectException ce){
    			System.out.println("Server not available");
    		}
    		catch(IOException ioe){
    			System.out.println("IO Exception");
    		}
    		catch(ClassNotFoundException cnfe){
    			System.out.println("Class not found");
    		}
    	}//end MyClient
    	
    	
    	
    	public static void main(String [] args){
    		MyClient mc = new MyClient(args);
    	}
    }
    Java Code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    public class MyServer{
    
    	private ArrayList<BankAccount> bankList = new ArrayList<BankAccount>();
    
    	public static void main(String [] args){
    		new MyServer(args);
    	}
    	
    	public MyServer(String[] args){
    		ServerSocket ss = null;
    		
    		try{
    			ss = new ServerSocket(Integer.parseInt(args[0]));
    			System.out.println("*****server is running*****");
    			Socket cs = null;
    			
    			while (true){
    				cs = ss.accept();
    				ReceivedThread rt = new ReceivedThread(cs);
    				rt.start();
    				SendThread st = new SendThread(cs);
    				st.start();
    			}
    		}
    		catch(BindException be){
    			System.out.println("Server is already running on this computer");
    		}
    		catch(IOException ioe){
    				System.out.println("IO error");
    				ioe.printStackTrace();
    		}
    	}//end MyServer
    	
    	//Recieve object from client
    	class ReceivedThread extends Thread{
    		Socket cs;
    		ObjectInputStream ois;
    		ArrayList <BankAccount> al = new ArrayList();
    		
    		public ReceivedThread(Socket cs){
    			this.cs = cs;
    		}
    		
    		public void run(){
    			try{
    				ois = new ObjectInputStream(cs.getInputStream());
    				al.add((BankAccount)ois.readObject());
    				
    				ois.close();
    				
    				System.out.println(al.get(0).toString());
    				System.out.println(al.get(1).toString());
    				System.out.println(al.get(2).toString());
    
    				al.get(0).modifyAmount(100);
    				al.get(1).modifyAmount(100);
    				al.get(2).modifyAmount(100);
    							}
    			catch(SocketException se){
    				System.out.println("Client disconnected");
    			}
    			catch(IOException ioe){
    				System.out.println("IO Exception");
    			}
    			catch(ClassNotFoundException cnfe){
    				System.out.println("Class not found");
    			}
    		}
    	}//end recieved thread
    	
    	//Send object back to client
    	class SendThread extends Thread{
    		Socket cs;
    		ObjectOutputStream oos;
    		ArrayList <BankAccount> al = new ArrayList();
    		
    		public SendThread(Socket cs){
    			this.cs = cs;
    		}
    		
    		public void run(){
    			try{
    				oos = new ObjectOutputStream(cs.getOutputStream());
    				oos.writeObject(al.get(0));
    				oos.writeObject(al.get(1));
    				oos.writeObject(al.get(2));
    				
    				oos.close();
    				
    				System.out.println(al.get(0).toString());
    				System.out.println(al.get(1).toString());
    				System.out.println(al.get(2).toString());
    
    				
    			}
    			catch(SocketException se){
    				System.out.println("Client disconnected");
    			}
    			catch(IOException ioe){
    				System.out.println("IO Exception");
    			}
    		}
    	}//end thread
    }
    errors:



    C:\Users\Nick\Desktop\>java MyServer 16789
    *****server is running*****
    Exception in thread "Thread-1" java.lang.IndexOutOfBoundsException: Index: 0, Si
    ze: 0
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at MyServer$SendThread.run(MyServer.java:94)
    Exception in thread "Thread-0" java.lang.ClassCastException: java.lang.String ca
    nnot be cast to BankAccount
    at MyServer$ReceivedThread.run(MyServer.java:57)

    C:\Users\Nick\Desktop\programming labs\Lab7>

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,184
    Rep Power
    20

    Default Re: Need help with sending an object through client and server

    Can you highlight the exact lines throwing these two excptions?
    In your send, you have an empty ArrayList, hence IndexOutOfBounds.

    Can't quite tell where the receive error is coming from.

    Actually, scratch that..you are sending Strings...not BankAccounts.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. error in sending file from client to server
    By shwetasapare in forum AWT / Swing
    Replies: 0
    Last Post: 03-25-2011, 08:48 PM
  2. Sending file from client to server
    By kaijeong in forum New To Java
    Replies: 50
    Last Post: 07-29-2010, 08:01 PM
  3. sending byte from server to C client
    By rnvrnv in forum Networking
    Replies: 11
    Last Post: 07-02-2010, 04:20 PM
  4. Sending Text File --- Server-To-Client
    By nigamsir in forum Networking
    Replies: 1
    Last Post: 03-08-2010, 04:45 PM
  5. sending jar files from client to server?
    By gobinathm in forum New To Java
    Replies: 2
    Last Post: 11-13-2007, 06:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •