简体   繁体   中英

How to set a timeout on a BufferedReader with a socket stream

Here I am creating a thread to check for a server response every 2 seconds, the issue is that the client.monitorResponse() is a readLine() method and will not continue until a response is received.

client = new ClientObject("localhost");
timer.scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
        try {
            String response = null;
            if(!(response = client.monitorResponse()).isEmpty()) {
                System.out.println("Response: " + response);
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
}, 2000, 2000);

I am sending the response via the Server like so (where client is a established Socket):

public SocketObject(Socket client, int numberOfClients) throws Exception {
    socket = client; // the .accept() socket is passed through
    // this is because I assign them ID's for later use (I hold an ArrayList of sockets)
    this.clientId = numberOfClients;

    // both these are static to the class

    outputStream = new PrintWriter(client.getOutputStream());
    inputStream = new BufferedReader(new InputStreamReader(client.getInputStream()));
}

public void sendResponse(String response) {
    outputStream.println(response);
}

I am then picking the response up via the client Socket that has connected to the server:

public ClientObject(String hostname) throws IOException {

    // socket is static to this class

    socket = new Socket(hostname, 4444);
    System.out.println("Connected to " + hostname + " on port 4444...");

    // both static to this class

    outputStream = new PrintWriter(socket.getOutputStream(), true);
    inputStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));

    System.out.println("Successfully started a stream on " + hostname);
    this.hostname = hostname;
}

public String monitorResponse() throws Exception {
    System.out.println("Listening for a response...");
    return inputStream.readLine();
}

The debug console only displays the Listening for a response... output once which is telling me that it doesn't get past the inputStream.readLine() method in-side the Thread. Is there anyway I can add a timeout on the BufferedReader? I have tried multiple solutions like adding a .setSoTimeout() to the socket before creating the BufferedReader but all that did was close the connection/socket after the specified time.

Any help would be appreciated.

You should use a non-blocking (NIO) request and read chunks looking for newlines in-between. Typically in Java you just have to look for the NIO version of the Stream class you are using and use it to check every N seconds for new content. In your case with minimal modifications you can use the less fancy and efficient method of blocking calls with BufferedReader.ready() to prevent blocking:

String partialLine="";
public static String monitorResponse() throws Exception {
    System.out.println("Listening for a response...");
    int nextByte;
    String nextChar;
    while (inputStream.ready()) {
        nextByte = inputStream.read();
        nextChar = Character.toString ((char) nextByte);
        partialLine += nextChar;
        if ("\n".equals(nextChar)) {
            String line = partialLine;
            partialLine = "";
            return line.replace("\r\n", "");
        }
    }
    return "";
}

Check out http://tutorials.jenkov.com/java-nio/nio-vs-io.html for more info.

Is there anyway I can add a timeout on the BufferedReader?

No, but you can set a timeout on the Socket , with Socket.setSoTimeout() .

I have tried multiple solutions like adding a .setSoTimeout() to the socket before creating the BufferedReader but all that did was close the connection/socket after the specified time.

No it doesn't close the socket. It throws SocketTimeoutException , which you should catch and handle as pertinent. If the socket is being closed, you're closing it. Solution: don't.

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