简体   繁体   中英

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

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. 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. Does this have some concurrency relation I'm overlooking?

thx

The problem is that readLine tries reading a line. 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.

Also as suggested you can try flushing the ostream after writing to it.

writeUTF() doesn't write a line. See the Javadoc. writeUTF() is for use with readUTF(). And Readers are for use with Writers. So change the DataOutputStream to a BufferedWriter and call write() and then newline().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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