Hey all -

I have to create a basic client browser that creates a socket with a webpage and then uses a command similar to :

GET index.html\r\nHTTP/1.1\r\nHost: www.mysite.com\r\n\r\n

To get the source code for the website. I will store each line recieved from the server into an arrayList, which i will then go through and count certain words within the source code.

My first problem is obviously correctly connecting to a server. My attempts so far using anything close to the above within Socket creation always ends up with a UnknownHost Exception. Am i even using this in the correct manner? Does anyone know what I am trying to achieve with that?!

For now, what i did is the following:
1) Created a connection with a socket to my localhost (i currently have WAMP installed and is running a server in the background).
2) When connected, and i have the following code:

try {
	sock = new Socket("localhost", 80);
	out = new PrintWriter(sock.getOutputStream(), true);
	in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
	kbd = new BufferedReader(new InputStreamReader(System.in));
			
	System.out.println("Enter a string: ");
	while (true)
	{
	line = kbd.readLine();
	if (line.equals("."))
	{
	sock.close();
	break;
	}
	out.println(line);
	System.out.println(in.readLine());
				
	}
	}
	catch (UnknownHostException uhe)
	{
	System.out.println("Unknown Host");
	}
	catch (IOException ioe)
	{
	System.out.println("IO error: " + ioe.getMessage());
	}

It asks for user input, if i type anything (except a period . ) i get this output:

Enter a string:
hello
<?xml version="1.0" encoding="iso-8859-1"?>

Which is the first line of HTML from within my localhost root (index.html) file.

How would i go about taking this simple code to make it go through until EOF - storing the values into an arrayList.

My attempt was something like:

while (true)
{
                line = in.readLine();
	arrayList.add(line);
			
	System.out.println(arrayList);
		
}

But this doesn't read/print anything (i would think it would print each time a new line was added to the arrayList).

I am either not using flush() or write() or something else. Any help would be greatly appreciated!

Thanks

Recommended Answers

All 3 Replies

I created the following code have got it reading from the file correctly. My problem is how to read till the EOF? If i don't know when it gets to the end? Is there a way to test this so my while can stop?

Here is the new code:

try {
sock = new Socket("localhost", 80);
out = new PrintWriter(sock.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
out.println("GET \r\nHTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
			
while (i < 20)
{
	line = in.readLine();
	System.out.println(line);
	arrayList.add(line);
	out.flush();
	i++;
		
}
}

If i could figure a way the program knows when it reaches the end of the .html file, that would be great.

Thanks again - and hopefully you can help me out.

Here is my code that works...but ONLY if we know the exact size of the HTML file being read on the server. For example, the following code was used as a test:

<html><title>&nbsp the theatre hello jpg world</title>
<body>&nbsp
hello theatre world again .jpg something.jpg
</body>&nbsp&nbsp
</html>


And my output was:
Total Lines Read: 5
Characters total: 133
Average Characters: 26.6
Smallest line: 7
Largest line: 54
' ' Count: 3
'.jpg' Count: 2
'the' Count: 1

Which is ALL correct... but this is done using hard coded values here:

try {
	sock = new Socket("127.0.0.1", 80);
	out = new PrintWriter(sock.getOutputStream(), true);
	in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
	kbd = new BufferedReader(new InputStreamReader(System.in));
	out.println("GET /index.html\r\nHTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
			
	while (i < 5)
	{
		line = in.readLine();
		System.out.println(line);
		arrayList.add(line);
		out.flush();
		i++;
	}
			
}


We know the file is 5 lines long - so we hard coded 5... how can we NOT do this... i would like to have a flag such as EOF in the while, but im unsure how this would work based on my current code.

Does anyone know how i could achieve this?

I will attach a .java file for viewing - this contains all my code (only about 80 lines or so). Hopefully someone has an idea how i can do this correctly!

Here is my file:

Generally, it's done like this

String line=null;
while ((line = in.readLine()) != null) {
   System.out.println(line);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.