tags:

views:

56

answers:

2

such as create an inputStream from URL.openStream();

  1. and read from it. what is the use of this information read?

  2. When we create an URL object using an URL address, actually we are not setting up a connection between our java program and the URL on the internet, right? Since no such connection is built (our 2 are isolated in other words), how do we even read the information from it? in other words, how do we know what it is like?

Sorry, a frosh about network. :p

Thanks for any input!

+3  A: 

URL.openStream() does open a connection to the URL. From the API documentation:

Opens a connection to this URL and returns an InputStream for reading from that connection. This method is a shorthand for:

     openConnection().getInputStream()

It should be obvious by now that a connection of some sort is established. The connection is setup to fetch the contents of the resource that is located at the URL. That of course, depends on the protocol scheme specified in the URL object - HTTP or JAR. The response can be read as bytes from the input stream, just like any other stream.

Vineet Reynolds
Thank you very much for your response!What is the difference between an URL and an InetAddress then? I find them the same somehow...?
Yue
an InetAddress is a type of URL? (all represent an address on the internet, but InetAddress is for host machine specifically.)?
Yue
The InetAddress class is used to represent a single host (IP address). URLs are user to represent any one of the possibly many resources present at the host.
Vineet Reynolds
@Yue: I think InetAdress is used to connect to a Socket, while URL is used to connect to a resource.
tulskiy
@tulskiy - partly right. To use a Socket you *also* need to know 1) TCP or UDP, 2) what port number, and 3) what protocol to talk / expect. A URL specifies all of these, explicitly or implicitly.
Stephen C
@tulskiy, you're partly right, because Sockets contain not just the InetAddress, but also the port number in the internal state. Like Stephen has stated, the URL contains information not just about the socket, but also some metadata about the message semantics. It is a higher abstraction - InetAddress is a OSI layer 3 (network layer) concept, whereas URLs are a layer 7 (application layer) concept.
Vineet Reynolds
+2  A: 

An InetAddress is "example.com" (or 99.99.99.99) but a URL is "http://example.com/wombats.html".

An InetAddress is just the network address a machine, without providing any information about what services that machine supports, or how to connect to them. (The InetAddress class will take care of mapping a DNS name such as "example.com" to an IP address.)

A URL is a location of a resource (e.g. file) and typically implies a way to fetch it. (URL is short for Uniform Resource Locator.) The URL.openStream() method is a simple way to (attempt to) fetch the resource denoted by a URL.

When you create a URL object you do not establish any network connections. The connection is established when you (for example) call URL.openStream().

Stephen C