java.net.Socket >输出流BufferedOutputStream flush() 确认 [英] java.net.Socket > outputStream > BufferedOutputStream flush() confirmation

查看:46
本文介绍了java.net.Socket >输出流BufferedOutputStream flush() 确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法知道 BufferedOutputStream 线程的 flush() 方法何时或是否成功完成?就我而言,我使用它通过 java.net.Socket 发送一个简单的字符串.在以下代码中,flush() 方法与 BufferedReader.read() 方法并行运行,并且套接字输出立即被输入读取阻塞,从而导致类似死锁的情况.我想做的是等待输出结束,然后开始读取输入.

is there a way of knowing when or whether the flush() method of a BufferedOutputStream thread has finished successfully? In my case I'm using it for sending a simple string through a java.net.Socket. In the following code, the flush() method is run in parallel with the BufferedReader.read() method and the socket output is immediately blocked by the input read resulting in something that resembles a deadlock. What I would like to do is wait for the output to end, and then start reading the input.

Socket sk = new Socket("192.168.0.112", 3000);
BufferedOutputStream bo = new BufferedOutputStream(sk.getOutputStream());
bo.write(message.getBytes());
bo.flush();

BufferedReader br = new BufferedReader(new InputStreamReader(sk.getInputStream()));
String line = br.readLine();
if (line.equals("ack")) {
    System.out.println("ack");
}


sk.close();

更新

服务器套接字:

ServerSocket ss = new ServerSocket(3000);
        System.out.println("server socket open");
        while (true) {
            Socket sk = ss.accept();
            System.out.println("new connection");
            BufferedReader br = new BufferedReader(new InputStreamReader(sk.getInputStream()));
            String line = br.readLine();
            System.out.println("received line: " + line);
            BufferedOutputStream bo = new BufferedOutputStream(sk.getOutputStream());
            bo.write("ack".getBytes()); bo.flush();
            sk.close();
        }

更新:

@Global 变量 - read 阻塞套接字的原因是它确实在等待 \n.使用

@Global Variable - the reason that read was blocking the socket is that it was waiting for the \n, indeed. Using

bo.write("ack\n".getBytes());

代替

bo.write("ack".getBytes());

让它发挥作用.

关于最初的问题,有没有办法知道flush()方法是否成功完成,@Stephen C提供了答案:

Regarding the initial question, is there a way of knowing if flush() method has finished successfully, @Stephen C provided the answer:

无法根据 Socket 或 OutputStream API 知道这一点.获得这种保证的正常方法是让远程应用程序发送回复"作为响应,并在本地读取一边.

there is no way to know that based on the Socket or OutputStream APIs. The normal way to get that sort of assurance is to have the remote application send an "reply" in response, and read it in the local side.

这个回复"在代码示例中实现并且有效.

This "reply" is implemented in the code sample and it works.

推荐答案

您的代码正在读取 reader.readLine() .你写的时候写\n吗?您可能希望将 \n 附加到您正在编写的字符串中.

Your code is reading reader.readLine() . Are your writing \n when writing? You may want to append \n to the string your are writing.

这篇关于java.net.Socket >输出流BufferedOutputStream flush() 确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆