我的“套接字到ServerSocket"-implementation的客户端看起来是这样的,但它从未通过ObjectInputStream-initialization。我试着注释掉这一行,然后执行下面的print-语句。连接(至少在最初阶段)是成功的,ServerSocket报告了成功的连接。
客户端
try(
Socket socket = new Socket("localhost", 4444);
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
) {
System.out.println("Connection and IO established");
} catch (IOException e) {
e.printStackTrace();
}服务器
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Client has connected!"); // This always prints
new Thread(new ServerThread(clientSocket)).start();
System.out.println("ServerThread has started"); // This as well
}发布于 2014-03-19 20:38:39
ObjectInputStream阻塞等待对等体的ObjectOutputStream的构造。显然,如果您在堆栈中有一个BufferedOutputStream,那么对等方没有构建它,或者在构建之后可能需要立即刷新它。
或者,它本身就是挂起构建ObjectInputStream的,因为您的客户端没有构建ObjectOutputStream.,解决方案是始终在ObjectInputStream.之前构造并刷新ObjectOutputStream
https://stackoverflow.com/questions/22515654
复制相似问题