I have to pass a String[] through a socket from an ObjectOutputStrem to an ObjectInputStrem but falls with an error:
Exception in thread "java.lang.ClassCastException" thread: java.net.SocketInputStream cannot be cast to java.io.ObjectInputStream
Why is it not clear not to be cast ... please help
Here is the client:
private void startClient() { this.socketAccept(); try (OutputStream out = this.socket.getOutputStream()) { String[] inSocket = {"abc", "mvd"}; this.getList((ObjectOutputStream) out, inSocket); } catch (IOException e) { e.printStackTrace(); } } private void getList(ObjectOutputStream out, String[] list) throws IOException { out.writeObject(list); } Here is the server it receives:
private void startServer() { this.initServerSocket(); System.out.println("wait connection..."); this.socketAccept(); System.out.println("connection accept"); try (InputStream in = this.socket.getInputStream()) { String[] res = this.getObj((ObjectInputStream) in); System.out.println(Arrays.toString(res)); } catch (IOException e) { e.printStackTrace(); } } private String[] getObj(ObjectInputStream objIn) throws IOException { try { return (String[]) objIn.readObject(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return new String[0]; }