Java 套接字ObjectInputStream和ObjectOutputStream不工作
我尝试了使用ObjectInputStream和ObjectOutputStream代替BufferedReader和BufferedWriter的简单客户机服务器,但这段代码不起作用。 这是密码 [客户] [守则]Java 套接字ObjectInputStream和ObjectOutputStream不工作,java,sockets,stream,objectinputstream,objectoutputstream,Java,Sockets,Stream,Objectinputstream,Objectoutputstream,我尝试了使用ObjectInputStream和ObjectOutputStream代替BufferedReader和BufferedWriter的简单客户机服务器,但这段代码不起作用。 这是密码 [客户] [守则] public class MainClient { public static final String SERVER_ADDRESS_STRING = "192.168.0.2"; public static final int PORT_NO = 8000; private
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();
}
}
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]
[服务器]
[守则]
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();
}
}
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上被阻塞
为什么?在ObjectInputStream之前创建ObjectOutputStream,否则会出现死锁。这里只有一个线程,因此只有一个客户端或服务器被阻塞。您能在代码中添加注释以突出显示您被阻止的确切位置吗?这是两个独立启动的程序。服务器尝试打开ObjectInputStream时被阻止,但您确定它已通过accept()?这是怎么发生的?