Send multiple data type over socket

这一生的挚爱 提交于 2019-12-13 04:24:15

问题


I want to send multiple data type over socket.

I have that code:

when I create client, it generates public and private keys and sends the public to serve and the serve stores it. when client wants to send message to another client, it gets his public key from the server to encrypt the message.

But when I run this code it freeze at client in dest_public_key=(PublicKey) oin.readObject() Why it stop at here? Why it doesn't read the PublicKey's object that the handler writes to it? Is there any problems in streams?

public class Server { 
private static final int PORT = 9001;
ServerSocket listener;
private Handler h[] = new Handler[5];
public PublicKey[] keys = new PublicKey[5];
private int clientCount = 0;
public Server() throws Exception{
    System.out.println("The server is running.");
    listener = new ServerSocket(PORT);
    run();
}
public void run() throws Exception{
    System.out.println("Waiting for a client ...");
    while (true) { addClient(listener.accept());}
}
private void addClient(Socket socket) throws Exception{
    h[clientCount] = new ServerHandler(this, socket,clientCount);
    h[clientCount].open(clientCount);
    h[clientCount].start(); 
    clientCount++;
}
public void store(int id,PublicKey key){
   keys[id]=key;}
public PublicKey getPublicKey(int id){
    return keys[id]; }
public static void main(String[] args) throws Exception {
    Server s = new Server();}
}

handle class

public class Handler extends Thread {
   private Server  server;
   private Socket socket;
   private int ID = -1;
   private DataInputStream  streamIn  =  null;
   private DataOutputStream streamOut = null;
   private ObjectInputStream obIn = null;
   private ObjectOutputStream obOut = null;
   public Handler(Server _server, Socket _socket, int i){
    super();
      server = _server;
      socket = _socket;
      ID     = i;
}
    public void run()
   {  while (true) 
      {  try
         {
          int dest=streamIn.readInt();
          PublicKey dest_public_key=server.getPublicKey(dest);
          obOut.writeObject(dest_public_key);
          obOut.flush();}}
      }  
 public void open(int i) 
   {  
      PublicKey publick = null;
      try {
          streamOut = new DataOutputStream(socket.getOutputStream());
      streamIn = new DataInputStream(socket.getInputStream());
      obOut = new ObjectOutputStream(socket.getOutputStream());
      obIn = new ObjectInputStream(socket.getInputStream());
          publick=(PublicKey)obIn.readObject();
      server.store(ID,publick);
      streamOut.writeInt(i);
      streamOut.flush();
    } catch (Exception e) {
        e.printStackTrace();}   
   }}

client class

public class Client extends implements Runnable {
DataInputStream din;
DataOutputStream dot;
    ObjectInputStream oin;
    ObjectOutputStream oot;
    public Client() {
          try {
            socket = new Socket(serverAddress, 9001);
        din  = new DataInputStream(socket.getInputStream());
        dot = new DataOutputStream(socket.getOutputStream());
        oot = new ObjectOutputStream(socket.getOutputStream());
        oin = new ObjectInputStream(socket.getInputStream());
        RSA.generateKey();
        public_key=RSA.keys.getPublic();
        private_key=RSA.keys.getPrivate();
        oot.writeObject(public_key);
        oot.flush();
        int j =din.readInt();  // read number from server
                 } catch (Exception e) {
        e.printStackTrace();
    }
   }
   public static void main(String[] args) throws Exception {
     Client client = new Client();
     PublicKey dest_public_key=null;
     System.out.println("enter client that you would to send to it");
     Scanner input = new Scanner( System.in );
     int select = input.nextInt();
     dot.writeInt(select);
     dot.flush();
     dest_public_key=(PublicKey) oin.readObject();
    }
    }

来源:https://stackoverflow.com/questions/26830233/send-multiple-data-type-over-socket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!