Java - Reading from a buffered reader (from a socket) is pausing the thread

前端 未结 5 1821
逝去的感伤
逝去的感伤 2021-01-04 14:05

I have a thread that reads characters from a Buffered reader (created from a socket as follows):

inputStream = new BufferedReader(new InputStreamReader(clien         


        
相关标签:
5条回答
  • 2021-01-04 14:49
    Socket socket;
    
    // Assuming socket is connected and not null
    
    if(socket != null){
        if(socket.getInputStream().available() > 0){
            byte[] buffer;
            buffer = new byte[socket.getInputStream().available];
            socket.getInputStream().read(buffer);
    
            // Your code here to deal with buffer.
    
        }
    }
    

    If you want to write to the socket,

    OutputStream mmOutStream;
    mmOutStream = socket.getOutputStream();
    
    public void write(byte[] buffer) {
        try {
            mmOutStream.write(buffer);
        } catch (IOException e) {
            Log.e(TAG, "Exception during write ", e);
        }
    }
    
    0 讨论(0)
  • 2021-01-04 14:53

    Your "sender" is waiting to receive data back from the "receiver", and this is where the code waits indefinitely. Is the receiver supposed to be sending a response when it gets a message?

    0 讨论(0)
  • 2021-01-04 14:55

    Implement a protocol where you send the length of your data in your headers so the server/client knows how much data to expect.

    0 讨论(0)
  • 2021-01-04 15:01

    You have to create ServerSocket That listen to client in every loop.

    ServerSocket socket = new ServerSocket(3000);
    

    Here is my run() method that will wait for client Socket every time

    public void run(){
            boolean dataRecieved = false;
            char[] inputChars = new char[1024];
            int charsRead = 0;
    
            while (!stopNow) {
                try {
                    System.out.println("Listen To Clients:");
    
                    // The ServerSocket has to listen the client each time.
                    InputStreamReader isr = new InputStreamReader( socket.accept().getInputStream() );
                    inputStream = new BufferedReader( isr );
    
                    //Read 1024 characters. Note: This will pause the thread when stream is empty.
                    System.out.println("Reading from stream:");
    
                    if ((charsRead =  inputStream.read(inputChars)) != -1)
                    {
                        System.out.println("Chars read from stream: " + charsRead);  
                        System.out.println(inputChars);
                        System.out.flush();
                    }
                } 
                catch (IOException e) 
                {
                    e.printStackTrace();
                }
            }
        }
    

    You have another minor mistake that halts the code and remove the line

    charsRead =  inputStream.read(inputChars); //<< THIS LINE IS PAUSING THE THREAD!>
    

    Because this line is moved in an if statement.

    0 讨论(0)
  • 2021-01-04 15:03

    java.io.InputStream.read() is a blocking call, which means if no data is available the thread halts until data becomes available.

    For non-blocking I/O, use classes from the java.nio package.

    0 讨论(0)
提交回复
热议问题