我想通过套接字发送多种数据类型。
我有那段代码:
当我创建客户端时,它会生成公钥和私钥,并将公共服务发送给服务,服务存储它。 当客户端想要向另一个客户端发送消息时,它从服务器获取他的公钥来加密消息。
但是当我运行此代码时,它会冻结在dest_public_key=(PublicKey) oin.readObject()
的客户端
为什么停在这里?
为什么它没有读取处理程序写入的PublicKey对象?
流中有什么问题吗?
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();}
}
处理类
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();}
}}
客户端类
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();
}
}