Question

I am trying to create a program that will get data directly from socket instead of going through using HttpURLConnection or HttpClient. Using a socket will give me more freedom to manipulate the data to match the requirements. I am hoping that using sockets, I can preserve the chunk headers sent with each chunk. Following is my code to accomplish that. Unfortunately, even though the code runs without any errors, it runs for at least 40 seconds before it stops. Also, I don't get any InputStream from the server even though I checked that the program was connected to the server.

    Socket socket = new Socket(hostAddr, port);
    InputStream in = socket.getInputStream();
    InputStreamReader isr = new InputStreamReader(in);
    char[] streamBuff = new char[8192];
    StringBuilder receivedData = new StringBuilder();
    int count = 0;
    while((count = isr.read(streamBuff)) > 0)
    {
        receivedData.append(streamBuff, 0, count);
    }
    System.out.println(receivedData);
Was it helpful?

Solution

HTTP/1.1 has persistent connections. You are reading all response chunks in that loop and then block until server times out and closes you TCP connection.

Edit 0:

By the way, you are not sending a request to the server, that's why you don't get anything back. Study the protocol if you want to implement it yourself.

OTHER TIPS

You first need to make a request, either GET or POST. Here's an example to show how to do use openStream() and then read the InputStream:

public class DateWala {

    public static void main(String[] args){

        try {
            URL url = new URL("http://www.google.com");
            InputStream is = url.openStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);

            String s = null;

            while ((s=br.readLine())!=null){
                System.out.println(s);
            }

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top