Hey i am trying to pass an object array from a client to server.....The problem is ,after passing it through the socket when i try to cast it back to client it gives me an exception
"Exception in thread "main" java.lang.ClassCastException: [Ljavaapplication28.Client; cannot be cast to javaapplication28.Client
"
And i fail to understand what is wrng...Plz Help
The code for Client is
and the code for server is/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package javaapplication28; import java.net.*; import java.io.*; public class Client implements Serializable { int value; String id; private Client[] Array; public Client[] getArray() { for (int i = 0 ; i < 5 ; i ++){ Array[i].id = id; Array[i].value = value; } return Array; } public Client(int x , String putInId) { id = putInId ; value = x; } Client(){ System.out.println("I am in constr"); value = 1; id = "my program is working"; } public static void main(String args[]) { try{ Socket accept_Socket = new Socket("127.0.0.1",1254); OutputStream through_data = accept_Socket.getOutputStream(); // getoutput returns the output stream where the data can be written ObjectOutputStream new_Object = new ObjectOutputStream(through_data); Client [] sendData = new Client [5]; System.out.println("The Client Side" + sendData); String putInId = "1 + "; int x = 1; for (int i = 0 ; i < 5; i++) { sendData[i] = new Client (x,putInId); } new_Object.writeObject(sendData); System.out.println("1"); new_Object.close(); System.out.println("2"); through_data.close(); System.out.println("3"); //accept_Socket.close(); } catch(Exception e) { System.out.println(e); System.out.println("4"); } } }
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package javaapplication28; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.Serializable; import java.net.ServerSocket; import java.net.Socket; /** * * @author Administrator */ public class Main implements Serializable { Main(){ int value = 1; String id = "Sana"; } public void driver() throws IOException, ClassNotFoundException { ServerSocket socket = new ServerSocket(1254); Socket accept_Socket = socket.accept(); InputStream getData = accept_Socket.getInputStream(); ObjectInputStream ois = new ObjectInputStream(getData); Client y = null; y = (Client) ois.readObject(); Client [] check = new Client [5]; check = y.getArray(); //check [] = (Client) ois.readObject(); // check[] = ois.readObject[]; for(int i = 0 ; i<5 ; i++) { System.out.println("The Server Side" + check[i].id); } // socket.close(); accept_Socket.close(); ois.close(); } public static void main(String[] args) throws IOException, ClassNotFoundException { Main calling = new Main(); calling.driver(); } }