This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes java Scanner Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "java Scanner" Watch "java Scanner" New topic
Author

java Scanner

krishna prasad gunasekaran
Ranch Hand

Joined: Jul 25, 2006
Posts: 158
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 ).


have a great day,
krishna prasad
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

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?


Steve
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
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 ]

Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
krishna prasad gunasekaran
Ranch Hand

Joined: Jul 25, 2006
Posts: 158
i can connect to www.javaranch.com from my browser. but not thro' java. why?
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
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

Joined: Mar 27, 2003
Posts: 836
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

Joined: Jul 25, 2006
Posts: 158
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
 
subject: java Scanner
 
Similar Threads
Why settimeout is not working?
HttpURLConnection
Shut Down a SSL Enabled JBOSS
how to resolve java.net.ConnectException: Connection refused: connect
Connection Refused Exception