El socket de formulario de lectura de Java BufferedReader no devuelve nulo

I'm currently writing an app in Java that opens a socket and should receive and send data over it.

It is my understanding that BufferedReader.readLine() returns null once the buffer is empty. However, my code doesn't exit from the loop that reads the lines from the BufferedReader. The idea is thata i receive a list of songs and then send a value to the server where a song starts playing.

Este es el código:

package me.frankvanbever.MediaServerClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

        public class MediaServerClient {

             * @param args
            public static void main(String[] args) {

                    Socket server;
                    try {
                        server = new Socket( "" , 2626 );

                        InputStream in = server.getInputStream();
                        OutputStream out = server.getOutputStream();

                        BufferedReader bin = new BufferedReader( new InputStreamReader( in ) , 4096);

                        String inputline;
                        while( (inputline = bin.readLine()) != null){



                        System.out.println("exited loop");
                    } catch (UnknownHostException e) {
                    } catch (IOException e) {



Is this normal behaviour? The server is written in python and I can change the source code.

preguntado el 05 de mayo de 13 a las 12:05

1 Respuestas

It is my understanding that BufferedReader.readLine() returns null once the buffer is empty.

No. It will return null once the underlying return has been cerrado. If the server you're connecting to doesn't close the connection, BufferedReader will just hang, waiting for the next line of text.

Don't forget that TCP is stream-oriented (as is BufferedReader). There's no indication of "a complete message" unless you put it in your protocol. That's why protocols often include things like message terminators, or they specify how much data is in a message before sending it.

contestado el 05 de mayo de 13 a las 12:05

so I'd have to hard code a way to let my client know the server has stopped sending stuff? - franco vanbever

@FrankVanbever: You'd need to build it into the protocol itself - both the server and the client would need to agree about this. You podría effectively have something along the lines of "if I haven't received anything for 5 seconds, I'll assume we're done" but that's very fragile in the face of slow networks, and slow in the face of veloz networks, as well as being relatively hard to code unless your platform supports timeouts. - jon skeet

@FrankVanbever Or you could close the connection when there is nothing more to send, or if you need to keep the connection open you could send an empty like or a line like [End of Message] or something of you choice. BTW email sends . en una línea por sí mismo. - pedro laurey

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.