0% found this document useful (0 votes)
90 views9 pages

Unit 14 Networking in Java: Structure

This document discusses networking in Java. It covers the java.net package which allows Java programs to communicate over networks. Key classes discussed include URL, URLConnection, and DatagramPacket. It describes how to create URL objects to represent web addresses and manipulate them. The document also discusses debugging Java programs using the sun.tools.debug library and DebuggerCallback interface. It lists the various remote object classes used to handle objects like arrays, threads, and variables during debugging.

Uploaded by

Pavan Thakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views9 pages

Unit 14 Networking in Java: Structure

This document discusses networking in Java. It covers the java.net package which allows Java programs to communicate over networks. Key classes discussed include URL, URLConnection, and DatagramPacket. It describes how to create URL objects to represent web addresses and manipulate them. The document also discusses debugging Java programs using the sun.tools.debug library and DebuggerCallback interface. It lists the various remote object classes used to handle objects like arrays, threads, and variables during debugging.

Uploaded by

Pavan Thakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Object Oriented Programming – Java Unit 14

Unit 14 Networking in Java


Structure:
14.1 Introduction
Objectives
14.2 Networking in Java
14.3 URL Objects
14.4 Summary
14.5 Terminal Questions
14.6 Answers

14.1 Introduction
In the last unit, we have discussed JSP and Servlets. In this unit, we shall
discuss Networking features of Java. The Net class library, java.net,
provides a high-level interface for connections over the Internet or any other
kind of TCP/IP network. Using these interfaces makes it possible for a Java
application to connect to existing network services, provide its own network
services, and even allow for multiuser, network-aware games over the
Internet. Whereas these can be quite complicated to program in other
languages, the java.net package allows the basics of socket connection to
be set up in as little as 10 lines of code.
Objectives:
After studying this unit, you should be able to:
 explain Networking in Java
 describe URL objects

14.2 Networking in Java


Java’s class library includes the java.net package, which makes it possible
to communicate over a network with Java programs. The package provides
cross-platform abstractions for simple networking operations, including
connecting and retrieving files by using Common Web protocols and
creating basic UNIX like sockets.
Java applets cannot read or write from the disk on the machine where the
browser is running. Java applets cannot connect to system other than the
one on which they were originally stored. By two ways, communication can
done to the system:

Sikkim Manipal University – DDE B2119 Page No. 226


Object Oriented Programming – Java Unit 14
 getInputStream() is a method that opens a connection to an URL and
enables to extract data from that connection.
 The socket classes Socket and ServerSocket enables to open standard
socket connections to hosts and read to and write from those connection.
The following sections describe the classes in the java.net package.
The ContentHandler Class
ContentHandler is used to read a stream of data that is generated by a
network connection and produce an object. The exact content handler
called depends on the MIME type of the remote data.
ContentHandler should not be called directly. This task is much better
performed by URL.getContent() or URLConnection.getContent(), which
simplify the process of reading a stream of data.
The DatagramPacket Class
DatagramPacket is used to both send and receive datagram packets. A
datagram packet is a segment of data containing packet data, packet length,
Internet address, and port. This allows programming at the IP packet level.
DatagramPacket methods are used to determine the address, length, and
port of the datagram packet, as well as retrieve the data itself.
The DatagramSocket Class
DatagramSocket is used for creating UDP-based connections over a
network. Services such as Sun’s Network File System (NFS) use UDP as
the underlying protocol. Java applications can also create and/or use
UDP-based network connections by making use of this class.
The InetAddress Class
InetAddress makes an object of Internet addresses. This makes them able
to be manipulated as String objects or within other network classes.
Self Assessment Questions
1. _____ package contains all the network related classes and methods of
Java.
2. _____ method opens a connection to an URL and enables to extract
data from that connection.
3. _____ class is used to read a stream of data that is generated by a
network connection and produce an object.

Sikkim Manipal University – DDE B2119 Page No. 227


Object Oriented Programming – Java Unit 14
14.3 URL Objects
Addresses on WWW are represented in Java by URL objects. A URL
represents any type of information which has to be linked with web. The
URL is set up to perform certain activities when a connection is made to it,
such as loading a web page or downloading a file. These URL classes are
similar to sockets in that they allow easy integration of network applications
into a program. URLs are a bit more user-friendly to work with than the
sockets.
The URL Class
The URL class transforms a URL string into an object that can then be
manipulated using associated methods. These methods serve purposes
such as getting the filename, protocol, and hostname. URL also has
methods that can open and maintain the data connection of an input stream.
This is much easier to use than the socket classes.
The URLConnection Class
URLConnection is another abstract class used to create and control a
connection to a platform and firewall specific location. It is a simplified
version of connection interface for the URL class.
The URLEncoder Class
URLEncoder takes a string of text and turns it into the x-www-form-
urlencoded format, which is a MIME format. This can then be used in
conjunction with the URL class.
The URLStreamHandler Class
URLStreamHandler is an abstract class. Its purpose is to create a format
for opening a stream connection to a specific URL. This class is concerned
with the protocol of the URL. If a programmer wants to create a new URL
stream protocol, it is necessary to implement the methods specified in this
abstract class.
To link a new page in WWW an instance of the class URL has to be created.
To create a new URL any one of the following constructors can be used:
 URL(String) creates an object from a full web address such as
http://www.smude.edu.in
 URL(URL, String) creates an URL object with a base address provided
by the specified URL and a relative path provided by the String.
 URL(String, String, int, String) creates a new URL object, given a
protocol, host name and a filename or path name.

Sikkim Manipal University – DDE B2119 Page No. 228


Object Oriented Programming – Java Unit 14
 URL(String, String, String) is the same as the previous constructor
without the port number.
The sun.tools.debug Library
This library provides the necessary framework for debugging Java programs.
Whenever Java debugger is used, this library automatically gets accessed.
Own debugger can also be created.
The DebuggerCallback Interface
The DebuggerCallback interface is a template for implementing
communications between an application and a debugger. The basic syntax
for the interface is as follows:
public interface DebuggerCallback
{

}
All communications between client applications and a debugger are
asynchronous.
One of the key methods in this interface is breakpointEvent(), which is
used to report when a breakpoint is reached. The method accepts a
RemoteThread object – the thread that reached the breakpoint-as a
parameter.
Other key methods in this class include exceptionEvent(), printTo
Console(), quitEvent(), and threadDeathEvent(). The exceptionEvent()
method is used to report that an exception has occurred. It accepts a
RemoteThread object and a String object as parameters.
The printToConsole() method is used whenever the DebuggerCallback
interface prints messages to the console – which, as in the Java debugger.
The printToConsole() method accepts a String object as a parameter.
The quitEvent() method is used to tell the debugger that the client has
exited. Generally, clients exit either by returning from the main thread or by
calling System.exit().
The threadDeathEvent() is used to report that a thread has died. This
method accepts a RemoteThread object, which is the thread that died, as a
parameter.

Sikkim Manipal University – DDE B2119 Page No. 229


Object Oriented Programming – Java Unit 14
Methods that deal with RemoteObjects
A complete listing of the types of remote objects handled by the debugger is
as follows: array, boolean, byte, character, class, double, field, float, integer,
long, short, string, thread, thread group, and variable value.
Each of these object types is handled in a separate class. These classes
include:
RemoteArray
RemoteBoolean
RemoteByte
RemoteChar
RemoteClass
RemoteDouble
RemoteField
RemoteFloat
RemoteInt
RemoteLong
RemoteShort
RemoteString
RemoteThread
RemoteThreadGroup
RemoteValue
Table 14.1 lists the Methods in the RemoteObject class
Table 14.1: Methods in the RemoteObject class
Method Purpose
description() Returns a description of the object
finalize() Performs this code when garbage collection is run
getClass() Returns the object’s class
getField() Returns an instance variable
getFields() Returns the non-static fields of an object
getFieldValue() Returns the value of an object’s instance variable
getId() Returns the ID of an object
setField() Sets an instance variable, specified by slot or name
toString() Returns the object as a string
typeName() Returns the object type

Sikkim Manipal University – DDE B2119 Page No. 230


Object Oriented Programming – Java Unit 14
The RemoteStackFrame class
Using the RemoteStackFrame class, the debugger can examine a
suspended thread’s stackframe. The stackframe provides a frame of
reference for everything related to a thread’s stack. It works as a snapshot
of a thread’s internals, including the thread’s program counter, local
variables, referenced methods, referenced classes, and the current line
number of execution. The methods used to retrieve this information follow a
very basic syntax. For example, when the getLineNumber() method is
called, the method returns the current line number of execution. This and
other methods in this class are shown in the table 14.2:
Table 14.2: Methods in the RemoteStackFrame class
Method Purpose
getLineNumber() Returns the current line number of execution
getLocalVariable() Returns a named stack variable in the current stackframe
getLocalVariables() Returns an array of all valid local variables and method
arguments for the stackframe
getMethodName() Returns the method name that the stackframe references
getpc() Returns the program counter that the stackframe
references
getRemoteClass() Returns the class that the stackframe references

The RemoteStackVariable class


The RemoteStackVariable class is similar to the RemoteStackFrame class.
Using this class, the caller can obtain information about a specific stack
variable.
The class has three methods: getName(), getValue(), and inScope(). The
getName() method is used to get the name of a variable on the stack. The
getValue() method is used to get the value of a variable on the stack. The
inScope() method is used to tell whether the variable is accessible.
The RemoteDebugger class
Client applications use this class as an interface to the Java debugging
classes because these classes can instantiate a connection with the Java
interpreter session being debugged. To create a client interface,
constructors can be used. The RemoteDebugger class provides an
overloaded constructor interface that allows for the two different ways the
debugger can be instantiated.

Sikkim Manipal University – DDE B2119 Page No. 231


Object Oriented Programming – Java Unit 14
The first constructor is used to attach the debugger to a current interpreter
session. The interpreter passes back a password, which is then passed to
the debugger when it is invoked. In addition to accepting the password as a
parameter, the constructor accepts a String object that identifies
 The host running the current interpreter session
 The client using the DebuggerCallback interface to receive messages
from the debugger
 A verbose flag to turn on internal debugger message text
Here is the syntax for the first constructor:
public RemoteDebugger(String host, String password,
DebuggerCallback client, boolean verbose) throws Exception
The second constructor is used to create a remote debugger that will start a
client interpreter session. When user starts the debugger directly, it in turn
starts the Java interpreter with any parameters passed on the command line.
For this reason, the second constructor accepts arguments that are to be
passed on to the interpreter. Other parameters allow the client using the
DebuggerCallback interface to receive messages from the debugger, and
others set a verbose flag to turn on or off internal debugger message text.
The syntax for the second constructor follows:
public RemoteDebugger(String javaArgs, DebuggerCallback client,
boolean verbose) throws Exception
Methods in the RemoteDebugger class enable the user to use debugging
techniques in the programs. These methods are given in the table 14.3.
Table 14.3: Methods in the RemoteDebugger class
Method Purpose
addSystemThread() Adds a system thread
close() Closes the connection to the remote debugger
findClass() Finds a specified class
freeMemory() Gets a report of the free memory available to the
Java interpreter being debugged
gc() Frees all objects referenced by the debugger
get() Gets an object from the remote object cache
getExceptionCatchList() Gets the list of the exceptions on which the
debugger will stop
getSourcePath() Gets the source file path the client is currently using
itrace() Turns instruction tracing on or off

Sikkim Manipal University – DDE B2119 Page No. 232


Object Oriented Programming – Java Unit 14
listBreakpoints() Gets a list of the current breakpoints
listClasses() Gets a list of the currently known classes
listThreadGroups() Gets a list of the currently known thread groups
run() Loads and runs a runnable Java class
setSourcePath() Sets the search path for source files
totalMemory() Obtains a report of the total memory used by the
Java interpreter being debugged
trace(boolean) Turns method call tracing on or off

The StackFrame class


The StackFrame class provides a wrapper for the stackframe of a
suspended thread. This class has a very basic constructor and a single
method, called toString(), that returns an object as a string.
Self Assessment Questions
4. _____ is an abstract class that is used to create and control a
connection to a platform- and firewall-specific location.
5. _____ is an abstract class that is used to create a format for opening a
stream connection to a specific URL.
6. The _____ method is used to report that a thread has died.

14.4 Summary
 The java.net package contains classes that provide an easy-to-use
interface to basic network programming.
 The sun.tools.debug class library adds built-in debugging support to the
Java programming language.
 When sockets are used in an applet, still it is subject to the security
restrictions that prevents from connecting to any other system other than
the same one the applet came from.
 There are many new subclasses of the SocketException class, to
represent network errors on a finer level of granularity than in Java 1.02.

14.5 Terminal Questions


1. Explain the various classes available in java.net package.
2. Explain the various URL objects.
3. What are the various Remote Object classes?
4. What are the methods available in RemoteStackFrame?
5. What are the methods available in RemoteDebugger class?

Sikkim Manipal University – DDE B2119 Page No. 233


Object Oriented Programming – Java Unit 14
14.6 Answers
Self Assessment Questions
1. java.net
2. getInputStream()
3. ContentHandler()
4. URLConnection
5. URLStreamHandler
6. threadDeathEvent()

Terminal Questions
1. ContentHandler, DatagramPacket, DatagramSocket, InetAddress.
(Refer Section 14.2.)
2. URL, URLConnection, URLEncoder, URLStreamHandler.
(Refer Section 14.3.)
3. RemoteArray, RemoteBoolean, RemoteByte, RemoteChar etc.
(Refer Section 14.3)
4. getLineNumber(), getLocalVariable(), getMethodName() etc.
(Refer Section 14.3)
5. addSystemThread(), close(), findClass(), freeMemory() etc.
(Refer Section 14.3)

––––––––––––––––––––

Sikkim Manipal University – DDE B2119 Page No. 234

You might also like