Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Simple Socket Code but getting Error

Reply
Thread Tools

Simple Socket Code but getting Error

 
 
Page
Guest
Posts: n/a
 
      05-19-2004
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 );
}
}
}
 
Reply With Quote
 
 
 
 
Steve Horsley
Guest
Posts: n/a
 
      05-19-2004
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
 
Reply With Quote
 
 
 
 
Page
Guest
Posts: n/a
 
      05-25-2004
Steve Horsley <(E-Mail Removed)> wrote in message news:<c8geqq$qki$(E-Mail Removed)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.
 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      05-25-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 1 02-03-2009 06:20 AM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 0 02-01-2009 12:45 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM



Advertisments