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

Edited 6 Years Ago by tones1986: deleted white space in code examples

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:

Edited 6 Years Ago by tones1986: n/a

Attachments
import java.net.*;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.*;

public class EchoClient {
	static ArrayList<String> arrayList = new ArrayList<String>();
	static int linesRead,
	    charsTotal,
	    shortestLine,
	    longestLine;
	 static double aveChars;
	public static void main(String[] args)
	{
		Socket sock;
		PrintWriter out;
		BufferedReader in;
		BufferedReader kbd;
		String line;
		
		int i = 0;
		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++;
			}
			
		}
		catch (UnknownHostException uhe)
		{
			System.out.println("Unknown Host");
		}
		catch (IOException ioe)
		{
			System.out.println("IO error: " + ioe.getMessage());
		}
		calcLineInfo();
	}
	
	
	
	public static void calcLineInfo() {
		int smallest = 100000, largest = 0, lineSize;
		linesRead = arrayList.size();
		for (int i = 0; i < arrayList.size(); i++)
		{
			charsTotal += arrayList.get(i).length();
		}	
		aveChars = (double) charsTotal / linesRead;
		for (int i = 0; i < arrayList.size(); i++)
		{
			lineSize = arrayList.get(i).length();
			if (lineSize < smallest)
				smallest = lineSize;
			if (lineSize > largest)
				largest = lineSize;
			
		}
		
		int string1Count = 0,
			string2Count = 0,
			string3Count = 0;
			
		for (int j = 0; j < arrayList.size(); j++)
		{
		String string1 = "&nbsp", string2 = ".jpg", string3 = "the";
		
		String phrase = arrayList.get(j);
		String delims = "[\\/ ''<>!@=$\"?]";
		
		String[] tokens = phrase.split(delims);
		for (int i = 0; i < tokens.length; i++)
		{
		    System.out.println(tokens[i]);
		    int index1 = tokens[i].indexOf(string1);
		    int index2 = tokens[i].indexOf(string2);
		    if (index1 != -1)
		    	string1Count++;
		    else if (index2 != -1)
		    	string2Count++;
		    else if (tokens[i].equals(string3))
		    	string3Count++;
		}
		}
		System.out.println("Total Lines Read: " + linesRead);
		System.out.println("Characters total: " + charsTotal);
		System.out.println("Average Characters: " + aveChars);
		System.out.println("Smallest line: " + smallest);
		System.out.println("Largest line: " + largest);
		System.out.println("'&nbsp' Count: " + string1Count);
		System.out.println("'.jpg' Count: " + string2Count);
		System.out.println("'the' Count: " + string3Count);

	}
	  
}

Generally, it's done like this

String line=null;
while ((line = in.readLine()) != null) {
   System.out.println(line);
}
This article has been dead for over six months. Start a new discussion instead.