I tried a simple client server with ObjectInputStream and ObjectOutputStream instead BufferedReader and BufferedWriter but this code don't want to work . This is the code
[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]
The problem is that the client and server are blocked on ois = new ObjectInputString.
Why?
在ObjectInputStream之前创建ObjectOutputStream,否则会出现死锁。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.