【问题标题】:DataInputStream from socket来自套接字的 DataInputStream
【发布时间】:2012-02-29 05:46:48
【问题描述】:

更新 - 转向一致的类型提供的解决方案

客户端将消息发送到服务器套接字,然后服务器以原始消息响应客户端。在引入后一种功能时,服务器只接收一条消息,而不是继续接收该消息,并且不响应客户端。评论了添加的行。任何有关挂断的见解都会很棒。

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

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

  while (!line.equals(".bye"))
  {  try
     {  line = console.readLine();
        streamOut.writeUTF(line); //Send console data to server socket
        String reply = streamIn.readUTF(); //Recieve confirmation msg from server
        System.out.println( reply ); //Print the msg
        streamOut.flush();
     }

   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();
   }

【问题讨论】:

  • 这可以编译吗?不应该是 streamOut.writeBytes( nextReply.getBytes() ); ?

标签: java stream io


【解决方案1】:

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

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

【讨论】:

  • 我已经从 UTF 转移到 readLine()writeBytes() 并希望得到适当的缓冲。由于服务器不再从客户端接收数据,我是否应该继续使用 *UTF()?
  • 正如我指出的 readLine() 等待换行符,所以我假设您在 writeBytes() 末尾发送一个新行。否则,我会按照我的建议使用 writeUTF/readUTF。
  • 恢复为 read/writeUTF,与之前类似的症状,客户端能够通过初始 msg 发送,之后不再接收 msg。
  • 您在每次 writeUTF() 之后调用 flush() 并且您的客户端在 readUTF() 上阻塞(您可以在调试器中看到这一点)
  • 已移动writeUTF(),这是否也阻止了readUTF()?我应该如何调用调试器以更好地了解正在发生的事情?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-12
  • 2011-05-05
  • 2014-06-04
  • 2016-02-18
相关资源
最近更新 更多