简体   繁体   English

BufferedReader readLine方法挂起并阻止程序

[英]BufferedReader readLine method hangs and block program

I'm trying to learn sockets using Java and I sucessfully sent data to a ServerSocket running in my own machine. 我正在尝试使用Java学习套接字,并且我成功地将数据发送到在我自己的机器上运行的ServerSocket。 When I try to read from this socket using readline (so I can just echo my own sent message) my program hangs and won't return. 当我尝试使用readline从这个套接字读取时(所以我可以回显我自己发送的消息)我的程序挂起并且不会返回。

Here's the code: 这是代码:

public static void main(String[] args) throws UnknownHostException, IOException {

    TCPClient cli = new TCPClient("127.0.0.1", "15000");
    try {
        cli.ostream.writeUTF("Teste");
        String echo = cli.istream.readLine(); //it hangs in this line
        System.out.println(echo);
    }

TCPClient is a class I defined so I can test my program on a simpler interface before using swing on my homwework. TCPClient是我定义的类,所以我可以在我的homwework上使用swing之前在更简单的界面上测试我的程序。 here's the code: 这是代码:

public class TCPClient {

public DataOutputStream ostream = null;
public BufferedReader istream = null;

public TCPClient(String host, String port) throws UnknownHostException {
    InetAddress ip = InetAddress.getByName(host);

    try {
        Socket socket = new Socket(host, Integer.parseInt(port));

        ostream = new DataOutputStream(socket.getOutputStream());
        istream = new BufferedReader(new InputStreamReader(socket.getInputStream()));


    } catch (IOException ex) {
        Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
    }

}

My server is pretty simple. 我的服务器非常简单。 After the connection is estabilished, it enters in this loop and stays here until I close the client (because of the infinite loop). 建立连接后,它进入此循环并保持在此直到我关闭客户端(因为无限循环)。 Afterwards, some exception handling returns it to the point before the connection started. 之后,一些异常处理将其返回到连接开始之前的点。

            while(true){
                String msg = istream.readLine();
                System.out.println("Arrived on server: " + msg); //just works on debug
                ostream.writeUTF("ACK: " + msg);
                ostream.flush();
            }

I don't see what am I missing. 我不知道我错过了什么。

PS: the wierd stuff is that if I debug the server, I can see the message arriving there (I can print it, for example), but this isn't possible if I just run this code. PS:奇怪的是,如果我调试服务器,我可以看到消息到达那里(例如我可以打印它),但如果我只运行此代码,这是不可能的。 Does this have some concurrency relation I'm overlooking? 这是否有一些我忽略的并发关系?

thx 谢谢

The problem is that readLine tries reading a line. 问题是readLine尝试读取一行。 It will not return the line until it's sure that the end of line has been reached. 在确定已到达行尾之前,它不会返回该行。 This means that it expects either a newline character, or the end of the communication. 这意味着它需要换行符或通信结束。 Since the server doesn't send any newline char and doesn't close the stream, the client waits indefinitely. 由于服务器不发送任何换行符并且不关闭流,因此客户端将无限期地等待。

cli.ostream.writeUTF("Teste");

Shouldn't this be containing a new line? 这不应该包含新的一行吗? Otherwise read method will be waiting for new line I think. 否则read方法将等待我想的新行。

Also as suggested you can try flushing the ostream after writing to it. 同样如建议您可以在写入后尝试刷新ostream。

writeUTF() doesn't write a line. writeUTF()不写一行。 See the Javadoc. 见Javadoc。 writeUTF() is for use with readUTF(). writeUTF()用于readUTF()。 And Readers are for use with Writers. 读者可以和作家一起使用。 So change the DataOutputStream to a BufferedWriter and call write() and then newline(). 因此,将DataOutputStream更改为BufferedWriter并调用write(),然后调用newline()。

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

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