//这是请求服务器将公钥发送到客户端
的代码package gameserver;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import java.net.Socket;
import java.io.*;
import javax.swing.JFrame;
import java.awt.*;
import javax.swing.*;
public class GameClient extends JFrame implements ActionListener {
private JButton b1 = new JButton("press");
public GameClient () {
GridLayout layout = new GridLayout(3, 0, 0, 0);
setLayout(layout);
setSize(320,150);
add(b1);
b1.addActionListener(this);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
GameClient frame = new GameClient();
frame.setVisible(true);
}
});
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == b1) {
Socket socket = null;
ObjectInputStream in = null;
ObjectOutputStream out = null;
Scanner console = new Scanner(System.in);
try {
socket = new Socket("127.0.0.1", 12346);
in = new ObjectInputStream(socket.getInputStream());
out = new ObjectOutputStream(socket.getOutputStream());
Object result;
result = in.readObject();
System.out.println(result);
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
}
//服务器代码,用于在客户端连接时将公钥发送到客户端
package gameserver;
import java.io.*;
import java.net.*;
import java.security.*;
import javax.crypto.*;
public class GameServer {
public static void main(String arg[]) {
try {
ServerSocket ss = new ServerSocket(12346);
while(true) {
Socket s = ss.accept();
ClientHandler ch = new ClientHandler(s);
ch.start();
}
}
catch(IOException ioe) {
ioe.printStackTrace();
}
}
}
class ClientHandler extends Thread {
Socket socket;
KeyPair keyPair = null;
public ClientHandler(Socket socket) {
this.socket = socket;
}
public KeyPair genKeyPair () {
try {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);
keyPair = keyGen.generateKeyPair();
//PrivateKey privateKey = keyPair.getPrivate();
//PublicKey publicKey = keyPair.getPublic();
} catch (NoSuchAlgorithmException e) {
System.out.println(e.getMessage());
}
return keyPair;
}
public void run() {
try {
ObjectInputStream Ois = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream Oos = new ObjectOutputStream(socket.getOutputStream());
keyPair = genKeyPair ();
PublicKey publicKey = keyPair.getPublic();
Oos.writeObject(publicKey);
socket.close();
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
}
问题是,当我按下按钮时,客户端似乎没有连接到服务器,并且没有收到任何想法?感谢
答案 0 :(得分:3)
您的代码在此行被阻止:
ObjectInputStream Ois = new ObjectInputStream(socket.getInputStream());
来自文件
创建一个从指定内容读取的ObjectInputStream 的InputStream。从流中读取序列化流头部 验证。这个构造函数将阻塞直到相应的 ObjectOutputStream已经编写并刷新了标题。
如果安装了安全管理器,则此构造函数将检查 调用时的“enableSubclassImplementation”SerializablePermission 直接或间接由子类的构造函数 覆盖ObjectInputStream.readFields或 ObjectInputStream.readUnshared方法。
修正:
更改您的
ObjectInputStream Ois = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream Oos = new ObjectOutputStream(socket.getOutputStream());
到
ObjectOutputStream Oos = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream Ois = new ObjectInputStream(socket.getInputStream());