Simple Socket Code but getting Error

Discussion in 'Java' started by Page, May 19, 2004.

  1. Page

    Page Guest

    The code below seems simple enough to me but returns the following
    error:
    Error: java.net.NoRouteToHostException: Operation timed out

    Your help is appreciated. The code is run as a Lotus Domino Agent,
    but most of the code is lifted straight out of Sun Microsystem's Core
    Java 2, Volume II.

    import lotus.domino.*;
    import java.io.*;
    import java.net.*;

    public class JavaAgent extends AgentBase {
    Database db;

    public void NotesMain() {

    try {
    System.out.println("start.");
    final int PORT_HTTP = 80;

    Session session = getSession();
    AgentContext agentContext = session.getAgentContext();
    db = agentContext.getCurrentDatabase();

    String strHost;
    long timeout = 1000;
    strHost = "www.google.com";

    System.out.println("variables set.");

    try {
    Socket s = new Socket ( strHost, PORT_HTTP );
    System.out.println("socket open.");
    BufferedReader in = new BufferedReader ( new
    InputStreamReader ( s.getInputStream ( ) ) );

    System.out.println("buffer set.");

    boolean more = true;
    while (more) {
    String line = in.readLine();
    if ( line == null ) {
    more = false;
    s.close();
    } else {
    System.out.println ( line );
    }
    }
    } catch ( IOException e ) {
    System.out.println ( "Error: " + e );
    }
    System.out.println("done."); // This line for debugging
    only.

    } catch ( Exception e ) {
    System.out.println ( "Error: " + e );
    }
    }
    }
     
    Page, May 19, 2004
    #1
    1. Advertising

  2. Page wrote:
    > The code below seems simple enough to me but returns the following
    > error:
    > Error: java.net.NoRouteToHostException: Operation timed out
    >


    No route to host means that there is no IP route to the destination
    address. Since we know that www.google.com exists, and you would
    get a different message if it couldn't resolve the name, I guess
    that you are on a network that douesn't have a route to the internet.

    I guess that for your web browsing, you go through a proxy server?

    So I think you need to look at how to connect through a proxy.
    You might be able to use java's UrlConnection for this (I've
    not used that class). Or if using plain sockets, you will have
    to talk to the proxy yourself.

    Steve
     
    Steve Horsley, May 19, 2004
    #2
    1. Advertising

  3. Page

    Page Guest

    Steve Horsley <> wrote in message news:<c8geqq$qki$2surf.net>...
    > Page wrote:
    > > The code below seems simple enough to me but returns the following
    > > error:
    > > Error: java.net.NoRouteToHostException: Operation timed out
    > >

    >
    > No route to host means that there is no IP route to the destination
    > address. Since we know that www.google.com exists, and you would
    > get a different message if it couldn't resolve the name, I guess
    > that you are on a network that douesn't have a route to the internet.
    >
    > I guess that for your web browsing, you go through a proxy server?
    >
    > So I think you need to look at how to connect through a proxy.
    > You might be able to use java's UrlConnection for this (I've
    > not used that class). Or if using plain sockets, you will have
    > to talk to the proxy yourself.
    >
    > Steve


    Thanks, you are 100% correct. I finally figured that out when the
    code connected successfully on a different computer that doesn't go
    through our network. Once I connected successfully, I started getting
    errors with the portion of the code that reads the file. A friend
    gave me some totally different code that works great, but I was a bit
    frustrated that the code straight from Sun's book was locking up my
    machine. The following line locks up my machine completely.

    String line = in.readLine();

    Although I have working code now, if you happen to know why that line
    gives me so much grief, I would appreciate it.
     
    Page, May 25, 2004
    #3
  4. On 25 May 2004 12:13:51 -0700, Page wrote:
    > The following line locks up my machine completely.
    >
    > String line = in.readLine();
    >
    > Although I have working code now, if you happen to know why that line
    > gives me so much grief, I would appreciate it.


    That line is where you wait for data to arrive from the remote host
    you connected to.

    It will return when one of the following happens:

    - the remote host sends a full line of text
    - the remote host closes the connection

    It appears from your initial post that you are attempting to connect
    to a web server. The web server won't send you any data until you send
    a request, but there is nothing in the code you posted to indicate
    that you've sent a request. If that's the case, the server will most
    likely eventually time out the connection and close it.

    You need to send a proper HTTP request if you want to receive any
    useful data from a web server. Not all protocols work that way
    however, for example some kinds of servers will immediately send you a
    short greeting after you connect.

    For lots of information about HTTP, read this:
    http://ietf.org/rfc/rfc2616.txt

    Perhaps you could say what you're actually trying to do, or what your
    code really looks like.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, May 25, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    5,007
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    1,012
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    582
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    699
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    746
Loading...

Share This Page