The term network programming refers to writing programs that execute across multiple devices (computers), in which the devices are all connected to each other using a network.
The java.net package of the J2SE APIs contains a collection of classes and interfaces that provide the low-level communication details, allowing you to write programs that focus on solving the problem at hand.
The java.net package provides support for the two common network protocols:
- TCP: TCP stands for Transmission Control Protocol, which allows for reliable communication between two applications. TCP is typically used over the Internet Protocol, which is referred to as TCP/IP.
- UDP: UDP stands for User Datagram Protocol, a connection-less protocol that allows for packets of data to be transmitted between applications.
This site gives good understanding on the following two subjects:
- Socket Programming: This is most widely used concept in Networking and it has been explained in very detail.
- URL Processing: This would be covered separately. Click here to learn about URL Processing in Java language.
Url Processing
URL stands for Uniform Resource Locator and represents a resource on the World Wide Web, such as a Web page or FTP directory.
This section shows you how to write Java programs that communicate with a URL. A URL can be broken down into parts, as follows:
protocol://host:port/path?query#ref
Examples of protocols include HTTP, HTTPS, FTP, and File. The path is also referred to as the filename, and the host is also called the authority.
The following is a URL to a Web page whose protocol is HTTP:
http://www.amrood.com/index.htm?language=en#j2se
Notice that this URL does not specify a port, in which case the default port for the protocol is used. With HTTP, the default port is 80.
URL Class Methods:
The java.net.URL class represents a URL and has complete set of methods to manipulate URL in Java.
The URL class has several constructors for creating URLs, including the following:
SN | Methods | Description |
1 | public URL(String protocol, String host, int port, String file) throws MalformedURLException. | Creates a URL by putting together the given parts. |
2 | public URL(String protocol, String host, String file) throws MalformedURLException | Identical to the previous constructor, except that the default port for the given protocol is used. |
3 | public URL(String url) throws MalformedURLException | Creates a URL from the given String |
4 | public URL(URL context, String url) throws MalformedURLException | Creates a URL by parsing the together the URL and String arguments. |
|
The URL class contains many methods for accessing the various parts of the URL being represented.
Some of the methods in the URL class include the following:
SN | Methods | Description |
1 | public String getPath() | Returns the path of the URL. |
2 | public String getQuery() | Returns the query part of the URL. |
3 | public String getAuthority() | Returns the authority of the URL. |
4 | public int getPort() | Returns the port of the URL. |
5 | public int getDefaultPort() | Returns the default port for the protocol of the URL. |
6 | public URLConnection openConnection() throws IOException | Opens a connection to the URL, allowing a client to communicate with the resource. |
|
URLConnections Class Methods:
The openConnection() method returns a java.net.URLConnection, an abstract class whose subclasses represent the various types of URL connections.
For example:
- If you connect to a URL whose protocol is HTTP, the openConnection() method returns an HttpURLConnection object.
- If you connect to a URL that represents a JAR file, the openConnection() method returns a JarURLConnection object.
- etc...
The URLConnection class has many methods for setting or determining information about the connection, including the following:
SN | Methods | Description |
1 | Object getContent() | Retrieves the contents of this URL connection. |
2 | Object getContent(Class[] classes) | Retrieves the contents of this URL connection. |
3 | String getContentEncoding() | Returns the value of the content-encoding header field. |
4 | int getContentLength() | Returns the value of the content-length header field. |
5 | String getContentType() | Returns the value of the content-type header field. |
6 | int getLastModified() | Returns the value of the last-modified header field. |
7 | long getExpiration() | Returns the value of the expires header field. |
8 | long getIfModifiedSince() | Returns the value of this object's ifModifiedSince field. |
9 | public void setDoInput(boolean input) | Passes in true to denote that the connection will be used for input. The default value is true because clients typically read from a URLConnection. |
10 | public void setDoOutput(boolean output) | Passes in true to denote that the connection will be used for output. The default value is false because many types of URLs do not support being written to. |
11 | public InputStream getInputStream() throws IOException | Returns the input stream of the URL connection for reading from the resource. |
12 | public OutputStream getOutputStream() throws IOException | Returns the output stream of the URL connection for writing to the resource |
13 | public URL getURL() | Returns the URL that this URLConnection object is connected to |
|