我正在做java服务器客户端应用程序,我有问题.当我想让ObjectInputStream卡住时,我成功地获得了客户端套接字.
码:
serverSocket = new ServerSocket(9999);
while(true){
System.out.println("Waiting for player");
Socket socket = serverSocket.accept();
System.out.println("Player connected, waiting for command");
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
System.out.println("input created");
}
输出:
等待球员
玩家连接,等待命令
我该怎么办?
解决方法:
ObjectInputStream等待传入的标头.直到标题被完全接收,你将被困在这一行:
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
要解决此问题,您需要在建立连接后立即在另一端(即服务器)的ObjectOutputStream上调用flush().
有关更多信息,请阅读javadoc.