title: ObjectOutputStream, ObjectInputStream. Cannot cast to ConcurrentHashMap
body: <p>My client sends a public key to the server. The server puts the public key and the client's ip address in a ConcurrentHashMap and returns the ConcurrentHashMap back to the client.
  The public key and client's ip is stored in <code>public ConcurrentHashMap&lt;Key,String&gt; list = new ConcurrentHashMap&lt;Key,String&gt;();</code>
  This has been implemented using ObjectOutputStream and ObjectInputStream. However I get an error in the line <code>list = (ConcurrentHashMap&lt;Key, String&gt;) oin.readObject();</code> as follows : <code>sun.security.rsa.RSAPublicKeyImpl cannot be cast to java.util.concurrent.ConcurrentHashMap</code>.</p>
  
  <p>Here is the Client code :</p>
  
  <pre><code>public class Send {
  private Key k;
  public Send() {
      try {
          this.k = generate();
      } catch (NoSuchAlgorithmException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  private Key generate() throws NoSuchAlgorithmException {
      // TODO Auto-generated method stub
      KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
      kpg.initialize(2048);
      KeyPair kpair = kpg.genKeyPair();
      return kpair.getPublic();
  }
  public static void send() throws IOException{
      Send s = new Send();
      ConcurrentHashMap&lt;Key,String&gt; list = new ConcurrentHashMap&lt;Key,String&gt;();
      try (SocketChannel channel = SocketChannel.open()) {
          InetSocketAddress local = new InetSocketAddress(5000);
          channel.bind(local);
          InetSocketAddress remote = new InetSocketAddress("192.168.0.101", 5000);
          channel.connect(remote);
  
          ObjectOutputStream oos = new ObjectOutputStream(channel.socket().getOutputStream());
          oos.writeObject(s.k);
          //oos.close();
  
          ObjectInputStream oin = new ObjectInputStream(channel.socket().getInputStream());
          list = (ConcurrentHashMap&lt;Key, String&gt;) oin.readObject();
          //oin.close();
      } catch (ClassNotFoundException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      System.out.print(list.get(s.k));
  }
  }
  </code></pre>
  
  <p>Here is a part of the Server code :</p>
  
  <pre><code>public class WorkerRunnable implements Runnable {
  
  SocketChannel client;
  int port;
  String ip;
  public WorkerRunnable(SocketChannel client, int port, String ip) {
      this.client = client;
      this.port = port;
      this.ip = ip;
  }
  @Override
  public void run() {
      // TODO Auto-generated method stub
  
      Receive r = new Receive();
      Key k = null;
      try {
          ObjectInputStream oin = new ObjectInputStream(client.socket().getInputStream());
          k = (Key) oin.readObject();
          //oin.close();
          r.list.put(k, ip);
  
          ObjectOutputStream oos = new ObjectOutputStream(client.socket().getOutputStream());
          oos.writeObject(r.list);
          //oos.close();
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  
  }
  </code></pre>
  
  <p>HELP</p>
  
  <p>EDIT 1 : Here is the Receive class</p>
  
  <pre><code>public class Receive {
  
  public ConcurrentHashMap&lt;Key,String&gt; list;
  
  public Receive() {
      list = new ConcurrentHashMap&lt;Key,String&gt;();
  }
  public static void receive() {
  
      ServerSocketChannel serverSocket;
  
      try {
          serverSocket = ServerSocketChannel.open();
          InetSocketAddress local = new InetSocketAddress(5000);
          serverSocket.bind(local);
          SocketChannel client = serverSocket.accept();
          int p = client.socket().getLocalPort();
          String ip = client.socket().getInetAddress().toString();
          (new Thread(new WorkerRunnable(client, p, ip))).start();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
  }
  }
  </code></pre>
  
  <p>N.B.
  In fact this is a simplified version of the entire java application that I'm building. A number of clients send their public keys to the server at regular intervals. The server listens at a port. It receives the public keys, gets the ip addresses and puts them in a ConcurrentHashMap. The ConcurrentHashMap is then sent back to the clients. In this way the clients will know who is "online".
  In this simplified version the server gets the public key and ip of one client and sends back the ConcurrentHashMap with the client's public key and ip only.</p>