Results 1 to 7 of 7
  1. #1
    IceNine is offline Member
    Join Date
    Mar 2013
    Posts
    33
    Rep Power
    0

    Default A Simple Client Program

    So i am suppoused to make a simple client program that retrieves the webpage myweb.cwpost.liu.edu/cmalinow/cwptoc.html but every time i try it i keep getting this error. it works if i do other websties but not this one, can someone shed some light on why I am having this problem

    Exception in thread "main" java.net.UnknownHostException: myweb.cwpost.liu.edu/cmalinow/cwptoc.html
    at java.net.AbstractPlainSocketImpl.connect(AbstractP lainSocketImpl.java:178)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.j ava:157)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.j ava:391)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:208)
    at webget.WebGet.main(WebGet.java:45)
    Java Result: 1

    Here is my code
    Java Code:
    package webget;
    
    import java.io.InputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.util.Scanner;
    
    /**
     *
     * 
     */
    public class WebGet {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws IOException
       {
          // Get command-line arguments
    
          String host;
          String resource;
    
          if (args.length == 2)
          {
             host = args[0];
             resource = args[1];
          }
          else
          {
             System.out.println("Getting / from horstmann.com");
             host = "myweb.cwpost.liu.edu/cmalinow/cwptoc.html";
             resource = "/";
          }
    
          // Open socket
    
          final int HTTP_PORT = 80;
          Socket s = new Socket(host, HTTP_PORT);
    
          // Get streams
          
          InputStream instream = s.getInputStream();
          OutputStream outstream = s.getOutputStream();
    
          // Turn streams into scanners and writers
    
          Scanner in = new Scanner(instream);
          PrintWriter out = new PrintWriter(outstream);      
    
          // Send command
    
          String command = "GET " + resource + " HTTP/1.1\n" 
             + "Host: " + host + "\n\n";
          out.print(command);
          out.flush();
    
          // Read server response
    
          while (in.hasNextLine())
          {
             String input = in.nextLine();
             System.out.println(input);
          }
    
          // Always close the socket at the end
    
          s.close();      
       }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,017
    Rep Power
    6

    Default Re: A Simple Client Program

    Quote Originally Posted by IceNine View Post
    it works if i do other websties but not this one, can someone shed some light on why I am having this problem

    Exception in thread "main" java.net.UnknownHostException: myweb.cwpost.liu.edu/cmalinow/cwptoc.html
    Well, I can't be certain but it seems you may have answered the question yourself.

    Hint 1. Works at other sites but not this one.
    Hint 2. Error says UnknownHostException

    Perhaps your URL is incorrect?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: A Simple Client Program

    Have you tried setting host to the actual host name - myweb.cwpost.liu.edu?

  4. #4
    IceNine is offline Member
    Join Date
    Mar 2013
    Posts
    33
    Rep Power
    0

    Default Re: A Simple Client Program

    I have tried that and that also works after i connect to the host would i have to open another socket and connect to the myweb.cwpost.liu.edu/cmalinow/cwptoc.html?

    Quote Originally Posted by pbrockway2 View Post
    Have you tried setting host to the actual host name - myweb.cwpost.liu.edu?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: A Simple Client Program

    after i connect to the host would i have to open another socket and connect to the myweb.cwpost.liu.edu/cmalinow/cwptoc.html?
    How would you do that? Given that "myweb.cwpost.liu.edu/cmalinow/cwptoc.html" gives you the UnknownHostException.

    My (limited) understanding is that you create a socket with a host (name or ip) and port. The host computer assigns you a socket and the communication takes place with you using the socket's input and output streams. The thing is that all you are dealing with is a host and a port.

    From the look of your code you are trying to print the contents of a web page. In that case what you have to start with is not a host name (or ip) but a whole url: ie host, port (80) and all the rest of the url that specifies the page. For that I'd use a URLConnection or, more simply, a URL.

  6. #6
    IceNine is offline Member
    Join Date
    Mar 2013
    Posts
    33
    Rep Power
    0

    Default Re: A Simple Client Program

    I have tried a URLConnection and when i do i get this error

    Exception in thread "main" java.net.MalformedURLException: no protocol: myweb.cwpost.liu.edu/cmalinow/cwptoc.html
    at java.net.URL.<init>(URL.java:583)
    at java.net.URL.<init>(URL.java:480)
    at java.net.URL.<init>(URL.java:429)
    at urlget.URLGet.main(URLGet.java:41)
    Java Result: 1

    Quote Originally Posted by pbrockway2 View Post
    How would you do that? Given that "myweb.cwpost.liu.edu/cmalinow/cwptoc.html" gives you the UnknownHostException.

    My (limited) understanding is that you create a socket with a host (name or ip) and port. The host computer assigns you a socket and the communication takes place with you using the socket's input and output streams. The thing is that all you are dealing with is a host and a port.

    From the look of your code you are trying to print the contents of a web page. In that case what you have to start with is not a host name (or ip) but a whole url: ie host, port (80) and all the rest of the url that specifies the page. For that I'd use a URLConnection or, more simply, a URL.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: A Simple Client Program

    Exception in thread "main" java.net.MalformedURLException: no protocol: myweb.cwpost.liu.edu/cmalinow/cwptoc.html
    That exception means exactly what it says: when you constructed the URL instance you did not specify the protocol ("http://").

    I guess when I said in #5 "a whole url: ie host, port (80) and all the rest of the url that specifies the page" I should have said "a whole url: ie the protocol, host, port (80) and all the rest of the url that specifies the page."

    You don't say what code gave that runtime exception, but if you are following the code in either of the links I suggested, notice how the oracle URL instance is constructed from a string which begins "http://".

Similar Threads

  1. Replies: 3
    Last Post: 04-04-2012, 01:38 AM
  2. Replies: 6
    Last Post: 01-09-2012, 10:30 PM
  3. Simple Client (2) - Server program
    By Reztem in forum New To Java
    Replies: 2
    Last Post: 01-09-2012, 03:05 AM
  4. Simple Socket program: Java Client- C server
    By pimmling in forum New To Java
    Replies: 1
    Last Post: 11-08-2010, 02:27 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
  •