简体   繁体   English

Java - 从缓冲读取器(从套接字)读取正在暂停线程

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

I have a thread that reads characters from a Buffered reader (created from a socket as follows):我有一个线程从缓冲读取器中读取字符(如下从套接字创建):

inputStream = new BufferedReader(new InputStreamReader(clientSock.getInputStream()));

This code works only one time.此代码只工作一次。 For example, if a client connects and sends this: "This is a test" and "This is another test", the host output is:例如,如果客户端连接并发送:“This is a test”和“This is another test”,则主机 output 为:

 Reading from stream:
 Chars read from stream: 16
 This is a test

 Reading from stream:

Note that the program does not receive "This is another test", because it is stuck on reading the stream.请注意,程序不会收到“这是另一个测试”,因为它在读取 stream 时卡住了。 Is there any way of dealing with this without reducing the buffer size?有没有办法在不减少缓冲区大小的情况下处理这个问题? This is the code for the thread:这是线程的代码:

public void run() {
        boolean dataRecieved = false;
        char[] inputChars = new char[1024];
        int charsRead = 0;

        while (!stopNow) {

            try {
                Thread.sleep(getDataDelay);

                //Read 1024 characters. Note: This will pause the thread when stream is empty.
                System.out.println("Reading from stream:");
                charsRead =  inputStream.read(inputChars); //<< THIS LINE IS PAUSING THE THREAD!> 


                if ((charsRead =  inputStream.read(inputChars)) != -1)
                {
                    System.out.println("Chars read from stream: " + charsRead);  
                    System.out.println(inputChars);
                    System.out.flush();
                }


            } catch (IOException e) {
                System.out.println("IOException");
                //TODO: CLIENT HAS DISCONNECTED...
            } catch (InterruptedException e) {
                System.out.println("Interrupted");
                // Sleep was interrupted.
            } 

        }

    }

Code for client/sender (not my code):客户端/发件人的代码(不是我的代码):

public static void main(String[] args) throws IOException {
        // <<<<<<<<<<< CLIENT >>>>>>>>>>>>>>>

        Socket sock = new Socket("127.0.0.1", 3000);
        // reading from keyboard (keyRead object)
        BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
        // sending to client (pwrite object)
        OutputStream ostream = sock.getOutputStream(); 
        PrintWriter pwrite = new PrintWriter(ostream, true);

        // receiving from server ( receiveRead  object)
        InputStream istream = sock.getInputStream();
        BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));

        System.out.println("Start the chitchat, type and press Enter key");

        String receiveMessage, sendMessage;               
        while(true)
        {
            sendMessage = keyRead.readLine();     // keyboard reading
            pwrite.println(sendMessage);       // sending to server
            System.out.flush();         // flush the data

            if((receiveMessage = receiveRead.readLine()) != null) //receive from server
            {
                System.out.println(receiveMessage); // displaying at DOS prompt
            }         
        }               
    }          

java.io.InputStream.read() is a blocking call, which means if no data is available the thread halts until data becomes available. java.io.InputStream.read()是一个阻塞调用,这意味着如果没有可用的数据,线程将暂停直到数据可用。

For non-blocking I/O, use classes from the java.nio package.对于非阻塞 I/O,请使用java.nio package 中的类。

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?接收者是否应该在收到消息时发送响应?

Implement a protocol where you send the length of your data in your headers so the server/client knows how much data to expect.实现一个协议,您可以在标头中发送数据长度,以便服务器/客户端知道需要多少数据。

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

You have to create ServerSocket That listen to client in every loop.您必须创建在每个循环中侦听客户端的ServerSocket

ServerSocket socket = new ServerSocket(3000);

Here is my run() method that will wait for client Socket every time这是我的 run() 方法,每次都会等待客户端Socket

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.因为这一行是在if语句中移动的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM