我尝试了一个带ObjectInputStream和ObjectOutputStream的简单客户端服务器,而不是BufferedReader和BufferedWriter,但是这段代码不想工作。 这是代码
[CLIENT]
[CODE]
public class MainClient {
public static final String SERVER_ADDRESS_STRING = "192.168.0.2";
public static final int PORT_NO = 8000;
private static ObjectInputStream ois;
private static ObjectOutputStream oos;
public static void main(String[] args) {
Socket socket = null;
try {
socket = new Socket(SERVER_ADDRESS_STRING, PORT_NO);
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("Client : 1");
try {
ois = new ObjectInputStream(socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Client : 2");
try {
oos = new ObjectOutputStream(socket.getOutputStream());
oos.flush();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Client : 3");
//Diffie-Hellman
try {
@SuppressWarnings("unused")
BigInteger shared_key = DiffieHellmanExchangeClient(socket, ois, oos);
System.out.println(shared_key);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
[\ CODE]
[SERVER]
[CODE]
public class MainServer {
public static final int PORT_NO = 8000;
private static final int BACKLOG_NO = 10;
private static ObjectInputStream ois;
private static ObjectOutputStream oos;
public static void main(String[] args) {
ServerSocket sslserver = null;
try {
sslserver = new ServerSocket(PORT_NO, BACKLOG_NO);
} catch (IOException e2) {
e2.printStackTrace();
}
System.out.println("Server : 1");
Socket socket = null;
try {
socket = (Socket) sslserver.accept();
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("Server : 2");
try {
ois = new ObjectInputStream(socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Server : 3");
try {
oos = new ObjectOutputStream(socket.getOutputStream());
oos.flush();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Server : 4");
//Diffie-Hellman
try {
@SuppressWarnings("unused")
BigInteger shared_key = DiffieHellmanExchangeServer(socket, ois, oos);
System.out.println(shared_key);
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
sslserver.close();
} catch (IOException e) {
e.printStackTrace();
}
}
[\ CODE]
问题是客户端和服务器在ois = new ObjectInputString上被阻止。
为什么?
答案 0 :(得分:0)
在ObjectInputStream之前创建ObjectOutputStream,否则会出现死锁。