Results 1 to 8 of 8
  1. #1
    ITSlave is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default 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);
                                    }
                                                }
                               }
    So my question, for now anyway, is how do I pass the GetContentLength back from the thread to void main? And, is GetContentLength the proper method to use?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default Re: Using Threads to Connect to URL

    how do I pass the value returned by getContentLength() back from the thread to void main
    Do you have a design in mind for how you want your program to work?
    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.

  3. #3
    ITSlave is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default 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?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default Re: Using Threads to Connect to URL

    Can the thread return the data directly to the client?

  5. #5
    ITSlave is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default 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

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default 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.

  7. #7
    ITSlave is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default 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.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default Re: Using Threads to Connect to URL

    some way for me to pass that into an array in the main function
    Use a class level defined array or arraylist to save the data. Being at the class level, all methods can see it.
    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

  1. threads
    By brindha1688 in forum New To Java
    Replies: 3
    Last Post: 05-24-2011, 09:07 PM
  2. RSS threads
    By morghul in forum New To Java
    Replies: 4
    Last Post: 10-19-2010, 04:00 PM
  3. Threads!
    By rameshraj in forum Advanced Java
    Replies: 1
    Last Post: 05-04-2008, 05:11 PM
  4. Using threads
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-11-2007, 11:25 AM
  5. Threads
    By one198 in forum Threads and Synchronization
    Replies: 1
    Last Post: 11-20-2007, 07:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •