[英]Socket ObjectInputStream and ObjectOutputStream not working
I tried a simple client server with ObjectInputStream and ObjectOutputStream instead BufferedReader and BufferedWriter but this code don't want to work . 我尝试使用ObjectInputStream和ObjectOutputStream而不是BufferedReader和BufferedWriter的简单客户端服务器,但是此代码不起作用。 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. 问题是客户端和服务器在ois = new ObjectInputString上被阻止。
Why? 为什么?
在ObjectInputStream之前创建ObjectOutputStream,否则会出现死锁。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.