What is networking in java

What You May Already Know About Networking in Java

The word networking strikes fear in the hearts of many programmers. Fear not! Using the networking capabilities provided in the Java environment is quite easy. In fact, you may be using the network already without even realizing it!

Loading Applets from the Network

If you have access to a Java-enabled browser, you have undoubtedly already executed many applets. The applets you’ve run are referenced by a special tag in an HTML file — the tag. Applets can be located anywhere, whether on your local machine or somewhere out on the Internet. The location of the applet is completely invisible to you, the user. However, the location of the applet is encoded within the tag. The browser decodes this information, locates the applet, and runs it. If the applet is on some machine other than your own, the browser must download the applet before it can be run.

This is the highest level of access that you have to the Internet from the Java development environment. Someone else has taken the time to write a browser that does all of the grunt work of connecting to the network and getting data from it, thereby enabling you to run applets from anywhere in the world.

For more information:
The «Hello World!» Application shows you how to write your first applet and run it.

Читайте также:  Notepad работа с python

The Java Applets trail describes how to write Java applets from A to Z.

Loading Images from URLs

If you’ve ventured into writing your own Java applets and applications, you may have run into a class in the java.net package called URL. This class represents a Uniform Resource Locator and is the address of some resource on the network. Your applets and applications can use a URL to reference and even connect to resources out on the network. For example, to load an image from the network, your Java program must first create a URL that contains the address to the image.

This is the next highest level of interaction you can have with the Internet — your Java program gets an address of something it wants, creates a URL for it, and then uses some existing function in the Java development environment that does the grunt work of connecting to the network and retrieving the resource.

For more information:
How to Use Icons shows you how to load an image into your Java program (whether applets or applications) when you have its URL. Before you can load the image you must create a URL object with the address of the resource in it.

Working with URLs, the next lesson in this trail, provides a complete discussion about URLs, including how your programs can connect to them and read from and write to that connection.

Источник

Java Networking Overview

The Java Networking API ( java.net ) provides the interfaces/classes for the following functions:

The goal of this document is to provide a high-level overview of the facilities the java.net package provides. For details about interfaces, classes, or factories, see the networking API. For basic networking concepts, see Trail: Custom Networking in the Java Tutorial.

Addressing

java.net provides the following addressing-related classes:

  • InetAddress
  • Inet4Address
  • Inet6Address
  • SocketAddress
  • InetSocketAddress

For IP addressing, three classes are provided: InetAddress , Inet4Address , and Inet6Address . InetAddress represents an IP address, which is either a 32- or 128-bit unsigned number used by IP, the lower-level protocol on which protocols like TCP and UDP are built. To represent 32-bit IPv4 address, Inet4Address is provided. (An IPv4 address has the familiar form nnn.nnn.nnn.nnn , where n is an integer; e.g., 192.0.2.254 ). It is a subclass of InetAddress . To represent 128-bit IPv6 addresses, Inet6Address is provided. It is also a subclass of InetAddress .

For socket addressing, two classes are provided: SocketAddress and InetSocketAddress . SocketAddress is an abstract socket address, independent of a specific protocol. It is intended for subclassing for a specific protocol. InetSocketAddress below is an example. InetSocketAddress is a subclass of SocketAddress ; it represents an IP socket address. It can include an IP address (e.g., 192.0.2.254 ) and port (e.g., 80 ); a hostname (e.g., example.com ) and port (e.g., 1000 ); or port only (e.g., 1010 ). In the latter case, a wildcard IP address is assumed.

Making TCP Connections

These classes are related to making normal TCP connections:

For simple connections between a client and a server, ServerSocket and Socket are all that you will probably need.

ServerSocket represents the socket on a server that waits and listens for requests for service from a client. Socket represents the endpoints for communication between a server and a client. When a server gets a request for service, it creates a Socket for communication with the client and continues to listen for other requests on the ServerSocket . The client also creates a Socket for communication with the server. The sequence is shown below:

Once the connection is established, getInputStream() and getOutputSteam() may be used in communication between the sockets

Sending/Receiving Datagram Packets via UDP

The following are related to sending and receiving datagram packets via UDP.

DatagramPacket represents a datagram packet. Datagram packets are used for connectionless delivery and normally include destination address and port information. DatagramSocket is a socket used for sending and receiving datagram packets over a network via UDP. A DatagramPacket is sent from a DatagramSocket by calling the send(. ) method of DatagramSocket with DatagramPacket as the argument: send(DatagramPacket dp) . receive(DatagramPacket dp) is use for receiving a DatagramPacket . (The MulticastSocket class may be used for sending/receiving a DatagramPacket to a mulitcast group. It is a subclass of DatagramSocket that adds functionality for multicasting.)

Locating/Identifying Network Resources

These classes are related to locating or identifying network resources:

  • URI
  • URL
  • URLClassLoader
  • URLConnection
  • URLStreamHandler
  • HttpURLConnection
  • JarURLConnection

The most commonly used classes are URI , URL , URLConnection , and HttpURLConnection .

URI represents a Uniform Resource Identifier for a resource; it is an identifier for a resource but not necessarily a locator for that resource. URL represents a Uniform Resource Locator for a resource. URLs are a subset of URIs, though the class URL is not a subclass of the URI class. In short, a URL tells how to access the resource, while a URI may or may not. The Uniform Resource Name ( URN ) is another subset of URI . No Java class exists for it.

URLConnection is the abstract superclass of all classes that represent a connection between an application and a network resource identified by a URL . Given a URL and hence a protocol, URL.openConnection() returns an instance of the appropriate implementation of URLConnection for the protocol. (The protocol is known from the URL .) The instance provides the means with the method URLConnection.connect() to actually open the connection and access the URL .

HttpURLConnection is the most commonly used implementation of URLConnection . It is for http protocol, the protocol used for accessing content on web servers. In the above diagram, if the access protocol for the URL were http , then an instance of HttpURLConnectio n would be returned by the openConnection() method.

Security

Security includes authentication- and permissions-related classes. Authentication relates to user authentication and involves username and password checking. Authentication of a user may be required in a number of situations, such as when a user tries to access a URL. Permissions relate to what actions may be performed; e.g., unless the NetPermission object » setDefaultAuthenticator » exists, then invoking the method Authenticator.setDefault(Authenticator a) will cause a security exception.

Authentication

Some proxies and origin servers require authentication information, using authentication schemes such as BASIC and DIGEST. For instance, when connecting with http via a proxy and the proxy requires authentication, we call the Authenticator class to obtain usernames, passwords, and other items needed to authenticate. The following classes relate to authentication:

In addition to methods for user authentication, the abstract class Authenticator also has methods for querying about the authentication being requested (see getRequestingXXX() ). It is typically subclassed and an instance of the subclass is registered with the system by calling setDefault(Authenticator a) . (Note that if there is a security manager, it checks to see that the security policy permits the NetPermission » setDefaultAthenticator «.) Then, when the system requires authentication, it will call a method such as requestPasswordAuthentication() .

PasswordAuthentication is simply a data holder for a user name and a password.

Permissions

A SocketPermission consists of a host, with optional port range, and a set of actions that may be performed on that host: connect , accept , listen and/or resolve . It includes methods to determine if one SocketPermission is equal to another or implies another Permission . A SocketPermission may be included in a PermissionCollection for easy checking if a permission exists.

NetPermission is a class for various named network permissions. Currently there are three: setDefaultAuthenticator , as mentioned above; requestPasswordAuthentication ; and specifyStreamHandler . A NetPermission may be included in a PermissionCollection for easy checking if a permission exists.

For more information about permissions, see the Permissions topic.

Источник

Trail: Custom Networking

The Java platform is highly regarded in part because of its suitability for writing programs that use and interact with the resources on the Internet and the World Wide Web. In fact, Java-compatible browsers use this ability of the Java platform to the extreme to transport and run applets over the Internet.

This trail walks you through the complexities of writing Java applications and applets that can be used on the Internet.

Overview of Networking has two sections. The first describes the networking capabilities of the Java platform that you may already be using without realizing that you are using the network. The second provides a brief overview of networking to familiarize you with terms and concepts that you should understand before reading how to use URLs, sockets, and datagrams.

Working With URLs discusses how your Java programs can use URLs to access information on the Internet. A URL (Uniform Resource Locator) is the address of a resource on the Internet. Your Java programs can use URLs to connect to and retrieve information over a network. This lesson provides a more complete definition of a URL and shows you how to create and parse a URL, how to open a connection to a URL, and how to read from and write to that connection.

All About Sockets explains how to use sockets so that your programs can communicate with other programs on the network. A socket is one endpoint of a two-way communication link between two programs running on the network. This lesson shows you how a client can connect to a standard server, the Echo server, and communicate with it via a socket. It then walks you through the details of a complete client/server example, which shows you how to implement both the client side and the server side of a client/server pair.

All About Datagrams takes you step by step through a simple client/server example that uses datagrams to communicate. It then challenges you to rewrite the example using multicast socket instead.

Programmatic Access to Network Parameters explains why you might want to access network interface parameters and how to do so. It gives examples of how to list all the IP addresses assigned to the machine as well as other useful information such as whether the interface is running.

Working With Cookies discusses how cookies are used to create a session between a client and server, and how you can take advantage of cookies in your HTTP URL connections.

Note that communications over the network are subject to approval by the current security manager. The Security Manager describes what a security manager is and how it impacts your applications. For general information about the security features provided by the JDK, refer to Security Features in Java SE .

The example programs in the following lessons that cover URLs, sockets, and datagrams are standalone applications, which, by default, have no security manager. If you convert these applications to applets, they may be unable to communicate over the network, depending on the browser or viewer in which they are running. See What Applets Can and Cannot Do for information about the security restrictions placed on applets.

Источник

Оцените статью