newbie question - simple java socket example - it works but how do i . . .

S

stan k.

The example below works fine. It browses a url and then prints it out
one line at a time to the screen as the user presses enter. This might
sound like too easy a question but how to I adjust this to make it
automatically display itself to the screen, or better yet, copy the
whole contents to a string variable...

I am totally stuck on this, sorry if i am asking a stupid question
here, maybe
it's just a small change needed

------------



import java.io.*;
import java.net.*;

public class webpagedownloader{

public static void main(String[] args) throws IOException {
Socket sock = null;
PrintWriter out = null;
BufferedReader in = null;

try{
sock = new Socket("www.whatever", 80);
out = new PrintWriter(sock.getOutputStream(), true);
out.print("GET /yourlife/deleteme.html \r\n User-Agent: Mozilla/4.0
(compatible; MSIE 6.0; Windows NT 5.1) \r\n HOST: www.whatever.com
\r\n\r\n");
in = new BufferedReader(new
InputStreamReader(sock.getInputStream()));
}catch(UnknownHostException e){
System.err.println("can't find host");
System.exit(1);
}catch(IOException e){
System.err.println("Couldn't make i/o connection to server");
System.exit(1);
}//try-catch-catch

BufferedReader stdIn = new BufferedReader(new
InputStreamReader(System.in));
String userInput;

while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}//while

out.close();
in.close();
stdIn.close();
sock.close();

}//main()

}
 
M

Mark

Stan, no positive; but try taking out

while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}//while

and changing it to:

while (true) {
String theLine = in.readLine();
if (theLine.equals(".")) break;
System.out.println("echo: " + theLine);
}

I think that might solve your problem???
 
S

stan k.

Actually, my original example doesn't completely work (sorry about that)...
The web page *is* printed out the the screen one line at a time
everytime the user presses enter however It's suppossed to stop as
soon as it detects a null (end of the web page) but it doesn't. . .

I think maybe my combination of \r\n's within the GET request
string might be off... perhaps that's why your change suggestions
didn't seem to work either - when I tried doing what you said
the program seemingly freezes or at least never shows anything
at all on the screen...

Stan
 
G

Gordon Beaton

Actually, my original example doesn't completely work (sorry about that)...
The web page *is* printed out the the screen one line at a time
everytime the user presses enter however It's suppossed to stop as
soon as it detects a null (end of the web page) but it doesn't. . .

Your original code doesn't check for null on the connection to the web
server, it checks for null on stdin (the user input).

readLine() will indeed return null when the connection is closed by
the other end, but HTTP 1.1 lets you make multiple requests using the
same connection, so the web server won't necessarily close the
connection after transmitting the web page.

You can tell it to do so by including "Connection: close" as part of
your request header, or you can use the Content-Length attribute in
the reply to know how much data to expect.

Have a look at rfc2616:
http://www.w3.org/Protocols/rfc2616/rfc2616.html

Sections 4.3-4.4 talk about how the end of the message is indicated.
Note that HTTP doesn't use a '.' to indicate EOM, as the other
response suggested.

/gordon
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top