Java – how to programmatically cancel reading from InputStream?

I checked stackoverflow and Google, but I couldn't find a solution to the problem, so that's what I'm doing

I have a console application server that users connect to using any telnet client I am working on a client with three independent threads. One thread controls all program access to the client. The clientthread maintains two threads, one for processing input and one for sending output Clienthread reads the client socket and sends the input (if any) to the processingthread, which processes the input and performs the user specified operation The output thread manages the output queue that needs to be sent and sends it every time it wakes up When a user disconnects, I occasionally use the remaining output in the output "queue" before the connection is terminated, so I want to set it so that the connection remains active until the output queue is empty – but the thread reading the socket (and eventually closing it when there is no more output) hangs on the read, and I can't close it I just closed the socket before the user disconnected. This is not a problem I will give you the relevant code

public class ClientThread extends Thread {
   // Initialization and other code not pertaining to the problem 

   public void run() {
      while (connected) {
         try {
            String input = in.readLine();

            if (input != null) {
               processor.queue(TextUtility.processBackspaceChars(input));
               timeoutTimer.interrupt();
            }
         } catch (Exception e) {
            if (e.getMessage() != null && !(e.getMessage().equals("socket closed")))
               server.appendError("Issue reading from client: "
                     + getClientDisplayInfo() + "\n"
                     + "&r-The issue is:&w- " + e.getMessage() + "&r-.");
         }
      }

      while (disconnecting) {
         try {
            if (outputThread.hasOutput()) {
               sleep(10);

            } else {
               disconnecting = false;
               outputThread.disconnect();
               client.close();
            }
         } catch (Exception exc) { 
            server.appendError("Failed to close the Thread for client: " + getClientDisplayInfo());
         }
      }  
   }   

   public void disconnect() {
      try {
         connected = false;
         disconnecting = true;
         processor.disconnect();
         timeoutTimer.disconnect();
         in.close(); // This is the BufferedReader that reads the Socket Input Stream
         this.interrupt(); // This was my attempt to break out of the read,but it Failed

      } catch (Exception e) {
         server.appendError("Failed to close the Thread for client: " 
               + getClientDisplayInfo());
      }
   }
}

I'm just curious about how to cancel reading without closing the socket, because I need to continue sending output

Solution

I think it's similar to the problem I encountered yesterday. You can call is on the input stream Available() and check whether the data is available before performing (blocking) reading

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>