简体   繁体   中英

Has BufferedReader a Timeout when sending HTTP Get Request?

I am using a socket to send HTTP GET request in order to control a Signal Generator via SCPI commands. I manage to send several request and receive answer from the server.

My problem is if I wait about 15 seconds, then I send requests again, I do not receive any answer to my requests. It seems the BufferedReader does not received any message anymore

I have set the timeout of my socket to infinite. It seems my problem comes from a "timeout" of the BufferedReader as his ready method returns always false.

The only solution to receive message again is to close and re-create my Socket, PrintWriter, and BufferedReader.

I declare my socket and PrintWriter and BufferedReader as below:

tcpIpSocket = new Socket(ipAddress, tcpPort);
printWriteOutputStream = new PrintWriter(tcpIpSocket.getOutputStream(),false);
bufferInputStream = new BufferedReader(new InputStreamReader(tcpIpSocket.getInputStream()));

I use a seperate function to send and received my HTTP GET request as folow:

public static String sendRequestToSigGenerator(String scpiMessageToSend) throws Exception {

    StringBuffer receivedHTTPMessages=new StringBuffer();
    int receivedCharacterFromInputStreamInt;

    printWriteOutputStream.println("GET /;:"+scpiMessageToSend+ " HTTP/1.0\r\n"); 
    printWriteOutputStream.flush(); 

    Thread.sleep(100); //Sleep needed for the buffer to be filled

    while (bufferInputStream.ready()) {
         receivedCharacterFromInputStreamInt = bufferInputStream.read();
         receivedHTTPMessages.append((char)receivedCharacterFromInputStreamInt);
   }

    return(receivedHTTPMessages.toString());
}

After not sending message for 15 seconds and sending a new request, the while (bufferInputStream.ready() stays always false

EDIT: Instead of implementing my own HTTP, I use the library : java.net.URL; which handle easily my queries and avoid my problem:

public static String sendHTTPRequest(String scpiCommand) throws Exception {
    try{
        StringBuilder returnString= new StringBuilder();
        URL url = new URL("http://"+ipAddress+"/;:"+scpiCommand);
        BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));

        String strTemp = "";
        while (null != (strTemp = br.readLine())) {
            returnString.append(strTemp);
        }
        return(returnString.toString());
    } catch (UnknownHostException e) {
        return ERROR;
    } catch (IOException e) {
        return ERROR;
    }
}

It seems my problem comes from a "timeout" of the BufferedReader

No. There is no such thing.

as his ready method returns always false.

Which merely means that no data has arrived yet. It is your expectations that are at fault here. Instead of the pointless sleep() and the equally pointless ready() test, you should just block in read() until data arrives or EOS or an exception occurs.

If you don't want to block forever , set a read timeout on the socket. Don't turn off the mechanism you actually need and then try to reproduce it by other means.

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