Java 来自套接字的DataInputStream

Java 来自套接字的DataInputStream,java,stream,io,Java,Stream,Io,更新-移动到一致类型提供的解决方案 客户机向服务器套接字发送消息,然后服务器用原始消息响应客户机。当引入后一种功能时,服务器只接收一条消息,而不是继续接收所述消息,并且不响应客户端。对添加的行进行了注释。任何对挂断的洞察都会很棒 客户端、评论的问题和响应中的代码更新: { private Socket socket = null; private BufferedReader console = null; private DataInputStre

更新-移动到一致类型提供的解决方案

客户机向服务器套接字发送消息,然后服务器用原始消息响应客户机。当引入后一种功能时,服务器只接收一条消息,而不是继续接收所述消息,并且不响应客户端。对添加的行进行了注释。任何对挂断的洞察都会很棒

客户端、评论的问题和响应中的代码更新:

{  private Socket socket              = null;
   private BufferedReader  console   = null;
   private DataInputStream  streamIn   = null;
   private DataOutputStream streamOut = null;


服务器端,已评论问题。

   public void handleClient() throws IOException {
      boolean done = false;
      try {
      System.out.println("Server Thread " + ID + " running.");
      while (!done) {
        String nextCommand = streamIn.readUTF();
        if( nextCommand.equals(".bye") ) {
           System.out.println("Client disconnected with bye.");
           done = true;
        } else {
           System.out.println( nextCommand );
           String nextReply = "\"You sent me ->" +nextCommand.toUpperCase() + "\"\n";
           streamOut.writeUTF( nextReply );
        }
     }
   } finally {
     streamIn.close();
     streamOut.close();
     socket.close();
   }
   }
   public void open() throws IOException
   {
      streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
      streamOut = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
//      streamOut = new DataOutputStream(socket.getOutputStream());
  }
   public void close() throws IOException
   {  if (socket != null)    socket.close();
      if (streamIn != null)  streamIn.close();
      if (streamOut != null) streamOut.close();
   }

您使用的是writeUTF和readUTF,但是在一个地方streamIn.readLine(),我希望在它等待新行时阻止它。我怀疑您需要一致地使用readUTF


顺便说一句,控制台不是一个数据流,它是文本,我建议您使用BufferedReader。

您使用的是writeUTF和readUTF,但在一个地方streamIn.readLine(),我希望在它等待新行时阻止它。我怀疑您需要一致地使用readUTF


顺便说一句,控制台不是一个数据流,它是文本,我建议您使用BufferedReader。

这个编译吗?它不应该是streamOut.writeBytes(nextReply.getBytes())?这个编译吗?它不应该是streamOut.writeBytes(nextReply.getBytes())?我已经从UTF转移到了
readLine()
writeBytes()
,希望能得到适当的缓冲。当服务器不再从客户端接收数据时,我是否应该继续使用*UTF()?正如我指出的,readLine()等待换行符,因此我假设您正在writeBytes()的末尾发送一行新行。否则,我会像我建议的那样使用writeUTF/readUTF。恢复为read/writeUTF,与之前的症状类似,客户机能够发送初始消息,之后,就不再接收msg。在每次writeUTF()之后,您都会调用flush(),并且您的客户机在readUTF()上阻塞(您可以在调试器中看到这一点)
writeUTF()
,这是否也会阻塞
readUTF()
?我应该如何调用调试器来更好地理解正在发生的事情?我已经从UTF转移到
readLine()
writeBytes()
,并希望得到适当的缓冲。当服务器不再从客户端接收数据时,我是否应该继续使用*UTF()?正如我指出的,readLine()等待换行符,因此我假设您正在writeBytes()的末尾发送一行新行。否则,我会像我建议的那样使用writeUTF/readUTF。恢复为read/writeUTF,与之前的症状类似,客户机能够发送初始消息,之后,就不再接收msg。在每次writeUTF()之后,您都会调用flush(),并且您的客户机在readUTF()上阻塞(您可以在调试器中看到这一点)
writeUTF()
,这是否也会阻塞
readUTF()
?我应该如何调用调试器来更好地理解发生了什么?
   public void start() throws IOException
   {  console = new BufferedReader(new InputStreamReader(System.in)); //Changed console to BufferedReader
      streamIn  = new DataInputStream(socket.getInputStream());
      streamOut = new DataOutputStream(socket.getOutputStream());
   }
   public void stop()
   {  try
      {  if (console   != null)  console.close();
         if (streamOut != null)  streamOut.close();
         if (streamIn != null)  streamIn.close(); //Is it good practice to close
         if (socket    != null)  socket.close();
      }
   public void handleClient() throws IOException {
      boolean done = false;
      try {
      System.out.println("Server Thread " + ID + " running.");
      while (!done) {
        String nextCommand = streamIn.readUTF();
        if( nextCommand.equals(".bye") ) {
           System.out.println("Client disconnected with bye.");
           done = true;
        } else {
           System.out.println( nextCommand );
           String nextReply = "\"You sent me ->" +nextCommand.toUpperCase() + "\"\n";
           streamOut.writeUTF( nextReply );
        }
     }
   } finally {
     streamIn.close();
     streamOut.close();
     socket.close();
   }
   }
   public void open() throws IOException
   {
      streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
      streamOut = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
//      streamOut = new DataOutputStream(socket.getOutputStream());
  }
   public void close() throws IOException
   {  if (socket != null)    socket.close();
      if (streamIn != null)  streamIn.close();
      if (streamOut != null) streamOut.close();
   }