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

10

我有一个线程从通过套接字创建的缓冲读取器中读取字符:

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

这段代码只能运行一次。例如,如果客户端连接并发送以下内容:“This is a test”和“This is another test”,则主机输出为:

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

 Reading from stream:

请注意,程序没有接收到"This is another test",因为它被卡在读取数据流的过程中。有没有不减小缓冲区大小的方法来处理这个问题呢?以下是线程的代码:

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.
            } 

        }

    }

客户端/发送方的代码(非我的代码):

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

@bs0d1 我也会包含发送者的代码。 - David
@David,请尝试在pwrite.println后添加pwrite.flush()。 - sinan
我刚刚尝试了一下,主机仍然卡在“读取流”的状态。 - David
@David,代码看起来没有问题。我也想看一下发送数据的客户端代码。 - Talha Ahmed Khan
这里睡眠是没有意义的。你似乎不理解阻塞 I/O 的概念。 - user207421
显示剩余6条评论
5个回答

12

java.io.InputStream.read() 是一个阻塞式调用,这意味着如果没有可用的数据,线程会暂停直到有数据可用。

要进行非阻塞I/O,请使用java.nio包中的类。


3

您的“发送方”正在等待从“接收方”接收数据,这就是代码无限期等待的地方。接收方是否应该在收到消息时发送响应?


1
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.

    }
}

如果你想向套接字写入数据,
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

实现一个协议,在头部发送数据的长度,这样服务器/客户端就知道要期望多少数据。


-1
你需要创建一个 ServerSocket,在每个循环中监听客户端。
ServerSocket socket = new ServerSocket(3000);

这是我的 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();
            }
        }
    }

你还有一个小错误会导致代码停止运行,请移除这一行

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

因为这行代码被移动到了一个if语句中。


从他的服务器I/O代码已经在执行这一事实中可以看出,他已经拥有了一个ServerSocket。你最后的评论是无法理解的。 - user207421

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接