SSLSocket BufferedReader blocking problem

Discussion in 'Java' started by sean.antony@gmail.com, Mar 6, 2007.

  1. Guest

    I have a client program which connects to an Apache server, sends some
    HTTP messages and retrieves the responses.

    An example message is:

    OPTIONS / HTTP/1.1
    Host: 127.0.0.1
    Accept: */*


    The response from Apache is:

    HTTP/1.1 200 OK
    Date: Wed, 07 Mar 2007 14:57:16 GMT
    Server: Apache/2.2.3 (Unix) mod_ssl/2.2.3 OpenSSL/0.9.8a DAV/2
    DAV: 1,2
    DAV: <http://apache.org/dav/propset/fs/1>
    MS-Author-Via: DAV
    Allow:
    OPTIONS,GET,HEAD,POST,DELETE,TRACE,PROPFIND,PROPPATCH,COPY,MOVE,LOCK,UNLOCK
    Content-Length: 0
    Content-Type: httpd/unix-directory


    This is the code that gets the response from Apache, the in variable
    is a BufferedReader connected to the InputStream of the SSLSocket.


    private String getResponse() throws IOException {
    String response = "";
    int c = in.read();
    char ch;
    long start = System.currentTimeMillis();
    while (in.ready() && c != -1) {
    ch = (char) c;
    response += ch;
    c = in.read();
    }
    long finish = System.currentTimeMillis();
    System.out.println(finish - start);
    ch = (char) c;
    response += ch;
    return response;
    }


    The output to the console is how long it took to get the whole message
    from the InputStream. In HTTP (Socket) mode this number is between
    0-20 milliseconds but when i use HTTPS (SSLSocket) mode it is approx
    5000! This number is independent of the time it takes to handshake
    with the server.

    In SSL mode the above method fails actually, because in.ready()
    returns false the first time, instead i have to modify it to:


    while ((c = in.read()) != -1) {
    ch = (char) c;
    response += ch;
    System.out.print(ch);
    }


    In which case, i get the whole message printed to the console as fast
    as HTTP mode but on the final evaluation of in.read() it takes the
    previously stated 5000 ms to finally exit the loop and return the
    String response.

    I am certain my Apache server is configured correctly as if i use a
    web browser to navigate the site in HTTPS the response is instant as
    like in unsecure HTTP mode.

    So why am i seeing this 5000 ms delay, when i reach the end of the
    stream in the BufferedReader?

    There should be no difference in reading the BufferedReader when using
    either HTTP or HTTPS mode, given that i've accounted for the time it
    takes to handshake, right?
     
    , Mar 6, 2007
    #1
    1. Advertising

  2. Guest

    On 6 Mar, 20:57, wrote:
    > I have a client program which connects to an Apache server, sends some
    > HTTP messages and retrieves the responses.
    >
    > An example message is:
    >
    > OPTIONS / HTTP/1.1
    > Host: 127.0.0.1
    > Accept: */*
    >
    > The response from Apache is:
    >
    > HTTP/1.1 200 OK
    > Date: Wed, 07 Mar 2007 14:57:16 GMT
    > Server: Apache/2.2.3 (Unix) mod_ssl/2.2.3 OpenSSL/0.9.8a DAV/2
    > DAV: 1,2
    > DAV: <http://apache.org/dav/propset/fs/1>
    > MS-Author-Via: DAV
    > Allow:
    > OPTIONS,GET,HEAD,POST,DELETE,TRACE,PROPFIND,PROPPATCH,COPY,MOVE,LOCK,UNLOCK
    > Content-Length: 0
    > Content-Type: httpd/unix-directory
    >
    > This is the code that gets the response from Apache, the in variable
    > is a BufferedReader connected to the InputStream of the SSLSocket.
    >
    > private String getResponse() throws IOException {
    > String response = "";
    > int c = in.read();
    > char ch;
    > long start = System.currentTimeMillis();
    > while (in.ready() && c != -1) {
    > ch = (char) c;
    > response += ch;
    > c = in.read();
    > }
    > long finish = System.currentTimeMillis();
    > System.out.println(finish - start);
    > ch = (char) c;
    > response += ch;
    > return response;
    >
    > }
    >
    > The output to the console is how long it took to get the whole message
    > from the InputStream. In HTTP (Socket) mode this number is between
    > 0-20 milliseconds but when i use HTTPS (SSLSocket) mode it is approx
    > 5000! This number is independent of the time it takes to handshake
    > with the server.
    >
    > In SSL mode the above method fails actually, because in.ready()
    > returns false the first time, instead i have to modify it to:
    >
    > while ((c = in.read()) != -1) {
    > ch = (char) c;
    > response += ch;
    > System.out.print(ch);
    >
    > }
    >
    > In which case, i get the whole message printed to the console as fast
    > as HTTP mode but on the final evaluation of in.read() it takes the
    > previously stated 5000 ms to finally exit the loop and return the
    > String response.
    >
    > I am certain my Apache server is configured correctly as if i use a
    > web browser to navigate the site in HTTPS the response is instant as
    > like in unsecure HTTP mode.
    >
    > So why am i seeing this 5000 ms delay, when i reach the end of the
    > stream in the BufferedReader?
    >
    > There should be no difference in reading the BufferedReader when using
    > either HTTP or HTTPS mode, given that i've accounted for the time it
    > takes to handshake, right?


    Solution found at http://forum.java.sun.com/thread.jspa?messageID=9537105

    Case closed.
     
    , Mar 7, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news
    Replies:
    1
    Views:
    1,157
    hzhao2
    Nov 20, 2003
  2. Elian Kool
    Replies:
    3
    Views:
    637
    Esmond Pitt
    Mar 11, 2005
  3. Replies:
    4
    Views:
    2,261
    shakah
    Aug 3, 2005
  4. Marco
    Replies:
    0
    Views:
    917
    Marco
    Jan 3, 2006
  5. cvraiden
    Replies:
    0
    Views:
    773
    cvraiden
    Oct 19, 2006
Loading...

Share This Page