Results 1 to 8 of 8
Thread: Using Threads to Connect to URL
- 10-01-2011, 08:30 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 6
- Rep Power
- 0
Using Threads to Connect to URL
My assignment is to accept an unknown number of URLs from a client and deliver to a server. The server needs to accept these over a socket, launch a threaded worker to do the work of connecting to the URL and counting the characters from that URL, and finally, return the name of the URL and the number of characters to the client.
Ex. www.smu.edu 1234 characters, accuracy of # of characters isn't of paramount concern to the professor as far as non-ASCII vs. ASCII.
Here is the server code I have so far:
Java Code:import java.net.*; import java.io.*; import java.util.*; public class JokerServer extends Thread { private String address; public JokerServer (String s) { address = s; } public void run() { try { URL url = new URL(address); try { URLConnection connection = url.openConnection(); connection.getContentLength(); } catch (IOException e) { System.out.println(e); } } catch (MalformedURLException e) { System.out.println(e); } } public static void main(String[] args) { List<String> sl = new ArrayList<String>(); try { ServerSocket sock = new ServerSocket (6013); //now listen for connections while (true) { Socket client = sock.accept(); InputStream in = client.getInputStream(); BufferedReader bin = new BufferedReader(new InputStreamReader(in)); //read the URL from the socket String line; while ( (line = bin.readLine()) !=null) sl.add(line); //close the socket and resume //listening for connections client.close(); } } catch (IOException ioe) { System.err.println(ioe); } } }
- 10-01-2011, 03:42 PM #2
Re: Using Threads to Connect to URL
how do I pass the value returned by getContentLength() back from the thread to void main
You need that before you start writing code. Unless you are just testing out various techniques that you want to use in the program.
You have written some bits of code but I don't see how they fit together to solve your problem.
- 10-02-2011, 06:04 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 6
- Rep Power
- 0
Re: Using Threads to Connect to URL
So the idea of the design is this, the main function starts the socket. It then accepts a connection, reads in data from the clients, and stores that data in an array. After, the idea is to have a For loop that will call an instance of the thread for every URL in the array. That For loop will wait for the thread, and put the # of characters in a separate array. After it does this for all URLs, I'll have another For loop that sends the data back to the client.
I was attempting to finish the thread class before going back and finishing the main. I could not figure out how to pass information from the thread, the # of characters from the URL, back to the main function. So currently, I pass the thread the URL from the main function, create the URL object, and open the connection. Is GetContentLength going to give me the # of characters or is there some other function that will do this. And how do I pass the # of characters back to the main function?
- 10-02-2011, 03:10 PM #4
Re: Using Threads to Connect to URL
Can the thread return the data directly to the client?
- 10-02-2011, 04:10 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 6
- Rep Power
- 0
Re: Using Threads to Connect to URL
No. Here is the full text of the assignment:
• Modify your client so that the client collects valid URLs until the user types GO.
• When the client types GO, the client should deliver its set of URLs to the server.
• For each URL, the server should launch a threaded worker to do the work of
connecting to the URL and counting the characters from a URL. Pass a URL string to
a threaded worker who should then, in its own thread, connect to the URL and count
the characters.
• When all threaded workers have completed their tasks, the server should send back to
the client the names of all the URLs and the number of characters for each. Thus, the
client should display something like:
http://www.smu edu 1234 characters
Google 8876 characters
www.zondo.com 234 characters
- 10-02-2011, 04:42 PM #6
Re: Using Threads to Connect to URL
Have a collection of some kind that the threads can put data into when they have collected it.
When all threads are done, the data can be sent back to the requestors.
- 10-02-2011, 07:33 PM #7
Member
- Join Date
- Sep 2011
- Posts
- 6
- Rep Power
- 0
Re: Using Threads to Connect to URL
Do you have some kind of reference or example code for this? Is there some way for me to pass that into an array in the main function? I've done some searching, but my google-fu has not been able to find an example of this to use as a reference.
- 10-02-2011, 07:44 PM #8
Re: Using Threads to Connect to URL
some way for me to pass that into an array in the main function
Have a method that will store items in the array or arraylist.
Have the threads call that method when they get data to be saved.
When all threads have ended, the data is in the array/arraylist and can be sent.
Similar Threads
-
threads
By brindha1688 in forum New To JavaReplies: 3Last Post: 05-24-2011, 09:07 PM -
RSS threads
By morghul in forum New To JavaReplies: 4Last Post: 10-19-2010, 04:00 PM -
Threads!
By rameshraj in forum Advanced JavaReplies: 1Last Post: 05-04-2008, 05:11 PM -
Using threads
By Java Tip in forum Java TipReplies: 0Last Post: 12-11-2007, 11:25 AM -
Threads
By one198 in forum Threads and SynchronizationReplies: 1Last Post: 11-20-2007, 07:15 PM
Bookmarks