File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java Scanner

 
krishna prasad gunasekaran
Ranch Hand
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
URLConnection connection = new URL("http://72.14.207.99/").openConnection();
Scanner scanner = new Scanner(connection.getInputStream());
scanner.useDelimiter("\\z");

the highlighted code threw this exception
java.net.SocketException: Operation timed out: connect:could be due to invalid address
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:233)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:220)
at java.net.Socket.connect(Socket.java:536)
at java.net.Socket.connect(Socket.java:486)
at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:397)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:507)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:245)
at sun.net.www.http.HttpClient.New(HttpClient.java:319)
at sun.net.www.http.HttpClient.New(HttpClient.java:331)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:821)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:773)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:698)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:942)
at com.test.action.TextReader.readFile(TextReader.java:43)
at com.test.action.TextReader.main(TextReader.java:67)


1. when i connect to my local or any of my LAN systems (eg:http://localhost:8080/)- working fine. when i go to internet sites - not working.
2. java uses which browser's LAN setting (IE or firefox ).
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by krishna prasad gunasekaran:
1. when i connect to my local or any of my LAN systems (eg:http://localhost:8080/)- working fine. when i go to internet sites - not working.
2. java uses which browser's LAN setting (IE or firefox ).


1. You probably have a firewall blocking access to the outside world. Hard to say for sure.

2. Neither. What settings do mean?
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you actually connect to http://72.14.207.99 from your browser, or even ping it? (Note the latter doesn't always work if the host or any gateway blocks ICMP echos, though here the host allows ping since I just tried it!) If not, then your problem isn't in your application and is likely a network issue as mentioned above. BTW, it's happening in the connection.getInputStream() if you read the stack trace correctly, not in the Scanner constructor. This has nothing do with the Scanner.

Oh, and Java uses the IP settings of the NIC it connects to the LAN on. It knows nothing else unless you tell it.
[ October 09, 2008: Message edited by: Charles Lyons ]
 
krishna prasad gunasekaran
Ranch Hand
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i can connect to www.javaranch.com from my browser. but not thro' java. why?
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by connect? Can it not resolve hostname to IP? Does it open a socket to the right port (e.g. 80 for HTTP)? Code example please...
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also check you don't have a software firewall (inc. Windows Firewall) blocking the Java program (e.g. java.exe) from opening new sockets to the Internet. Common software firewalls do this by default, though most ask you on the first occasion. I usually let java.exe do what it wants ("allow always"), since I trust all the Java apps I run. This would explain why your browser executable is allowed through, but not your Java app. If in doubt, temporarily disable all firewalls and try again.
 
krishna prasad gunasekaran
Ranch Hand
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
URLConnection connection = new URL("http://www.javaranch.com/").openConnection();
String text = new Scanner( connection.getInputStream()). useDelimiter("\\Z").next();


the scanner constructor throws the following exception

java.net.UnknownHostException: www.javaranch.com
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at scanner.TextReader.readFile(TextReader.java:21)
at scanner.TextReader.main(TextReader.java:44)
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic