The class below sends a simple request for my webpage and retrieves
the response. But for some reason, the while loop seems to take
forever.
If I uncomment the System.out.print((char)c), I see the response
itself come back quickly, but the program will still stall for another
ten seconds or so, even though all of the response is retrieved.
It didn't help reading the socket with a bufferedreader (or any other
kind of reader for that sake) instead.
The API states that the read() method returns -1 at the end of the
stream, but it seems that it keeps blocking waiting for input data.
How do I get around this problem?
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
public class Connection {
public static void main(String[] args) throws Exception{
Socket s = new Socket("www.stud.ntnu.no", 80);
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
DataInputStream dis = new DataInputStream(s.getInputStream());
dos.writeBytes("GET /~paulsber/index.html" +
HTTP/1.1\r\nHost:www.stud.ntnu.no\r\n\r\n");
dos.flush();
int c = 0;
StringBuffer response = new StringBuffer();
while((c = dis.read()) != -1){
response.append((char)c);
//System.out.print((char)c);
}
System.out.println(response);
}
}
--
Espen Paulsberg
Roedy Green - 17 Apr 2004 06:35 GMT
>Socket s = new Socket("www.stud.ntnu.no", 80);
>DataOutputStream dos = new DataOutputStream(s.getOutputStream());
>DataInputStream dis = new DataInputStream(s.getInputStream());
>dos.writeBytes("GET /~paulsber/index.html" +
>HTTP/1.1\r\nHost:www.stud.ntnu.no\r\n\r\n");
>dos.flush();
Normally you would analyse the length in the http header and read only
that many bytes. see http://mindprod.com/fileio.html for how to use
either raw sockets or the HTTP classes to do this.
The socket is kept alive for possible subsequent transmission..
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Princess Morgiah - 17 Apr 2004 09:26 GMT
> The class below sends a simple request for my webpage and retrieves
> the response. But for some reason, the while loop seems to take
[quoted text clipped - 21 lines]
> dos.writeBytes("GET /~paulsber/index.html" +
> HTTP/1.1\r\nHost:www.stud.ntnu.no\r\n\r\n");
Be sure to add a Connection: close to this - if not, the server might
default to connection: keep-alive and let you wait the specified keep-alive
timeout. Should be something like 15 seconds on Apache by default I believe.
Princess Morgiah
Roedy Green - 18 Apr 2004 00:25 GMT
>Be sure to add a Connection: close to this - if not, the server might
>default to connection: keep-alive and let you wait the specified keep-alive
>timeout. Should be something like 15 seconds on Apache by default I believe.
In HTTP 1.1 keep-alive is the default.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.