Simple Socket Code but getting Error

P

Page

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 );
}
}
}
 
S

Steve Horsley

Page said:
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
 
P

Page

Steve Horsley said:
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.
 
G

Gordon Beaton

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top