You are on page 1of 351

Unit 1

Introduction to
Network Programming
What is Network programming?

Network programming is the act of using computer


code to write programs or processes that can
communicate with other programs or processes
across a network. Programmers use various
programming languages, code libraries, and protocols
to do the work.
1.1 Network Programming Features and Scope

• Features
• allows applications to connect and communicate via network.

• Read files from the internet and post files to the remote applications
via internet.

• concerns about the reliability of the messages

• Deals with the implementation of protocols at their corresponding


layers.
• Scope
• NP has become a core part of every developer since most of the
programs are network aware.

• Classic applications: email, web browsers and remote login

• Current and Future Scope:


• Text editors- open and save files directly from FTP servers

• IDEs- work with repositories like GitHub and Sourceforge

• Online Games- PUBG, Halo, Fight and Frag at real time

• Supermarkets, Music Players, Scheduling Applications and


many more
• Promising Field: Internet of Things (IoT)
1.2 Network programming Language, Tools and Platforms
• Lot of programming languages exist can be used for networking field.

• But most are not networking specific languages.

• Some languages that can be used for network programming are:


• Python – general purpose language, great for beginners, extremely readable

• Go – aka Golang, developed by Google for new engineers at


Google, strongly typed, powerful as a network programming
language.

• Perl – used to be darling of system and network engineers, high amount


of modules are available, module reusability is advantage, not popular
now

• Bash – language for Unix based system, very short and simple programs

• Java – OOP with rich sets of API, has a core library that includes classes
for communicating with the internet hosts, Java fills over 90% of most
• The course of network programming is based on Java.

• Several IDEs and tools are available:


• Any IDE that supports Java can be used (IntelliJ IDEA)

• Network programming must be able to support various platforms such a s


Windows, Linux, Mac OS, Mobile OS (Android, iOS) etc.

• Installing a suitable IDE


1.3 Client and Server
Applications
• A client/server application consists of a client program that consumes
services provided by a server program.

• The client requests services from the server by calling functions in


the server application.

• In distributed computing environment, where the client program and


server program execute on different machines and platform, the
client and server communicate through a communications layer that
is often called middleware.

• Although applications running on different machines obviously


require those machines to be physically connected in some way.
• We need to distinguish between the client/server application
architecture.

• The client application and server applications might run on a:


• Network client or a Network server (vice-versa)

• Same machine (either on client or on a server)

• So how do you classify which part of application is client and which


one is server?
• Client is always the requestor of services

• Server receives requests and serves the requests.


1.4 Client server model and Software Design

• Most modern network programming is based on a client/server model.

• A client server application typically stores large quantities of data on an


expensive, high-powered server and the program logic and UI is
handled by client software running on relatively cheap personal
computers.

• A more reliable distinction is that a client initiates a conversation while


a server awaits for clients to start conversations with it.

• Example: FTP

• However not all applications fit easily into a client/server model


• In Online games players will send data back and forth roughly equally
• Such connections are called peer to peer.

• Java does not have explicit peer-to-peer communication in its core


networking API.

• To achieve this peers can act as a client sometimes and as a server other
times.
Unit 2
Internet
Addresses
1. Internet Addresses:
Introduction
• Devices connected to internet are called nodes.

• Each node or host is identified by unique number called Internet address or


IP address.

• Most current IP addresses are 4 byte long IPv4 addresses

• Also 16 byte long IPv6 addresses do exist

• IP addresses are great for computers but problem for humans.

• To avoid the need to carry and memorize IP addresses Internet designers


created Domain Name System (DNS).

• DNS maps human readable hostnames to corresponding IP addresses.


• Often Internet addresses have ambiguous meaning. Can be hostname or URL or IP
address.

• In our scope, address is always a numeric IP address and name is hostname.

• Some names map to multiple IP addresses.


• Load Management

• Faster access

• DNS automatically chooses the best machine to respond to the request.

• How to find the address of a machine given a hostname?


• Solution: InetAddress Class
2. The InetAddress
Class
• java.net.InetAddress class in Java represents an IP addresses both IPv4 and
IPv6.

• Converts numeric addresses to host names and hostnames to numeric addresses.

• Utilized by most of the other networking classes such as:


• Socket

• ServerSocket

• URL

• DatagramSocket
3. Creating InetAddresses
• No public constructors in the InetAddress class.

• Has static factory methods that connect to DNS to resolve Hostnames.

• All address must be checked with DNS

• The general syntax is:

InetAddress obj = InetAddress.someMethod();

where, obj = some object name & someMethod = a method of the InetAddress
class

Example: InetAddress address = InetAddress.getByName(“www.google.com”)


The getByName() factory
method
import java.net.*;
public class GBN
{
public static void
main (String[]
args) {
try {
InetAddress address = InetAddress.getByName("www.google.com");
System.out.println(address);
} catch (UnknownHostException ex) {
System.out.println("Could not find the given hostname");
}
}
}
The getHostName() factory
method
import java.net.*;
public class GBN {
public static void main (String[] args) {
try {
InetAddress address = InetAddress.getByName(“205.27.59.102");
System.out.println(address.getHostName());
} catch (UnknownHostException ex)
{ System.out.println("Could not find the given
IP");
}
}
}
The getAllByName() factory
•method
Some names can have multiple addresses.
• In general any one of them serves the request, but if all the address are required use the
getAllByName() method.
• It returns an array.
try {
InetAddress[] addresses = InetAddress.getAllByName("www.oreilly.com");
for (InetAddress address : addresses) {
System.out.println(address);
}
} catch (UnknownHostException ex)
{ System.out.println("Could not find the
hostname");
}
The getLocalHost() method
• It returns the address for the host on which your code is running.

• InetAddress me = InetAddress.getLocalHost();

Returns the loopback address if the connection to the DNS fails.


The getByAddress() method
• If you know a numeric address, you can create an InetAddres object from that
address without talking to DNS using the getByAddress() method.

• Can create addresses for hosts that donot exist or cannot be resolved.
• public static InetAddress getByAddress(byte[] addr) throws UnknownHostException

public static InetAddress getByAddress(String hostname, byte[] addr) throws UnknownHostException

• The first method creates an InetAddress object with an IP address and no hostname

• The second one creates an |InetAddress object with an IP and a hostname.


• byte[] address = {107, 23, (byte) 216, (byte) 196};
InetAddress lessWrong = InetAddress.getByAddress(address);

InetAddress lessWrongWithname =
InetAddress.getByAddress( "lesswrong.com", address);
How can you create InetAddress objects?

Method Description
static InetAddress getLocalHost() throws This method returns the instance of InetAddress
UnknownHostException containing the local hostname and address.

public static InetAddress getByName( String This method returns the instance of InetAddress
host ) throws UnknownHostException containing LocalHost IP and name.

static InetAddress[] getAllByName( String This method returns the array of the instance of
hostName ) throws UnknownHostException InetAddress class which contains IP addresses.

static InetAddress getByAddress( byte This method returns an InetAddress object created
IPAddress[] ) throws UnknownHostException from the raw IP address.

static InetAddress getByAddress( String


hostName, byte IPAddress[] ) throws This method creates and returns an InetAddress
UnknownHostException based on the provided hostname and IP address.
Class Lab 1:
• Initialize an object of InetAddress class using:
• Hostname
• IP address

• Get and display all the IP address of the name “www.orielly.com”

• Initialize an InetAddress object using the Local address of your machine.

• Use the IP address 13.227.138.129 to define an InetAddress object and display


the
contents stored by the object.

• Define an InetAddress object using the hostname “www.xyz.com” and IP


address 202.25.1.152
4. Getter methods
• The InetAddress class contains four getter methods that return the hostname as
a string and the IP address as both a string and a byte array.
• public String getHostName()
public String getCanonicalHostName()
public byte[] getAddress()
public String getHostAddress()

• The getHostname() method returns a string that contains the name of the host
with the IP address represented by this InetAddress object.
• If the machine does not have hostname or any other problems are found,simply the dotted
quad form of the IP address is returned.
• The getCanonicalHostName() method is similar to getHostName() but the
getCanonicalHostName calls the DNS in any case and may replace the
existing cached hostname.

• The getHostAddress() method returns a string containing the dotted format of


the IP address.
• InetAddress machine = InetAddress.getByName(“www.facebook.com”);
String localhost = machine.getHostName();
String localhost = machine.getCanonicalHostName();
String dottedQuad = machine.getHostAddress();
• If you want to know the IP address of a machine, then you use the getAddress() method.

• It returns an IP address as an array of bytes in network byte order.


• InetAddress me = InetAddress.getLocalHost();
byte[] address = me.getAddress();

• Useful to determine either the address is IPv4 or IPv6.

• import java.net.*;
public class AddressTests {
public static int getVersion(InetAddress ia) {
byte[] address = ia.getAddress();
if (address.length == 4) return 4;
else if (address.length == 16) return 6;
else return -1;
}
}
Class Lab 2
• Illustrate the use of all the Getter methods using any suitable example. You can
choose any suitable InetAddress objects.
5. Address
Types
• Some IP addresses and some patterns of address have special meanings.
• 127.0.0.1 is loopback address
• 224.0.0.0 to 239.255.255.255 are multicast addresses
• Java includes 10 methods for testing whether an InetAddress object meets any of these criteria.

• public boolean isAnyLocalAddress()


public boolean isLoopbackAddress()
public boolean isLinkLocalAddress()
public boolean isSiteLocalAddress()
public boolean
isMulticastAddress() public
boolean isMCGlobal()
public boolean isMCNodeLocal()
public boolean isMCLinkLocal()
public boolean
isMCSiteLocal() public
• isAnyLocalAddress() : returns true if the address is a wildcard address. A
wildcard address matches any address of the local system.
• Important when system has multiple network interfaces (WiFi, Ethernet)

• isLoopbackAddress(): returns true if the address is the loopback address,


false otherwise.

• isLinkLocalAddress(): method returns true if the address is an IPV6 link-


local address, false otherwise.
• This address helps IPv6 networks self configure. (like DHCP on IPv4 address)

• isSiteLocalAddress(): returns true if the address is an IPv6 site-local address,


false otherwise.
• Similar to link-local addresses except that they may be forwarded by routers within a site.
• isMulticastAddress(): returns true if the address is a multicast address, false
otherwise.
• Broadcasts packets to all the computers rather than one particular PC.
• In IPv6, Multicast address begin with byte FF.

• isMCGlobal(): returns true if the address is a global multicast address.


• Global multicast addres may have subscribers around the world.

• isMCOrgLocal(): returns true if the address is an organization wide multicast


address.
• An organization wide multicast address may have subscribers within all the sites of
company
or organization but not outside the organization.

• isMCSiteLocal(): returns true if the address is a site wide multicast address.


• Packets addressed to a site wide address will only be transmitted within their local site.
• isMCLinkLocal(): returns true if the address is a subnet wide multicast address,
false otherwise.
• Packets addressed to a link-local address will only be transmitted within their own subnet.

• isMCNodeLocal(): returns true if the address is an interface-local multicast


address.
• Packets addressed to an interface local address are not sent beyond the network interface
from
which they originate.
Class Lab 3
• Write a program to find the type of address for the given input name or address
provided by the user.
6. Testing Reachability
• The InetAddress class has two isReachable() methods that test whether a particular node
is reachable from the current host.
• Reachable in the sense that a network connection can be established or not.

• Connections can be blocked for many reasons including fiurewalls, proxy srevers,
misbehaving routes, broken cables or if the remote host is turned off.
• public boolean isReachable(int timeout) throws IOException
public boolean isReachable(NetworkInterface interface, int ttl, int timeout) throws IOException

• They utilize the ICMP echo requests to determine reachability.

• In first method of if the host responds within the timeout, true is returned else false.

• The second allows you to specify le local network interface and Time-to-Live.
7. Object Methods
• Like every other class InetAddress inherits from java.lang.Object.

• Hence it has access to all the methods of that class and overrides three methods.
• public boolean equals(Object o)
public int hashCode()
public String toString()

• An object is equal to an InetAddress object only if it is itself an instance of


the InetAddress class and has same IP address.

• The hashCode() method is similar to equals() method. The integer that hashCode()
returns is calculated solely from the IP address. If addresses are same then they
have same hash code even if hostnames are different.
• The toString() method returns short text representation of the
object.

• Class Lab 4

• Write a program to check either the two names “www


.ibiblio.org” and “helios.ibiblio.org” are same or not. Also
generate the hash code for the both and print the hash
code as a string.
8. Inet4Address and
•Inet6Address
Java uses two classes, Inet4Address and Inet6Address, in order to distinguish IPv4 addresses
from IPv6 addresses.
• public final class Inet4Address extends InetAddress
public final class Inet6Address extends InetAddress

• Most of the time it does not concern


with whether an address is IPv4 ar IPv6
address.

• Even if we need to know then, it is quicker to check the size of byte array using getAddress()
method.

• Inet6Address adds one new method, public boolean isIPv4CompatibleAddress()

• It returns true if and only if the address is essentially an IPv4 address stuffed into an IPv6
container. That is, the address has the form 0:0:0:0:0:0:0:xxxx.
9. The Network Interface
Class:
• The NetworkInterface class represents a local IP address.

• This can either be a physical interface such as an Ethernet card or virtual


interface bound to same physical hardware.

• The NetworkInterface class provides methods to enumerate all the local


addresses and create InetAddress objects from them.

• Then these InetAddress objects can then be used to create sockets, server
sockets and so on.
9. The Network Interface Class: Factory
Methods
• Since NetworkInterface objects represent physical hardware and virtual
addresses, they cannot be constructed arbitrarily.

• Like the InetAddress class, there are static factory methods that return the
NetworkInterface object associated with a particular network interface.

•You an ask for a NetworkInterface by IP address, by name or by enumeration.

The factory methods are:


• public static NetworkInterface getByName(String name)

• public static NetworkInterface getByInetAddress(InetAddress address)

• public static Enumeration getNetworkInterfaces()

• All these methods throw SocketException


public static NetworkInterface getByName(String name) throws SocketException

• The getByName() method returns a NetworkInterface object representing


the network interface with the particular name.

• If there is no interface with than name, it returns null.

• If any problem persists while locating the relevant network interface,


a SocketException is thrown.

Example:
NetworkInterface ni = NetworkInterface.getByName("eth0");
public static NetworkInterface getByInetAddress(InetAddress address)
throws SocketException

• The getByInetAddres() method returns a NetworkInterface object


representing the network interface bound to the specified IP address.

• If no network interface is bound to that IP address on the local host, it


returns null.

• throws SocketException.
• InetAddress local = InetAddress.getByName("127.0.0.1");
NetworkInterface ni = NetworkInterface.getByInetAddress(local);
public static Enumeration getNetworkInterfaces() throws
SocketException
• The getNetworkInterfaces() method returns a java.util.Enumeration listing all
the
network interfaces on the local host.

• The Enumeration interface defines the methods by which you can enumerate
(obtain one at a time) the elements in a collection of objects.

Enumeration interfaces =
NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements())
{ System.out.println(interfaces.nextElement(
));
10. The NetworkInterface Class: Getter
•Methods
Once you have NetworkInterface object, you can inquire about its IP address
and
name.

• Three getter methods exist for NetworkInterface Class:

• public Enumeration getInetAddresses()

• public String getName()


• public String getDisplayName()
• public Enumeration getInetAddresses(): Single network interface may be bound
to more than one IP address.

• This situation isn’t common but does happen.

• It returns enumeration containing an InetAddres object for each IP address the


interface is bound to.

NetworkInterface eth0 = NetworkInterrface.getByName("eth0");


Enumeration addresses = eth0.getInetAddresses();
while (addresses.hasMoreElements())
{ System.out.println(addresses.nextElement(
));
}
• public String getName(): The getName() method returns the name of the
particular NetworkInterface object, such as eth0, wlan0 etc.

• public String getDisplayName(): The getDisplayName() method returns a more


human-friendly name for particular NetworkInterface.
• Eth0 => Ethernet Card 0

• Wlan0 => Wireless Local Area Network 0


Some Useful programs:
• A SpamCheck:
number of services monitor spammers, and inform clients whether a host attempting
to connect to them is a known spammer or not by using a real-time blackhole lists.

• To find out if a certain IP address is a known spammer, reverse the bytes of the
address, add the domain of the blackhole service, and look it up. If the address is
found, it’s a spammer. If it isn’t, it’s not

• For instance, if you want to ask sbl.spamhaus.org if 207.87.34.17 is a spammer, you


would look up the hostname 17.34.87.207.sbl.spam‐haus.org.

• If the DNS query succeeds (and, more specifically, if it returns the address 127.0.0.2),
then the host is known to be a spammer. If the lookup fails—that is, it throws an
UnknownHostException.
Processing Web Server Logfiles
• Web server logs track the hosts that access a website.

• The log reports the IP addresses of the sites that connect to the server.

• Most web servers have an option to store hostnames instead of IP addresses, but this
can
hurt performance because the server needs to make a DNS request for each hit.

• It is much more efficient to log the IP addresses and convert them to hostnames at
a later time, when the server isn’t busy.

• Such program is called Weblog that reads a web server logfile and prints each line
with IP address converted to hostnames.
• Go through the Lab Manual for Programs of SpamCheck and WebServer Log
files.
Unit 3
URLs and URIs
Instructor:
Manoj
Pokharel

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 1
Background
• Unit 2: learned how to address hosts on the internet via hostname and
IP address.

• In unit 3, we will increase the scope by addressing the resources that


may reside on any host.

• URL is most common type of Uniform Resource Identifier (URI)

• The URL class is the simplest way for a Java program to locate and
retrieve data from the network.
Class labs, programs and Related Java Code can be downloaded from:
HERE!!! Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 2
3.1. URIs: URLs and Relative URLs

• Uniform Resource Identifier (URI) is a string of characters in a


particular syntax identifies a resource.

• Resource may be file on a server, an email address, a book, person’s


name, or any thing residing on hosts.

• URI Syntax:
• scheme:scheme-specific-part

• The syntax of the scheme-specific part depends on the scheme being


used. Current schemes are.
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 3
• The first component of URI is scheme that contain a sequence of characters
that can be any combination of letter, digit, plus sign, or hyphen (_), which
is followed by a colon (:).

• http: an WWW server using the Hypertext Transfer Protocol

• data: Base64 encoded data included directly in link.

• File: A file on local disk

• ftp: an FTP server

• mailto: An email address

• magnet: a resource available via peer-to-peer networks such as BitTorrent


Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 4
• No specific syntax that applies to scheme-specific parts of all URIs.

• Common hierarchical form is:


• //authority/path?query

• Authority is optional and preceded by two slashes (//).


• Contains userinfo, host or port

• Path: contains sequence of path separated by slash (/).

• Query: optional, preceded by question mark.

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 5
Some examples of URI
• http://www.ietf.org/rfc/rfc3986.txt
• Scheme: http
• Authority: www.ietf.org
• Path: frc/rfc3986.txt
• Query: Not specified
• Implies that the server at www.ietf.org is responsible for mapping the path
frc/rfc3986.txt to a resource
• http://www.powells.com/cgibin/biblio?inkey=62-1565928709-0
• ftp://mp3:mp3@ci43198-a.ashvil1.nc.home.com:33/VanHalen-Jump.mp3
• mailto:hey.Doe@example.com
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 6
URLs
• URL is a URI  it identifies a resource and also helps client retrieve that
resource.

• URL Syntax: protocol://userInfo@host:port/path?query#fragment


• protocol: protocol used to access server. (ftp, http, https, magnet, telnet)

• userinfo: optional login information for server

• host: name of server that provides the resource you want

• port (optional):

• path: particular resource in the specified server

• query: additional arguments for the server

• fragment: specifies particular part of resource.


• Example: http://www.cafeaulait.org/javafaq.html#xtocid1902914
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 7
Relative URLs
• A URL tells a web browser about:
• The resource
• Protocol used to retrieve the resource
• Host where the resource resides
• Path to the resource
• And so on

• Most of this information is likely to be same for other URLs that are
referenced in the document.

• Therefore , rather than requiring each URL to be specified entirely,


amay
URLinherit the protocol , hostname and path of its parent document.
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 8
• URLs that are not complete but inherit pieces from their parent are called relative
URLs.

• A completely specified URL is called an absolute URL.

• In a relative URL any pieces that are missing are assumed to be same as the
corresponding pieces of absolute URL.
• suppose you are browsing: http://www.ibiblio.org/javafaq/java‐tutorial.html and click on
the hyperlink <a href="javafaq.html">

• The browser cuts javatutorial.html off the end of http://www.ibiblio.org/javafaq/java‐


tutorial.html to get http://www.ibiblio.org/javafaq/. Then it attaches javafaq.html onto the end
of http://www.ibiblio.org/javafaq/ to get http://www.ibiblio.org/javafaq/java‐ faq.html.
9
Finally, it loads that dCoomcpuelimd bye: Mnatn.oj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming,
3.2. The URL Class: Creating New URLs
• URL class has four constructors:
public URL(String url) throws MalformedURLException
public URL(String protocol, String hostname, String file) throws MalformedURLException
public URL(String protocol, String host, int port, String file) throws MalformedURLException
public URL(URL base, String relative) throws MalformedURLException

• Which constructor to use?


• Depends on the information you have and the form its in.

• All constructors throw MalformedURLException if you try to create a URL


for an unsupported protocol or URL is syntactically incorrect.
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 10
Constructing a URL from a string
• Simplest URL constructor that takes an absolute URL in string from
as its argument.
• public URL(String url) throws MalformedURLException

• The following code constructs a URL object from a String.

try {
URL u = new URL("http://www.audubon.org/");
} catch (MalformedURLException ex) {
System.err.println(ex);}

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 11
Constructing a URL from its component parts
• You can build a URL by specifying the protocol, the hostname and the file.

• public URL(String protocol, String hostname, String file) throws MalformedURLException

• Code:

try {
URL u = new URL("http", "www.eff.org", "/blueribbon.html#intro");
} catch (MalformedURLException ex) {

throw new RuntimeException("shouldn't happen; all VMs recognize http"); }

• This constructor uses default port, file argument should begin with a slash and
include a path, a filename.
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 12
• In rare cases, the default port may not be used.. The next constructor lets you
specify the port explicitly as an int data type. The other arguments are same.

try {
URL u = new URL("http", "fourier.dur.ac.uk", 8000, "/~dma3mjh/jsci/");
} catch (MalformedURLException ex) {
throw new RuntimeException("shouldn't happen; all VMs recognize http");
}

• For example, this code fragment creates a URL object that points to
http://fourier.dur.ac.uk:8000/ ~dma3mjh/jsci/, specifying port 8000 explicitly
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 13
Constructing relative URLs
• The constructor builds an absolute URL from relative URL and a base
URL.
• public URL(URL base, String relative) throws MalformedURLException

• For instance, you may be parsing an HTML document at


http://www.ibiblio.org/javafaq/ index.html and encounter a link to a file called
mailinglists.html with no further qualifying information. In this case, you use the
URL to the document that contains the link to provide the missing information.
The constructor computes the new URL as http://
www.ibiblio.org/javafaq/mailinglists.html
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 14
• The code for above case is:

try {
URL u1 = new URL("http://www.ibiblio.org/javafaq/index.html");
URL u2 = new URL (u1, "mailinglists.html");
} catch (MalformedURLException ex) {
System.err.println(ex);}

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 15
3.2.2 Retrieving Data From a URL
• The URL class has several methods that retrieve data from a URL.
• public InputStream openStream() throws IOException
public URLConnection openConnection() throws IOException
public Object getContent() throws IOException
public Object getContent(Class[] classes) throws IOException

• Most basic and commonly used: openStream(), which returns


an InputStream from which you can read the data.

• And then you can ask the URL for its content with getContent(() which
may give you a more complete object such as String or an Image.
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 16
Public final InputStream openStream() throws IOException
• The openStream() method connects to the resource referenced by the
URL, and returns an InputStream from which data can be read.

• The data you get from InputStream is the raw content of URL
references.

• The code shows how to connect to a resource and read the data from
the resource using InputStream.

• This method assumes that the availbale information is in text.

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 17
InputStream in = null
try {
URL u = new URL("ht
tp://www.lolcats.co
m
");
in = u.openStream();
int c;
while ((c =
in.read()) != -1)
System.out.write(c);
} catch (IOException ex) {
System.err.println(ex);
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) { Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 18
Class Lab 1

• Write a program to download a web page using the URL of the web
page. Use the openStream and InputStreamReader’s read()
method.

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 19
public URLConnection openConnection() throws IOException
• OpenConnection() method opens a socket to the specified URL and
returns a URLConnection object.

• A URLConnection represents an open connection to a network resource.

• Use this method when you wish to communicate directly with the server

• The URL connection gives you access to everything sent by the


server including the document itself in raw form.

• It also allows you to write data to as well as the read from a URL. (Send
email, POST to server)
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 20
try {
URL u = new URL("https://news.ycombinator.com/");
try {
URLConnection uc = u.openConnection();
InputStream in = uc.getInputStream();
// read from the connection...
} catch (IOException ex) {
System.err.println(ex);
}
} catch (MalformedURLException ex) {
System.err.println(ex);
}
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 21
public final Object getContent() throws IOException
• 3rd way to download data referenced by a URL.

• The getContent() method retrieves the data referenced by the URL.

• And tries to make it into some type of object.

• If URL refers to some kind of text such as an ASCII or HTML file, the object returned is
some sort of InputStream.
• If URL refers to an image such as a GIF or a JPEG, it returns java.awt.ImageProducer

• General syntax:

URL u = new URL("http://mesola.obspm.fr/");


Object o = u.getContent();
// cast the Object to the appropriate type

// work with the OCobmjpeeli cdtb.y.: .Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 22
Class Lab 2: A program to download an object specified by
URL

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 23
Public final Object getContent(Class[] classes) throws IOException

• In some cases using getContent can provide different views of a single


resource.
• This overloaded version of the getContent() method lets you choose
which class you’d like the content to be returned as.

• The method attempts to return the URLs content in the first available
format.
• If you prefer an HTML file to be returned as a String, but your
second choice is a Reader and 3rd choice is an InputStream, write the
code as:
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 24
URL u = new URL("http://www.nwu.org");
Class<?>[] types = new Class[3];
types[0] = String.class;
types[1] = Reader.class;
types[2] = InputStream.class;
Object o = u.getContent(types);

• Now we test the type of returned object using instanceof.

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 25
if (o instanceof String) {
System.out.println(o);

} else if (o instanceof
Reader) {
int c;
Reader r = (Reader) o;
while ((c = r.read()) != -1) System.out.print((char) c);
r.close();
} else if (o instanceof InputStream) {
int c;
InputStream in = (InputStream) o;
while ((c = in.read()) != -1) System.out.write(c);
in.close();
} else {
System.out.println("Error: unexpected type " +
o.getClass()); Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 26
3.2.3 Splitting a URL into pieces
• URLs are composed of five pieces:
• The scheme, also known as the protocol
• The authority
• The path
• The fragment identifier, also known as the section or ref
• The query string

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 27
• For example, in the URL
http://www.ibiblio.org/javafaq/books/jnp/index.html?isbn=1565922069#toc, the
scheme is http, the authority is www.ibiblio.org, the path is /
javafaq/books/jnp/index.html, the fragment identifier is toc, and the query string is
isbn=1565922069. However, not all URLs have all these pieces. For instance, the
URL http://www.faqs.org/rfcs/rfc3986.html has a scheme, an authority, and a path,
but no fragment identifier or query string.
• The authority may further be divided into the user info, the host, and the port.
For example, in the URL http://admin@www.blackstar.com:8080/, the authority
is admin@www.blackstar.com:8080. This has the user info admin, the host
www.black‐star.com, and the port 8080
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 28
• Read-only access to these parts of a URL is provided by nine public
methods:
• getFile(), getHost(), getPort(), getProtocol(), getRef(),
getQuery(), getPath(), getUserInfo(), and getAuthority()

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 29
• public String getProtocol() : returns a string containing the scheme of the
protocol. (http, file, https). Code fragment looks like this:

URL u = new URL("https://xkcd.com/727/");


System.out.println(u.getProtocol());

• Public String getHost(): returns a string


containing the hostname of the URL.

URL u = new URL("https://xkcd.com/727/");


System.out.println(u.getHost());

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 30
• public int getPort(): returns the port number specified in the URL as an int.
• If no port is specified it returns -1 (denotes use of default port)

• For example, if the URL is http://www.userfriendly.org/, getPort() returns -1; if


the URL is http://www.userfriendly.org:80/, getPort() returns 80.

• public int getDefaultPort(): returns the default port used for this URL’s
protocol when none is specified in the URL.
• If no default port is defined for the protocol, then getDefaultPort() returns -
1. For example, if the URL is http://www.userfriendly.org/, getDefaultPort()
returns 80; if the URL is ftp://ftp.userfriendly.org:8000/, getDefaultPort()
returns ????? Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 31
• public String getFile(): returns String that contains the path portion of the
URL. Everything from the first slash (/) after the hostname until the character
preceding the # sign is considered to be part of file.

• public String getPath(): similar to getFile(), but it doesnot include query


string in the String it returns.

• public String getRef(): returns the fragment identifier part of the URL. If the
URL doesn’t have a fragment identifier, the method returns null.

URL u = new URL("http://www.ibiblio.org/javafaq/javafaq.html#xtocid1902914");


System.out.println("The fragment ID of " + u + " is " + u.getRef());
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 32
• public String getQuery(): returns the query string of the URL. If no query
string is present, null is returned.

• public String getUserInfo(): Some URLs include usernames and occasionally


even password information.
• this information comes after the scheme and before the host; an @ symbol delimits it.

• For instance, in the URL http://elharo@java.oreilly.com/, the user info is elharo.

• public String getAuthority(): returns the authority that resolves the resource.
• in the URL http://conferences.oreilly.com/java/speakers/, the authority is simply the
hostname conferences.oreilly.com.

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 33
Class Lab 3: Write Java code to accept a URL as user input and split the
URL into pieces.

Trace the output using any three random URLs.

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 34
3.2.4 Equality & Comparison and Conversion
• The URL class contains the usual equals() and hashCode() methods.
• Two URLs are considered equal if and only if: both URLs point to the same
resource on the same host, port and path within the same fragment and query
string.
• equals() works with DNS, hence, http://www.google.com and
http://google.com are same. But http://www.google.com:80 is not equal to
http://www.google.com
• Also, http://chpl.com/index.html and http://chpl.com are not same same since it
does not compare the resources identified by two URLs.
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 35
Class Lab: Write Java code to check either http://www.ibiblio.org and
http://ibiblio.org are same or not?

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 36
Conversion
• URL has three methods that convert an instance to another form: toString(),
toExternalForm(), and toURI()

• String toString() and String toExternalForm() are similar.


• toString() produces URL in string format. However it is odd to use toString()
method outside Print statements.
• toExternalForm converts a URL object to string. Used outside the print statements .

• public URI toURI() throws URISyntaxException: converts a URL object to


an equivalent URI object.

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 37
URL vs URI classes

• equals() method on a URL is potentially a blocking I/O operation.


• Avoid stroing URLs in data structures that depend on equals() such as
HashMap. (redundancy)
• Prefer to use URI for this and convert from URIs to URLs when
necessary.

• Use URL class when you want to download content from a


server.

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 38
3.3 The URI Class:
• A Uniform Resource Identifier consists of both URLs and URIs.

• A URI is a string of characters used ti identify a resource on the Internet.

• URL is a subset of URI that specifies where an identified resource is available


and the mechanism for retrieving it.

• A URN only specifies where an identified resource is.

https://stackoverflow.com/questions/4913343/what-is-the-difference-between-uri-
url-and-urn -

• Use the URL class when you want to download the content at a URL and the
URI class when you want to use the URL for identification rather than retrieval
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 39
3.3.1 Constructing a URI
• URIs are built from strings. You can either pass the entire URI to the constructor
as a single string or the individual pieces.
public URI(String uri) throws URISyntaxException
public URI(String scheme, String schemeSpecificPart, String fragment) throws
URISyntaxException
public URI(String scheme, String host, String path, String fragment) throws
URISyntaxException
public URI(String scheme, String authority, String path, String query,String fragment)
throws URISyntaxException
public URI(String scheme, String userInfo, String host, int port,String path, String query,
String fragment) throws URISyntaxException
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 40
• As long as the syntax is correct for URI, an URI object can be created.

• That makes it usable for new and experimental URI schemes.


• URI voice = new URI("tel:+1-800-9988-9938");

URI web = new URI("http://www.xml.com/pub/a/2003/09/17/stax.html#id=_hbc");


URI book = new URI("urn:isbn:1-565-92870-9");
• URI absolute = new URI("http", "//www.ibiblio.org" , null);
URI relative = new URI(null, "/javafaq/index.shtml", "today");

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 41
3.3.2 The Parts of the URI
• A URI reference has up to three parts: a scheme, a scheme-specific part, and a
fragment identifier. The general format is: scheme:scheme-specific-part:fragment

• The methods used to extract and manipulate parts of URI are:

• public boolean isAbsolute(): A URI that has a scheme is an absolute URI. A


URI without a scheme is relative.

• public boolean isOpaque(): if the URI is not hierarchical it is opaque. Hierarchical in


the sense that the scheme-specific part has a particular hierarchical format divided into
an authority, a path, and a query string. The authority is further divided into user info,
host, and port.
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 42
• If the URI is opaque, all you get is the scheme, scheme-specific part, and
fragment identifier using following methods.
• public String getScheme()
public String getSchemeSpecificPart()
public String getFragment()

• If the URI is hierarchial, following getter methods can be used to retrieve


additional information: getAuthority(), getFragment(), getHost(), getPath(),
getPort(), getQuery(), getUserInfo()
• All these methods return a String and must be preceded by
public URI parseServerAuthority() throws URISyntaxException statement.
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Net. Programming, 2014 43
Class Lab 4: Write Java code to extract parts of URI.

• Check either URI is opaque or not?


• If opaque, extract scheme, scheme specific part and
fragment
• Else, extract authority, fragment, host, path, port, query .

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 44
3.3.3 Resolving Relative URIs
• URI class has three methods for converting back and forth between
relative and absolute URIs.
• public URI resolve(URI uri)
public URI resolve(String uri)
public URI relativize(URI uri)

• Example: what will be


the result in resolved
attribute of 3rd
statement?
• URI absolute = new URI("http://www.example.com/");
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 45
• URI top = new URI("javafaq/books/");
URI resolved = top.resolve("jnp3/examples/07/index.html");

• similarly, above statements resolved attribute holds


javafaq/books/jnp3/examples/07/index.html with no scheme or authority.

• Also possible to reverse this procedure, go from an absolute URI to a


relative one.
• URI absolute = new URI("http://www.example.com/images/logo.png");
URI top = new URI("http://www.example.com/");
URI relative = top.relativize(absolute);
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 46
3.3.4 Equality and Comparision of
URIs. String Representations

Refer Java Network Programming, E.R. Harold, pp. 150 - 151

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 47
3.4 x-www-form-urlencoded: URLEncoder and URLDecoder
• Major problem for web designers is to deal with difference between
operating systems.

• Major concern: naming convention for filenames and URL


• Some allow spaces some do not

• Some allow special characters some do not

• Filenames accept #, but in URL # indicates start of fragment identifier.

• Other special characters, nonalphanumeric characters might have one


meaning in an URL or on another Operating Systems.

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 48
• Hence characters used in URLs must come from a fixed subset of
ASCII, specifically:
• The capital letters A-Z
• The lowercase letters a-z
• The digits 0-9
• The punctuation characters -_.!~* and so on

• The characters : / & ? @ # ; $ + = and % may also be used, but only for their
specified purposes. If these characters occur as part of a path or query string, they
and all other characters should be encoded.

• Encoding is simple; Any characters that are not ASCII numerals, letters or
punctuation characters are converted into bytes and each byte is written as a percent
sign followed by two
Compiled hexadecimal
by: Manoj Pokharel @Nepathyadigits
College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 49
• Spaces are encoded as %20 and sometimes +

• + sign is encoded as %2B.

• The / # = & and ? Must be encoded when they are used as part of name,
not as a separator between parts of the URL.

• URL class does not encode or decode automatically. You construct URL
objects that use illegal ASCII and non-ASCII characters and then use
the Java URLEncoder and URLDecoder classes to perform encoding
and decoding

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 50
URLEncoder
• To encode a string, pass the string and the character set name to the
URLEncoder.encode() method. For example:

• String encoded = URLEncoder.encode("This*string*has*asterisks",


"UTF-8");

• URLEncoder.encode() returns a copy of the input string with a few changes.


Any non‐ alphanumeric characters are converted into % sequences (except the
space, underscore, hyphen, period, and asterisk characters).

• Although this method allows you to specify the character set, the only
such characterCompiled
set by:you should ever pick is UTF-8
Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 51
Class Lab 5: Show how the punctuation characters space, *, %, +, \, :, =, & are
encoded using the encode() method.

Class Lab 6: Also encode the google search query : “https://www.go


ogle.com/search?client=firefox-b-d&q=encoding+in+UETF8”

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 52
URLDecoder
• The URLDecoder class has a static decode() method that decodes string
encoded in x-www-form-url-encoded format.

• It converts all plus signs to spaces and all percent escapes to their
corresponding character.

• The syntax is:


public static String decode(String s, String encoding) throws
UnsupportedEncodingException

• Also throws an IllegalArgumentException if the string contains a percent sign


that isn’t followed by two hexadecimal digits.
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 53
• URLDecoder class doesnot touch non-escaped characters.

• You can simply pass an entire encoded URL rather then splitting it into
pieces first. The previously encoded string can be decoded as:

String input = "https://www.google.com/" +


"search?hl=en&as_q=Java&as_epq=I%2FO";
String output = URLDecoder.decode(input, "UTF-8");
System.out.println(output);

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 54
3.5 Proxies: System Properties, The ProxyClass and The ProxySelector

• Many systems access the WWW and Internet through proxy servers.

• A proxy server receives request from local clients and forwards to the
remote server, receives response from remote server and sends to client.

• Proxy server are used for security reasons:


• Prevent remote hosts from learning details about the local network

• Prevent users from accessing forbidden sites (filtering)

• Java programs based on the URL class can work with most proxy servers
and protocols.
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 55
3.5.1 System Properties
• For basic operations, you have to set few system properties to point to the
addresses of your local proxy servers.

• For pure HTTP proxy: set http.proxyHost to the domain name or the IP
address of proxy server and http.proxyPort to the port of the proxy
server.

• To to this, call the System.setProperty() method or use the –D option while


launching the program.

• If proxy requires a username and a password you need to use Authenticator


Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 56
• The example below sets the proxy server to 192.168.254.254 and the port
to 9000: using the -D command.
• java -Dhttp.proxyHost=192.168.254.254 -Dhttp.proxyPort=9000
com.domain.Program

• Altermatively, you can use the System.setProperty() method as:


• System.setProperty("http.proxyHost", "192.168.254.254");
System.setProperty("http.proxyPort", "9000");
System.setProperty("http.nonProxyHosts", "java.oreilly.com|xml.oreilly.com");

• The http.nonProxyHosts excludes the host from using the proxy server. i.e.
a direct connection is established with the specified host.
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 57
• Wildcard * can be used to indicate all hosts within a particular domain
that should not be proxied.
• System.setProperty("http.nonProxyHosts", *.oreilly.com");

• For FTP proxy, use ftp.proxyHost, ftp.ProxyPort and


ftp.nonProxyHosts.

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 58
3.5.2 The Proxy Class
• Manually choosing the proxy type and defining it manually as system
properties is not feasible.
• Hence we use the Proxy class, it allows more fine grained control of
proxy servers.
• To be specific, it allows you to choose different proxy servers for
different remote hosts.
• Proxy servers are represented by instances of the Proxy class and three
kinds of proxies do exist.
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 59
• Three proxies are: HTTP, SOCKS and DIRECT each represented as
three constants in the Proxy.Type enum:
• Proxy.Type.DIRECT

• Proxy.Type.HTTP

• Proxy.Type.SOCKS

• Here, the information (address, port) about proxy is provided as


SocketAddress object.
• SocketAddress address = new InetSocketAddress("proxy.example.com", 80);
Proxy proxy = new Proxy(Proxy.Type.HTTP, address);

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 60
3.6 Communicating with Server-Side programs through GET

• URL class helps Java applets and applications to communicate with


server-side programs that use the GET method.

• As a developer all you should know is what combination of names and


values the server side program receives.

• Then we can construct a URL with query string that provides the
names and values. The names and values must be x-www-form-
url- encoded using the URLEncoder.encode() method.

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 61
Additional Info:

Detrmining the syntax for a query string:

• Self written server side program => you already know the name value pairs

• Used a 3rd party program => documentation

• API => documentation

However, many programs are designed to process form input. In this case it is
easy to figure out what input the program excepts.

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 62
• The method the form uses should be the value of ‘method’ attribute
of the ‘form’ element. Method can be GET or POST. We deal with
GET.

• The part of the URL before the query string is given by the value of
‘action’ attribute of the form element.

• Finally, the ‘name’ attribute of the ‘input’ element denotes the


names of the name-value pairs and the values of the pair are
whatever user types into form.

• Consider an example:
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 63
Additional Info:
<form name="search" action="http://www.google.com/search" method="get">
<input name="q" />
<input type=“text" value="cafe.org" name="domains" />
<input type=“text" name="sitesearch" value="cafeconleche.org" />
<input type=“text" name="sitesearch2" value="cafeconleche.org" />
<br />
<input type="image" height="22" width="55"
src="images/search_blue.gif" alt="search" border="0“
name="search-image" />
</form>
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 64
• After you determine the name value pairs the server side program
expects, communicating with it is simple:

• To do so, we
• create a query string that includes the necessary name-value
pairs,
• Then form a URL that includes the query string
• Send the query string to the server
• Use the methods studied earlier to retrieve a HTML page.

• Example: Open Directory search option.


Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 65
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 66
• The Open directory interface has a simple form with one input field named
search.

• Input provided in this field is sent to a program at


http://search.dmoz.org/cgi- bin/search, which does the actual search.

• The HTML code for the form is shown below:

• <form class="center mb1em" action="search" method="GET">


<input style="*vertical-alig;" size="45" name="q" value="" class="qN">
<input style="*vertical-align;" value="Search“ class="btn" type="submit">
</form>
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 67
• So to submit a search request, we just need to append q=searchTerm to
https://dmoz-odp.org/search.

• For example, to search for ‘network’. You would open a connection to


the URL https://dmoz-odp.org/search?q=network and read the
resulting input stream.

• Class Lab: 7: communicating with the Youtube search program using


GET.

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 68
3.7 Accessing Password-Protected Sites

• Many sites require a username and password before accessing it.

• Authentication is implemented via HTTP authentication or through


cookies and HTML forms.

• The URL class can access sites that use HTTP authentication once the
username and password is provided.

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 69
3.7.1 The Authenticator Class

• Authenticator class is used when authentication is required to visit


some URL.

• Once it is known that authentication is required, it prompts the user for


the username and password.

• Credentials can be requested in two ways: GUI program would ask


credentials using dialog box while CUI program asks user to type
the username and password.

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 70
• The syntax of the Authenticator class is:

Public abstract class Authenticator extends Object{


//code of the authenticator class
}

• Then the member functions of the class can be used for authentication:
• getPasswordAuthentication()

• getRequesting Host()

• getRequestingPort

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 71
The PasswordAuthentication Class
• The class that processes two read-only attributes: username and password.

• The username is String and the password is a char array.

• The syntax is:

public PasswordAuthentication(String userName, char[] password)

• The username and password stored in the class is accessed using the getter
methods: public String getUserName() and public char[] getPassword()

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 72
The JPasswordField Class

• The JPasswordField component from Swing is used for asking


passwords in secure fashion. The syntax is:

public class JPasswordField extends JTextField

• It behaves exactly as a text field but anything typed in the field


appears as astriesk.

• JpasswordField also stores passwords as char array and provides the


getPassword() method to return password.

Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 73
Compiled by: Manoj Pokharel @Nepathya College, Reference: E.R Harold, Java Network Programming,O’Reilly 2014 74
Unit 4 : HTTP

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 1
4.1 The Protocol:
HTTP
• Standard protocol for communication between web browsers and web servers.

• HTTP specifies how :

• Client and Sever establish a connection

• Client requests data from server

• Server responds to the request

• And how the connection is closed.

• HTTP uses TCP/IP for data transfer

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 2
• Each HTTP request from client to server includes four steps:
• Client opens TCP connection to the server using the given port in URL
(default : 80)
• Client sends message to the server requesting the resource at the specified
path. Resource includes header.
• The server sends response to the client. The response includes a response
code followed by a header, and the requested document or an error message.
• The server closes the connection

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 3
•• EEaacchh rreeqquueesst aanndd rreessppoonnssee inn HHTTTTPP hhaass ssaammee bbaassicc foorrmm: aa hheeaaddeerr lliinnee,,
aann HHTTTTPP
header containing metadata, a blank line and then a message body.

GET /index.html HTTP/1.1 HTTP/1.1 200 OK Status Line


Request Lin e Date: Sun, 21 Apr 2013 15:12:46 GMT
User-Agent: Mozilla/5.0 (Macintosh; Intel Server: Apache
Mac OS X 10.8; rv:20.0)Gecko/20100101 Connection: close
Firefox/20.0 Content-Type: text/html; charset=ISO-8859-1
Header

Host: en.wikipedia.org Content-length: 115


Connection: keep-alive <html>
Accept-Language: en-US,en;q=0.5 <head>
<title>
Accept-Encoding: gzip, deflate A Sample HTML file
Accept:text/html,application/xhtml+xml,ap </title>
plication/xml;q=0.9,*/*;q=0.8 </head>
<body>
The rest of the document goes here
</body>
</html>

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 4
HTTP Keep-
Alive:
• HTTP version 1.0 opens a new connection for each request.

• In sessions where many small documents need to be transmitted, the time taken to
open and close connections is more than the time taken to transmit the data.

• The case seems more problematic for encrypted HTTPS connections using SSL
or TLS. (due to handshake required for secure socket)

• In HTTPS 1.1 and later, the server does not have to close the socket after it
sends response.

• i.e. it can leave the socket open and wait for a new request from the client on
the same socket.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 5
• A client indicates that it is willing to reuse the socket by including a Connection field in
the
HTTP request header with the value Keep-Alive.
• Connection: Keep-Alive

• The URL class supports HTTP Keep-Alive unless explicitly turned off. i.e. if the
client connects to the same server before the server has closed the connection, the
socket is reused.

• HTTP’s Keep-Alive attribute can be controlled with several system properties.

• http.keepalive to ‘true or false’: to enable or disable Keep-Alive (Default:Enabled)

• http.maxCconnections: no of sockets to keep open at a time. (Default: 5)

• Other properties doManoj


Compiled by exist, much
Pokharel@ Nepathyaof a less
College, Ref. Javaconcern for us.
Network Programming, 4 edition, Elliotte Rusty Harold
th 6
4.2 HTTP
Methods:
• Communication with an HTTP server follows a request-response pattern.

• Start line: Contains HTTP method and Path to resource


• Header containing name value fields that provide meta-information such
as authentication credentials
• Request body containing a representation of resource (POST and PUT
only)

• There are four main HTTP methods:

• GET, POST, PUT, DELETE

• Each has their own semantics and usage corresponding to Read, Create,
Update, Delete (CRUD)
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4 th edition, Elliotte Rusty Harold 7
GET Method:
• This method is used to read (retrieve) a representation of a resource.

• In case of success, GET returns a representation in JSON and an HTTP


response status code 200 (OK).

• In case of error, it often returns a code 404 (NOT FOUND) or 400 (BAD
REQUEST)

• HTTP GET methods are considered safe. i.e. they can be used without any risk
of data
modification or corruption.

• Calling it once and calling it multiple times has no any effect on data
modificationCompiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4 edition, Elliotte Rusty Harold
th 8
POST Method
• POST method is most often utilized to create new resources.

• Used for creating new instances of resources.

• Example: feeding data to a database table

• On successful execution of POST, code 201 is returned.

• POST is not safe operation. i.e. making two identical POST request will result
in two resources containing the same information but with different identifiers.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 9
PUT Method
• The PUT method uploads a representation of a resource to the server at a
known URL.

• It is not side effect free but idempotent. i.e. executing the same identical
request multiple times leaves the server in the same state. (No any effect)

DELETE Method:
• The DELETE request is used to delete a resource identified by the URL.

• Is not side effect free but idempotent.

• Deleting the same resource multiple times has no effect on state of data.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 10
4.3 The Request
Body
• Format of the request body for HTTP methods

• GET: retrieves the representation of the resource identified by the URL.

• The location of the resource is given by : path and query string of


URL.
• Hence no any special request body in case of URL.

• POST and PUT: in these cases, the client supplies the representation of the
resource along with the path and the query string.
• The representation is sent in the body of the request, after the header.

• That is, it sends these four items in order:


Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 11
• A starter line including the method, path and query string and HTTP
version

• An HTTP header

• A blank line

• The request body

POST /cgi-bin/register.pl HTTP 1.0


Date: Sun, 27 Apr 2013 12:32:36
Host: www.cafeaulait.org
Content-type: application/x-www-
form-urlencoded
Content-length: 54

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 12
• The HTTP header should include two fields that specify the nature of the body.

• A Content-length field that specifies how many bytes are in the body.

• A Content-type field that specifies the MIME media type of the bytes.

• For Example in previous slide. The request body is of 54 bytes and the media
type is (application/x-www-form-urlencoded)

• However other media types can also be defined, a picture can be uploaded
as image/jpeg, text editors might send text/html.
• Note: MIME = Multipurpose Internet Mail Extensions, specifies the content type or media
on Internet

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 13
• Example, a PUT request that uploads an Atom
document:
PUT /blog/software-development/the-power-of-pomodoros/ HTTP/1.1
Host: elharo.com
User-Agent: AtomMaker/1.0
Authorization: Basic ZGFmZnk6c2VjZXJldA==
Content-Type: application/atom+xml;type=entry
Content-Length: 322

<?xml version="1.0"?>
<entry xmlns="http://www
.w3.org/2005/Atom">
<title>The Power of Pomodoros</title>
<id>urn:uuid:101a41a6-722b-4d9b-8afb-
ccfb01d77499</id>
<updated>2013-02-22T19:40:52Z</updated>
</entry> Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4 th edition, Elliotte Rusty Harold 14
<author><name>Elliotte
Cookies
• Many websites use small strings of text known as cookies to store persistent
client-side state between connections.

• Cookies are passed from server to client and back again in the HTTP headers
of requests and responses.

• Cookies are used by server to indicate session IDs, login credentials, shopping
cart
details, user preferences etc.

• Cookies actually do not store the data but point to the data stored in server.

• Cookies are limited to non-whitespace ASCII text, and doesnot contain commas
or semicolons.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4 th edition, Elliotte Rusty Harold 15
• To set a cookie in a browser, the server includes a Set-Cookie header line in the
HTTP header.

HTTP/1.1 200 OK
Content-type: text/html
Set-Cookie: cart=ATVPDKIKX0DER

• If the browser makes a second request to the same server, it will send the
cookie back in Cookie line in the HTTP request header like:

GET /index.html HTTP/1.1


Host: www.example.org
Cookie: cart=ATVPDKIKX0DER
Accept: text/html
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 16
• Servers can set more than one cookie.

• In addition to a name = value pair , cookies can have other attributes that control
their scope including expiration date, path, domain, port, version and security
options. Example; cookie for subdomians,
• Set-Cookie: user=elharo;Domain=.foo.example.com

• Expires attribute: Set-Cookie: user=elharo; expires=Wed, 21-Dec-2015 15:23:00


GMT

• Max-Age attribute: Set-Cookie: user="elharo"; Max-Age=3600

• Other similar attributes do exist.


Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 17
CookieManager
• Java provides java.net.CookieManager subclass of CookieHandler that is used
for manipulating cookies.

• The first step is to enable the CookieHandler using the CookieManager.

CookieManager manager = new CookieManager();


CookieHandler.setDefault(manager);

• These two lines of code allow Java to store any cookies sent by HTTP servers
that you connect to by using the URL class.

• It will also send the stored cookies back to those servers in subsequent requests.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 18
• Further, you can specify which and whose cookies to accept using the
CookiePolicy. The policies are fixed and predefined:
• CookiePolicy.ACCEPT_ALL All cookies allowed

• CookiePolicy.ACCEPT_NONE No cookies allowed

• CookiePolicy.ACCEPT_ORIGINAL_SERVER Only first party cookies allowed

• Example to block third party cookies but accept 1st party cookies.
CookieManager manager = new CookieManager();
manager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
CookieHandler.setDefault(manager);

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 19
CookieStore
• In cases when cookies are necessary for manipulation locally, one can get the cookies stored in the disk
using the CookieStore.

• You can retrieve the cookies stored by the CookieManager using the getCookieStore() method.

• The CookieStore class allows to add remove and list the cookies. Following methods can be used:

public void add(URI uri, HttpCookie cookie)


public List<HttpCookie> get(URI uri)
public List<HttpCookie> getCookies()
public List<URI> getURIs()
public boolean remove(URI uri,
HttpCookie cookie)
public boolean removeAll()
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4 th edition, Elliotte Rusty Harold 20
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 21
Unit 5: URLConnections

1
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold
URLConnection
• Abstract class that represents an active connection to a resource specified by
a URL.

• URLConnection class has two purposes:


• Provides more control while communicating with the server than the URL class
• Can inspect header sent by server, set the header fields in the client request

• Send data to a web server with POST and PUT methods

• Many of the methods and fields including the constructor in the class are
protected.
• Can only be accessed by the instances of the class or its subclasses.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 2
5.1 Opening URLConnections
• A program can open connection to a URL following basic sequence of
steps:
1. Construct a URL object

2. Call the URL objects openConnection() method to retrieve a


URLConnection object for the URL.

3. Configure the URLConnection

4. Read the header fields.

5. Get an input stream and read data

6. Create an output stream and write data

7. Close the connection


Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 3
• The constructor for the URLConnection class is protected:
protected URLConnection(URL url)

• One can open a connection by invoking the openConnection() method


of the URL class using the URL object.

try {
URL u = new URL("http://www.overcomingbias.com/");
URLConnection uc = u.openConnection();
// read from the URL...
} catch (MalformedURLException ex) {
System.err.println(ex);
} catch (IOException ex)
{ System.err.println(ex);}
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 4
5.2 Reading Data from a
Server
•The steps needed to retrieve data from a URL using a
URLConnection object are:
1. Construct a URL object

2. Invoke the URL object’s openConnection() method to retrieve


a URLConnection object for that URL.

3. Invoke the URLConnection’s getInputStream() method.

4. Read from the input stream.

• The getInputStream() method returns a InputStream that lets you read


and parse the data that the server sends.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 5
• A program to read data from server specified by URL.
public static void main (String[] args) {
if (args.length > 0) {
try { // Open the URLConnection for reading
URL u = new URL(args[0]);
URLConnection uc = u.openConnection();
try (InputStream raw =
uc.getInputStream()) { // autoclose
InputStream buffer = new
BufferedInputStream(raw); // chain the
InputStream to a Reader
Reader reader = new
InputStreamReader(buffer);
int c;
while ((c = reader.read()) != -1) {
System.out.print((char) c);
}}} catch (MalformedURLException ex)
6
{ System.err.println(args[0]
}}}} + "Pokharel@
Compiled by Manoj is not aNepathya
parseable
College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold
• The program is similar to the one implemented in 3.2 Retrieving data from
URL.

• If both are same, why use URLConnection?


• Certain differences do exist

• URLConnection provides access to the HTTP header

• URLConnection can configure the request parameters sent to the server

• URLConnection can both read data from server and write data to server.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 7
5.3 Reading Header:
• HTTP servers provide plenty of information in the header that precedes
each response. See an sample header returned by an Apache web
server:
HTTP/1.1 301 Moved Permanently
Date: Sun, 21 Apr 2013 15:12:46 GMT
Server: Apache
Location:
http://www.ibiblio.org/ Content-
Length: 296
Connection: close
Content-Type: Compiled
text/html;
by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 8
• HTTP header may include:
• The content type of the requested document

• Length of the document in bytes

• The character set in which the content is encoded

• The date and time

• The date the content expires and so on.

• Here we deal with finding the metadata that server has provided and
retrieving the header fields.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 9
5.3.1Retrieving Specific Header
Fields
• There exist several methods that can be used to extract fields from header:
fields can be specific or arbitrary.

• Following six methods can be used to extract common fields from the
header:

1. public String getContentType => returns media type of the response


body
• Relies on the server to send valid content type.

• It throws no exceptions and returns null if the content is not available.

• Most common type is text/html, other types include text/plain, image/gif,


application/xml and image/jpeg
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 10
public class EncodingAwareSourceViewer {
public static void main (String[] args) {
for (int i = 0; i < args.length; i++) {
try {
// set default encoding
String encoding = "ISO-8859-1";
URL u = new URL(args[i]);
URLConnection uc = u.openConnection();
String contentType = uc.getContentType();
int encodingStart =
contentType.indexOf("charset=");
if (encodingStart != -1) {
encoding =
contentType.substring(encodingStart + 8);
}
InputStream in = new BufferedInputStream(uc.getInputStream());
Reader r = new InputStreamReader(in, encoding);
int c;
while ((c = r.read())Compiled
!= -1) by
{ Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 11
} r.close();
2. public int getContentLength() => returns the number of bytes in the
content.
• If no content exists , it returns -1
• Useful when you need to know exactly how many bytes to read or create a buffer large
enough to hold the data in advance.

3. public String getContentEncoding() => returns a string that tells how the
content is encoded.
• If the content is unencoded (common in HTTP servers) this method returns null.

4. Public long getDate() => returns a long that tells you when the document
was sent, in milliseconds since midnight, GMT, January 1,1970
• Can be converted inti java.util.Date using Date docdate = new Date(uc.getDate());
• If header has no date field, it returns zero.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 12
5. public long getExpiration() => some documents come with expiration dates that
indicates when the document should be deleted from the cache and reloaded
from the server.
• Similar to getDate(), returns a long indicating the number of milliseconds after
12:00 AM,GMT Janauary 1, 1970 at which the document expires.
• If no expiry date specified, returns zero.

6. public ling getLastModified()


• Returns the date on which the document was last modified.

• (similar to above)

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 13
5.3.2 Retrieving Arbitrary Header
• TheFields
previous six methods requested most common fields from the header.

• But there exist other header fields that a message can contain.

• The following five methods can be used to inspect these arbitrary fields:

1. public String getHeaderField(String name): returns the value of the


named header field.
• Name is not case sensitive.

• For example to get the value of Content-type of a URLConnection object (say


uc), the code becomes: String contentType = uc.getHeaderField("content-type");

• Write code for Content-Encoding, date, expires


Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 14
2. public String getHeaderFieldKey(int n): returns the field name of the nth
header field:
• The request method and path is at field zero and has null key.

• The first field of header has key 1.

• For example to get the sixth key of the header of the URLConnection object uc
the code becomes: String header6 = uc.getHeaderFieldKey(6);

3. public String getHeaderField(int n): returns the value of the nth header field.
• The starter line containing the request method and path is header field zero

• The actual header field is one.

• Example: pint the entire HTTP header of a URLConnection object uc


Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 15
4. public long getHeaderFieldDate(String name, long
default):
• This method first retrieves the header field value specified by the name
argument and converts the value into long that specifies the milliseconds
since midnight, Janauary 1, 1970
• Method only manipulates the field that contain value as date and also
the default value (0) must be provided.
• Example:

Date expires = new Date(uc.getHeaderFieldDate("expires", 0));


long lastModified = uc.getHeaderFieldDate("last-modified", 0);
Date now = new Date(uc.getHeaderFieldDate("date", 0));
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 16
5. public int getHeaderFieldInt(String name, int default): retrieves the value
of the header field spedified in the name and tries to convert to int.
• Accepts the field name and default value as argument

• If it fails to parse int or cant find the header it returns the default
value.
• Example: to retrieve content-length field of the header fron
the URLConnection object uc, the code would be:

int contentLength = uc.getHeaderFieldInt("content-length",


-1);

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 17
5.4 Web Cache for Java
• Web browsers have been caching pages and images for years.

• Several HTTP headers including the Expires and Cache-control can


control caching.

• By default, any web page accessed using GET method over HTTP can
be cached.

• A page accessed with HTTPS and POST usually need not be cached.

• However, the case is different for Java.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 18
• By default, Java does not cache anything.

• You need to install the system-wide cache of the URL class.


• A subclass of ResponseCache
• A subclass of the CacheRequest
• A subclass of the CacheResponse

• Once the cache is installed whenever a system tries to load new URL, it will
first look for it in the cache.
• If the content is found, cache returns the desired content and
the URLConnection wont need to connect to the remote server.
• If the desired content is not found, the data will be fetched from the server
and the data will be put in the cache
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 19
• Setting up the system-wide cache is done using the setDefault method:

static void setDefault(ResponseCache responsecache)

• Two methods in the ResponseCache class store and retrieve data from the systems cache:
• public abstract CacheResponse get(URI uri, String requestMethod, Map<String, List<String>> requestHeaders)
throws IOException

• public abstract CacheRequest put(URI uri, URLConnection connection) throws IOException

• The put() method returns a CacheRequest object that wraps an OutputStream into
which the URL will write cachable data it reads.

• The get() method in ResponseCache retrieves the data and headers from the cache
and returns then wrapped in CacheResponse object.
• If the desired URI is not in the cache, it returns null and URI is loaded form server.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 20
5.5 Configuring the
Connection:
• The URLConnection class has seven protected instance fields that define
exactly how the client makes request to the server.
protected URL url;
protected boolean doInput = true;
protected boolean doOutput = false;
protected boolean
allowUserInteraction =
defaultAllowUserInteraction;
protected boolean useCaches =
defaultUseCaches;
protected long ifModifiedSince = 0;
protected boolean connected =
false;
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 21
• The following getter/setter methods do exist:
public URL getURL()
public void setDoInput(boolean doInput)
public boolean getDoInput()
public void setDoOutput(boolean doOutput)
public boolean getDoOutput()
public void setAllowUserInteraction(boolean allowUserInteraction)
public boolean getAllowUserInteraction()
public void setUseCaches(boolean useCaches)
public boolean getUseCaches()
public void setIfModifiedSince(long
ifModifiedSince)
public long getIfModifiedSince()

• Modification to these fields are acceptable before the URLConnection


is connected. (before
Compiled reading
by Manoj Pokharel@ content
Nepathya College, Ref.or headers
Java Network from4th connection)
Programming, edition, Elliotte Rusty Harold 22
• Protected URL url : The url field specifies the URL that this
URLConnection connects to.
• The constructor sets it when the URLConnection is created. Value can be retieved
using the getURL() method.
• public class URLPrinter {
public static void main(String[] args) {
try {
URL u = new URL("http://www.oreilly.com/");
URLConnection uc = u.openConnection();
System.out.println(uc.getURL());
} catch (IOException ex) {
System.err.println(ex);
}}}
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 23
• protected boolean connected: the attribute connected is true if the connection
the URL is open and false if it is closed.
• Its initial value is set to false.

• protected boolean allowUserInteraction: some URLConnections need to


interact with a user. For example: a browser may ask for username and
password.
• It is false by default.

• Getters and setters can be used to read and change its values

• True indicates that the user interaction is allowed , false indicates that user
interaction is not possible.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 24
• Example for allowUserInteraction:

• URL u = new URL("http://www.example.com/passwordProtectedPage.html");


URLConnection uc = u.openConnection();
uc.setAllowUserInteraction(true);
InputStream in = uc.getInputStream();

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 25
• Protected boolean doInput: A URLConnection can be used for reading
from a server, writing to a server, or both.
• The doInput field is true if the URLConnection can be used for reading,
false if it doesnot allow read.
• Getter and setter methods can be used to read the value and change the
value.

• protected boolean doOutput: Programs can use URLConnection to send


output back to the server.
• If the field doOutput is true, the URLConnection can be used for writing.
if it
is false it cannot be used for writing.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 26
• Example for doInput • Example for doOutput
• try {
• try {
URL u = new URL("ht
URL u = new URL("http://www.oreilly.com"); tp://www.oreilly.com");
URLConnection uc = u.openConnection(); URLConnection uc =
if (!uc.getDoInput()) u.openConnection();
{ uc.setDoInput(true); if (!uc.getDoOutput()) {

} uc.setDoOutput(true);

// read from the connection... }


// write to the
} catch (IOException ex) { connection...
System.err.println(ex); } catch (IOException ex) {
} System.err.println(ex);
}
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 27
• Protected boolean ifModifiedSince: many browsers and proxies keep
caches of previously retrieved documents. If user asks for the same
document again, it can be retrieved from the cache.
• However it may have changed on the server since it was last retrieved.
The only way to ensure this is to ask the server.
• Clients can include ifmodifiedSince in the client request HTTP header.

• The header includes a date and time. If the document has changed since that
time, the server should send it. Otherwise it replies with a 304 Not
Modified message.
• The client then loads the document from its cache.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 28
• protected boolean useCaches: Some web browsers can retrieve a document from local
cache, rather than retrieving it from server.
• The useCaches variable determines whether a cache will be used if its available.

• Default value is true, If the program/app does not wish to use cache set to false

• Getter and Setter can be used.

• Timeouts: four methods query and modify the timeout values for connections that specifies
how long the socket will wait for remote host before throwing SocketTimeoutException.
• The ConnectTimeout() (get/set) method controls how long the socket waits for initial
connection.
• The ReadTimeout() (get/set) method specify how long the input stream waits for
data to arrive.
29
• If value = 0, indicates never timeout.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold
• Example for useCaches

• try {
URL u = new URL("http://www.sourcebot.com/");
URLConnection uc = u.openConnection();
uc.setUseCaches(false); • Example for Timeouts
• URL u = new URL("ht
// read the document...
tp://www.example.org");
} catch (IOException ex) {
URLConnuction uc = u.openConnection();
System.err.println(ex); uc.setConnectTimeout(30000);
} uc.setReadTimeout(45000);

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 30
5.6 Configuring the Client Request HTTP
Header
• A client using HTTP sends the data along with a request line and a header. Example:
GET /index.html HTTP/1.1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:20.0) Gecko/20100101
Firefox/20.0 Host: en.wikipedia.org
Connection: keep-alive
Accept-Language: en-
US,en;q=0.5 Accept-Encoding:
gzip, deflate
Accept:
text/html,application/xhtml+xml,a
pplication/xml;q=0.9,*/*;q=0.8

• A web server can use this information to serve different pages to different clients, to get
and set cookies,Compiled
header authenticate users through passwords and more
by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 31
• Each URLConnection sets a number of different name-value pairs in the header by
default.

• However, you can modify these and add new fields before connecting.
This modification is referred as configuring the HTTP request header.

• Adding headers is done using the setRequestProperty() method before opening


the connection. Using the method after connection throws IllegalStateException.

public void setRequestProperty(String name, String value)

• It adds a field to the header of the URLConnection object with the specified name
and value.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 32
• HTTP allows single name to have multiple values. In this case the separate
values are separated by commas.

• Given a URLConnection object uc, the header field ‘Accept: text/html, image/gif,
image/jpeg’ can be set as follows:

• uc.setRequestProperty(“Accept”, “text/html, image/jpeg, image/gif ”)

• Similarly Cookie field ‘Cookie: username=elharo; password=AD0; session=05’


can be set as: uc.setRequestPropery(“Cookie”, ‘’username=elharo; password=
”)

• The setRequestProperty changes the existing property value. To add new


property value, use addRequestProperty() method.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 33
5.7 Security Considerations for
URLConnections
• URLConnection object is used for making network connections, reading or
writing files, getting and setting headers and so on.

• However, all the URLConnection objects are subject to security restrictions.

• Before attempting to connect to a URL and reading and writing files, you
should confirm weather the connection will be allowed.

• Permission can be verified using the getPermission() method.


• public Permission getPermission() throws IOException

• The method returns a java.security.Permission object that specifies what a permission


is need to connect to the URL. Returns null if no permission is needed.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 34
5.9 HttpURLConnection
• The java.net.HttpURLConnection class is an abstract subclass of the
URLConnection.

• This class provides additional methods that are helpful when working
specifically with the http URLs.

• Particularly, it has:
• Methods to get and set request method (GET, POST, PUT, DELETE)

• Get response code and message

• Figure use of proxy server

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 35
• You cannot directly create its object because it is an abstract class and
its only constructor is protected.

• You can create an HttpURLConnection from the existing URLConnection


object by casting the URLConnection type as HttpURLConnection
object.
• URL u = new URL("http://lesswrong.com/");
URLConnection uc = u.openConnection();
HttpURLConnection http = (HttpURLConnection) uc;

• OR

• URL u = new URL("http://lesswrong.com/");


HttpURLConnection http
Compiled by Manoj = (HttpURLConnection)
Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 36
5.9.1 The Request
Method
• Every request from client to server includes a request line. In general, the request
line begins with a method and path of the resource the client points to and the
version of the HTTP protocol. For example:

• GET /catalog/jfcnut/index.html HTTP/1.0

• Also clients can POST responses to forms, PUT files to server or DELETE
files form server, get HEADer of a document an so on.

• These tasks are accomplished by changing the request method in the request
line.

• By default, HttpURLConnection uses the GET method. However you can


modify the method using the setRequestMethod() method.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 37
• The syntax is:

public void setRequestMethod(String method) throws ProtocolException

• The method argument should be one of these seven case-sensitive strings.


• GET, POST, HEAD, PUT, DELETE, OPTIONS, TRACE

• If any other method is used then a java.net.ProtocolException, is raised.

• However it is not enough to simply set the request method, you must adjust
the HTTP header and also provide a message body depending on the
header as well.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 38
• HEAD: method instructs the sever to return only the HTTP header, not the file.

• HEAD can be used to check either a resource has been modified since last time or not.

• try {
URL u = new URL(args[i]);
HttpURLConnection http = (HttpURLConnection) u.openConnection();
http.setRequestMethod("HEAD");
System.out.println(u + " was last modified at "
+ new Date(http.getLastModified()));
} catch

//Error handling}

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 39
• OPTIONS: The options method asks what options are supported for a particular
URL. For example, for the request
• OPTIONS /xml/ HTTP/1.1
Host: www.ibiblio.org
Accept: text/html, image/gif, image/jpeg
Connection: close

• The response is:


• HTTP/1.1 200 OK
Date: Sat, 04 May 2013 13:52:53 GMT
Server: Apache
Allow: GET,HEAD,POST,OPTIONS,TRACE
Content-Style-Type: text/css
Content-Length: 0
Connection: close
Content-Type: text/html; charset=utf-8
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 40
• TRACE: The TRACE request method sends the HTTP header that the
server received from the client.

• It is mainly used to see what proxy server is changing in the clients request.
• If no proxy server is used, the Response:
HTTP/1.1 200 OK
part of the messages in red
Date: Sat, 04 May 2013 14:41:40 GMT
remain identical or else might Server: Apache
change. Connection: close
Request: Content-Type:
TRACE /xml/ HTTP/1.1 message/http
Hello: Push me TRACE /xml/ HTTP/1.1
Host: www.ibiblio.org Hello: Push me
Accept: text/html, image/gif, image/jpeg Host: www.ibiblio.org
Connection: close Accept: text/html, image/gif, image/jpeg
Connection: close
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 41
5.9.2 Disconnecting from the
Server
• HTTP 1.1 and later versions support persistent connections that allow
multiple requests and responses to be sent over a single TCP socket
using the Keep-Alive attribute.

• When the Keep-Alive attribute is used, the server wont close the connection
even all the bytes of the requested data is sent because servers expect that
the client may send further data.

• However servers will time out and close the connection in 5 seconds on
inactivity, it is preferred for the client to close the connection as soon as it
knows that its task is complete.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 42
• As every HTTP protocol, HttpURLConnection class transparently
supports HTTP Keep-alive unless explicitly turned off.

• Once you know you are done talking with the server, you can use
the disconnect() method to break the connection with the server.

• The syntax is: public abstract void disconnect()

• The disconnect() method closes all the streams that are open on the
specified connection(if any).

• However closing the stream on the persistent connection does not close the
socket and disconnect.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 43
5.9.3 Handling Server
Responses
•The Status line of an HTTP response includes a numeric code and
a message referred as response code and response message.

• Most common HTTP response is 200 OK

• These responses determine the further actions to be taken by the


program.

• These responses can be extracted using two methods of the


HttpURLConnection class.
public int getResponseCode() throws IOException

public String getResponseMessage() throws IOException

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 44
5.9.4 Proxies
• Many users behind firewalls or other ISP’s access the Web through proxy
servers.

• The HttpURLConnection provides the usingProxy() method that


tells whether the particular connection is going through a proxy
server.

• The syntax is: public abstract boolean usingProxy()

• It returns true if a proxy is being used, false if proxy is not being


used.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 45
5.9.5 Streaming Mode
• When you need to send and receive data Java caches everything that is fed
into the OutputStream retrieved from the HttpURLConnection until the
stream is closed.

• The scheme is fine for small requests such as form data. However it is not
good for large responses or other huge messages.

• It is very inefficient if you need to share large files since everything needs
to be cached.

• Java has two solutions to this problem.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 46
• First, if you know the size of data (if you are uploading the file of known
size using PUT)- you can tell the HttpURLConnection object the size of
data.

• Second, if you do not know the size, you can use chunked transfer.

• Chunk transfer the request body data is split into pieces and sent in multiple
pieces each with its own content length.

• Chunked transfer can be enabled using the method:

public void setChunkedStreamingMode(int chunkLength)

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 47
End of Unit

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 48
Unit 6: Socket for Clients

1
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold
6.1 Introduction to
•sockets
Data is transmitted as packets called datagrams.
• Header + Payload = Datagram
• Address + Port No. (both sender and receiver) + other details = Header
• Details = checksum (error detection), fragmentation info
• Keeping track of all these tasks and managing these is a tedious task.
• Hence sockets come in play,
• Socket allow a programmer to treat a network connection as a stream onto
which
the bytes can be written and read.
• Low level details such as error detection, packet sizes, fragmentation and
reassemblyCompiled
all are automatically
by Manoj handled
Pokharel@ Nepathya College, by sockets
Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 2
6.2 Using Sockets
• A socket is a connection between two hosts.
• Address + Port = Socket (Example)

• A socket can be used to perform seven basic operations:


• Connect to a remote machine

• Send and Receive data

• Close a connection

• Bind to a port

• Listen for incoming data

• Accept connection from remote machine on the bound port.


Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 3
• The first four operations, used by both clients and servers are provided by
the Java’s Socket class.

• The later three operations are applicable for server are provided by the
ServerSocket class.

• Java programs normally employ client sockets in the following


fashion:
• Create a new socket with constructor

• Use socket to connect to the remote host

• The connection then gets the input output streams from the socket and
use these streams to send and receive data to each other.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 4
• The connection is full duplex.
6.2.1 Investigating Protocols with Telnet
• Telnet provides a simple way that demonstrates how a protocol operates.

• By default telnet attempts to connect to port 23. To connect to servers in


different port, server and port can be specified as follows:
• $telnet server port

• Example: $telnet localhost 25

• the above command requests a connection to port 25, the SMTP port on
the local machine.

• Requires servers such as Postfix, Exim, OpenSMTPD etc.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 5
6.2.2 Reading from Servers with
Sockets
• We will connect to the daytime server at the National Institute for Standards
and Technology (NIST) and fetch the current time.

• The server listens on port 13 and sends the UTC time in human readbale
format and closes the connection. Example:
• telnet time.nist.gov 13 returns 59659 22-03-21 05:53:07 50 0 0
881.9 UTC(NIST) *Connection to host lost.

• The line “59659 22-03-21 05:53:07 50 0 0 881.9 UTC(NIST)” is sent by


the server. Reading the socket InputStream produces same output.

• How do you retrive the same data using Java Sockets?


Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 6
• First open a socket to time.nist.gov on port 13:
• try (Socket socket = new Socket("time.nist.gov", 13)) {
// read from the socket...
} catch (IOException ex) {
System.err.println("Could not connect to time.nist.gov");
}

• Now we use the InputStream() object to read bytes from the socket:
• InputStream in = socket.getInputStream();
StringBuilder time = new StringBuilder();
InputStreamReader reader = new InputStreamReader(in, "ASCII");
for (int c = reader.read(); c != -1; c = reader.read())
{ time.append((char) c);
}
7
System.out.prinCotmlnpi(letdibmy Mea)n;oj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty
• The overall program looks as follows:
public static void main(String[] args) {
String hostname = args.length > 0 ? args[0] : "time.nist.gov"; Socket socket = null;
try {
socket = new Socket(hostname, 13);
socket.setSoTimeout(15000);
InputStream in = socket.getInputStream();
StringBuilder time = new StringBuilder();
InputStreamReader reader = new InputStreamReader(in, "ASCII");
for (int c = reader.read(); c != -1; c = reader.read())
{ time.append((char) c);
}
System.out.println(time);
} catch (IOException ex) {
System.err.println(ex);
} finally {
if (socket != null) {
try
{ socket.close();
} catch
(IOException ex) {
// ignore
}}}}} 8
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold
6.2.3 Writing to servers with Sockets
• Writing to sockets is quite similar to reading from them. Here you simply
ask the socket for an output stream as well as an input stream.

• See an example of TCP protocol dict, that communicates with the


dict server using port 2628.

• Example:
• telnet dict.org 2628

• DEFINE fd-eng-ara hello

• See the response and definition sent by the server, you never know how
the communication took place lets implement it in Java.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 9
• Open socket: Socket socket = new Socket("dict.org", 2628);

• Set timeout(prevents server hang): socket.setSoTimeout(15000);

• 1st we need to send request, create an OutputStream using socket object:


OutputStream out = socket.getOutputStream();

• Chain stream to a writer:Writer writer = new OutputStreamWriter(out, "UTF-


8");

• Write command over the socket: writer.write("DEFINE fd-eng-ara hello\r\n")

• Flush the output: writer.flush();

• Now the server sends response to the request. Use the input stream to read output.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 10
InputStream in = socket.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in, "UTF-8"));
for (String line = reader.readLine();
!line.equals(".");
line = reader.readLine()) {
System.out.println(line);
}

• Finally, disconnect
from the server:
writer.write("quit\r\n");

writer.flush(); Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 11
6.3 Constructing and connecting Sockets: Basic
Constructors
• Each constructor for Socket specifies the host and the port to connect to.

• Hosts can be specified as an string or an InetAddress object.

• Remote ports are specifies as int values from 1 to 65535.


• public Socket(String host, int port) throws UnknownHostException, IOException
public Socket(InetAddress host, int port) throws IOException

• These constructors connect to the socket, if connection cannot be opened


IOException is raised also if host cannot be resolved UnknownHostException is
raised.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 12
Example: Find active ports on a specified
host.

• Refer to complete program.

• Is the program applicable?


• Yes, as an network programmer, you need to secure a system.

• This program helps determine the open ports and rouge servers using
these ports.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 13
6.3.2 Picking a Local Interface to Connect
From
• You can choose among the local interfaces by which you wish to establish a
socket connection to a server.

• The following constructors can be used:


• public Socket(String host, int port, InetAddress interface, int localPort) throws
IOException, UnknownHostException
public Socket(InetAddress host, int port, InetAddress interface, int localPort)
throws
IOException

• The first two arguments in both constructors specify the host and port to connect
to.

• The last two arguments specify the local interface from which connection is
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 14
established.
6.3.3 Constructing without
•Connecting
All the constructors studied so far create the socket and open a network connection to a
remote host.

• But sometimes these tasks must be split. This can be done using constructor:
• public Socket()

• Later on, connection can be established by passing a SocketAddress to the connect() method.
• try {
Socket socket = new Socket();
// fill in socket options
SocketAddress address = new InetSocketAddress("time.nist.gov", 13);
socket.connect(address);
// work with the sockets...
} catch (IOException ex) {
15
System.err.println(ex);}Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold
Original Constructor NoArgs Constructor
Socket socket = Socket socket = new Socket();
null; try { SocketAddress address = new
socket = new InetSocketAddress(SERVER, PORT);
Socket(SERVER, try {
PORT); socket.connect(address);
// work with the // work with the socket...
socket... } catch (IOException ex) {
} catch (IOException ex) { System.err.println(ex);
System.err.println(ex); } finally {
} finally { try
if (socket != null) { { socket.close
try ();
{ socket.close(); } catch
} catch (IOException
(IOException ex) { ex) {
// ignore //Programming,
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network ignore 4th edition, Elliotte Rusty Harold 16
6.3.4.1 Socket
Addresses
• The SocketAddress class represents a connection endpoint and has only
a constructor.
• The SocketAddress class acts as a convenient store for transient socket
connection information such as the IP address and port that can be reused to
create new sockets even after the original socket is disconnected and garbage
is collected.
• The Socket class has two methods that return SocketAddress objects.
• public SocketAddress getRemoteSocketAddress() : returns the address of the system being
connected
public SocketAddress getLocalSocketAddress(): returns the address from which the connection
is made 17
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold
• Example: Connecting to Yahoo and store its address for reuse.

• Socket socket = new Socket("www.yahoo.com", 80);


SocketAddress yahoo = socket.getRemoteSocketAddress();
socket.close();

• Later on, we can use the closed socket info using the
address:

• Socket socket2 = new Socket();


socket2.connect(yahoo);

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 18
6.3.4.2 Proxy Servers
• The proxy server that a socket uses is controlled by the socksProxyHost
and socksProxyPort system properties.

• These system properties are applicable for all the sockets in the system.

• However if you need to modify the proxy server for a single socket you
can employ the following constructor:
• public Socket(Proxy proxy)

• For example: to use a proxy server “myproxy.example.com” to connect a


socket identified by login.ibiblio.org on port 25

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 19
• SocketAddress proxyAddress = new InetSocketAddress("myproxy.example.com", 1080);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, proxyAddress)
Socket s = new Socket(proxy);
SocketAddress remote = new InetSocketAddress("login.ibiblio.org", 25);
s.connect(remote);

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 20
6.4 Getting information about a
socket
• Socket objects have several properties that are accessible through getter methods:
• Remote Address

• Remote Port

• Local Port

• Following getter methods can be used:


• public InetAddress getInetAddress() : tells the address of the remote host
public int getPort(): tells the port of the remote host
public InetAddress getLocalAddress(): tells the address of the local host
public int getLocalPort(): tells the local port from where socket is made.

• There are no setter methods since the details are extracted from a socket object.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 21
• There are no setter methods since the details are extracted from a socket
object.
• Example get a socket’s information:
public class
staticSocketInfo
void main(String[]
{ args) { Command:
for (String host : args) { java SocketInfo.java
try { www.oreilly.com
Socket theSocket = new Socket(host, 80); www.oreilly.com
System.out.println("Connected to " + theSocket.getInetAddress() www.elharo.com
+ " on port " + theSocket.getPort() + " from port "
login.ibiblio.org
+ theSocket.getLocalPort() + " of "
+ theSocket.getLocalAddress());
} catch (UnknownHostException ex)
{ System.err.println("I can't find " +
host);
} catch (SocketException ex)
{ System.err.println("Could not connect to " +
host);
22
}}}}
} catch (IOException ex) Compiled
{ by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold
Closed or Connected?
• The isClosed() method can be used to check either the socket is closed
or open?
• It returns boolean value; true if socket is open, false if socket is closed.

• Rather than handling IOException by working on closed socket one can


check socket is open before performing any task.
• if (socket.isClosed()) {
// do something...
} else {
// do something else...
}

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 23
• However it is still not perfect test.

• If the connection has never been made in the first place using the socket,
then isClosed() always returns false.

• Hence Socket class also has an isConnected() method, that tells weather a
socket has ever been connected to the remote host.

• It does not tell either the socket is currently connected or not.

• So to check either the socket is open we need to use both the methods as
follows:

boolean connected = socket.isConnected() && ! socket.isClosed();

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 24
toString()
• The Socket class overrides the standard toString() method to produce
a string that looks as follows:
• Socket[addr=www.oreilly.com/198.112.208.11,port=80,localport=50055]

• The toString() method converts the output obtained from


getInetAddress(), getPort() and getLocalPort() into a string arranged as
shown above.

• Primarily used for debugging.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 25
6.5 Setting Socket Options
• Socket options specify how the actual sockets send and receive
data.
• Java has nine options for client side sockets:
• TCP_NODELAY
• SO_BINDADDR
• SO_TIMEOUT
• SO_LINGER
• SO_SNDBUF
• SO_RCVBUF
• SO_KEEPALIVE
• OOBINLINE
• IP_TOSCompiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 26
TCP_NODELA
Y
• Setting TCP_NODELAY to true ensures that packets are sent as quickly as
possible regardless of their size.

• Normally small packets are combined into larger packets before being sent.
• Nagle’s Algorithm: wait for acknowledgement before sending another packet.
• Inefficient for GUI programs such as games and remote access

• Setting TCP_NODELAY to true avoids this buffering problem.

public void setTcpNoDelay(boolean on) throws SocketException


public boolean getTcpNoDelay() throws SocketException

Example: if (!s.getTcpNoDelay()) s.setTcpNoDelay(true);


Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 27
SO_LINGER
• This option specifies what to do with datagrams that have not yet been sent
but the socket is closed.

• By default, the close() method closes the socket immediately but the system
still tries to send the remaining data.

• If SO_LINGER is turned on and linger time is any positive value, the


close() method is blocked for specified time and the remaining data is sent
till the time expires.(Max linger time = 65535 seconds)
• public void setSoLinger(boolean on, int seconds) throws SocketException
public int getSoLinger() throws SocketException

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 28
SO_TIMEOUT
• While reading data from socket, the read() method will listen until the no
data is available for reading.

• By setting SO_TIMEOUT we can specify the maximum time limit in


milliseconds. When the time expires an InterruptedIOException is
thrown.

• It does not disconnect the socket and can again be used for reading
purpose.

public void setSoTimeout(int milliseconds) throws SocketException


public int getSoTimeout() throws SocketException
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 29

SO_RCVBUF and SO_SNDBUF
• Buffers are implemented in networks to improve performance.

• For fast connections (10 Mbps and more) large buffers tend to improve performance.

• While slower connections improve with use of small buffers.

• The SO_RCVBUF option controls the suggested buffer size for receiving data.
• The SO_SNDBUF option controls the suggested buffer size for sending data.
• public void setReceiveBufferSize(int size) throws SocketException, IllegalArgumentException

• public int getReceiveBufferSize() throws SocketException


public void setSendBufferSize(int size) throws SocketException, IllegalArgumentException
public int getSendBufferSize() throws SocketException
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 30
SO_KEEPALIV
E
•The SO_KEEPALIVE attribute is used to ensure that the server is
responding and has not crashed.

• It does so by sending a data packet over an idle connection.


• If the server does not respond to the packet, the client keeps trying for
approx. 11 minutes and finally closes the socket.

• Without this attribute a client can never know that the server is not
responding, and wait infinitely to get served.

public void setKeepAlive(boolean on) throws SocketException


public boolean getKeepAlive() throws SocketException
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 31
OOBINLINE
• In some cases client needs to send a single byte of urgent data, this data
must be sent even if there exists other data being transferred.

• The receiver also processes this urgent data with high priority.

• The following method is used to send urgent data:


• public void sendUrgentData(int data) throws IOException

• By default, Java ignores urgent data received from a socket. However it can
be enabled using the following methods:

• public void setOOBInline(boolean on) throws SocketException


public boolean getOOBInline() throws SocketException
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 32
SO_REUSEADDR
• In general, a single socket is bound to a single port.

• However, ports need to be used interchangeably. Java by default does not allow
sharing of the port simultaneously.

• To enable simultaneous communication between shared port


SO_REUSEADDR
attribute can be turned on.

• It allows another socket to bind to a port even if the data may be present for
previous socket.

• The following methods are used:


• public void setReuseAddress(boolean on) throws SocketException
33
public boolean getReuseAddress()
Compiled throws
by Manoj Pokharel@ Nepathya College,SocketException
Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold
IP_TOS Class of Service
• Different types of Internet service have different performance needs.
• Example: Video Chat and Email

• The class of service is stored in eight bit field called IP_TOS in the IP
header.

• Java allows to inspect and set a value of service using following


methods:

• public int getTrafficClass() throws SocketException


public void setTrafficClass(int trafficClass) throws SocketException

• The service class is provided as an int between 0 and 255.


Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 34
6.6 Socket in GUI
Applications
•Whois

• A Network Client
Library

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 35
End of Unit

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 36
Unit 7 : Sockets for Clients

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 1
Introduction
• Previous Chapter: Socket for Clients
• Discussed sockets from clients viewpoint

• But client socket cannot work on own without server socket.

• Facts in Server side scokets:


• Client knows the address and port of the remote host
• But a server does not. (Any machine with the address and port info can connect to server)
• Analogy: caller and a Receptionist

• In other words, server sockets wait for connections while client sockets initiate connections.

• When a client on a remote host attempts to connect to that port, the server wakes up,
negotiates the connection between the client and the server, and returns a regular
Socket object representing the socket between the two hosts.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 2
7.1 Using ServerSockets
• The ServerSocket class contains everything needed to write servers in
Java.

• Has a constructor that create new ServerSocket objects,


• methods that listen for communications on a specified port.,

• methods to configure server socket options and

• usual methods.

• Lets discuss a basic life cycle of a server program:

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 3
• Basic life of a server program:

1. A new ServerSocket is create on a particular port using a ServerSocket() constructor

2. The ServerSocket listens for incoming connection on the port using its accept() method.

3. The accept() method returns a Socket object that connects the client and the server.

4. The Socket’s getInputStream() or getOutputStream methods are used as per need.

5. The client and server interact until the socked is closed

6. The server, client or both close the connection

7. Returns to 2nd step and waits for next connection

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 4
Implementing a NIST time
Server
• Create a server socket that listens on port 13.

• ServerSocket server = new ServerSocket(13)

• Accept a connection

• Socket connection = server.accept(); // The program waits until a client


connects on port 13. when a client connects, the method returns a socket
object.

• Since the daytime server only speaks, we get OutputStream from socket and
the format is text we chain it to OutputStreamWriter.
• OutputStream out = connection.getOutputStream();
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 5
Writer writer = new OutputStreamWriter(writer, "ASCII")
• Now, we get the time and write it into the outputstream.

• Date now = new Date();


out.write(now.toString() +"\r\n"); // carriage return/linefeed terminates
the line.

• Finally flush the connection and close it.

• out.flush(); // send any remaining data residing in socket.


connection.close();

• Is it done? NO!!!
• The server cannot exit after a request is processed. It must listen for other
clients continuously.
• Solution: put all these steps inside a loop.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 6
• The final code becomes: (This is called an iterative server)
• ServerSocket server = new ServerSocket(port);
while (true) {
try (Socket connection = server.accept()) {
Writer out = new OutputStreamWriter(connection.getOutputStream());
Date now = new Date();
out.write(now.toString() +"\r\n");
out.flush();
} catch (IOException ex) {
// problem with one client; don't
shut down the server
System.err.println(ex.getMessage
}}
()); Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4 edition, Elliotte Rusty Harold 7
th
7.2 Serving Binary
Data
• Sending binary data is quite simple. (we have done at very beginning ch. 2, 3)

• For serving binary data, we use an OutputStream that writes a byte array
rather than a writer that writes a string.
• i.e. We typecast the data as byte and write it to the OutputStream.

• Lets see an example:

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 8
Public class ServesBd{
Public final static int PORT = 37;
Public static void main (String[] args){
try(ServerSocket server = new ServerSocket(PORT)){
while (true){
try(Socket connection = server.accept()){

OutputStream out = connection.getOutputStream();


Date now = new Date();
long ms = (byte) now.getTime();
out.write(time);
out.flush();}
//catch blocks }}}
} Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 9
Writing to Servers with Sockets
• In previous example, a server sent data to the client using the
outputstream.

• Now we illustrate how a client sends data to the server.

• It is quite simple:

• Accept a connection as before


• Create a InputStream to receive data from client

• Create a OutputStream to send data to client

• A simple echo server represents this task:

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 10
• public Void call() throws IOException {
try {
InputStream in = new BufferedInputStream(connection.getInputStream());
OutputStream out = connection.getOutputStream();
int c;
while ((c = in.read()) != -1) {
out.write(c);
out.flush();
}
} catch (IOException ex) {
System.err.println(ex);
} finally
{ connection.close
();
}
}return null; Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 11
Closing Server Socket
• After the server socket’s task is completed, it must be closed.

• Closing a server socket frees the port for other programs and the port can be used by the other
programs.
• A server side socket can be closed using the close() method on the ServerSocket object and
follows a try catch finally sequence:
• ServerSocket server = null;
try {
server = new
ServerSocket(port);
// ... work with the server
socket
} finally {
if (server != null) {
try
{ server.close();
// ignore} Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold
} catch 12
7.2 Logging:
• Logging is the process of recording error and warnings that occur within a system.

• Since a server runs for long periods of time, it might be important to debug
what happened to the server during the run.

• Hence it is necessary to record server logs and store for some period of time.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 13
What to Log?
• Two things are primarily stored in log messages:

• Requests

• Server Errors

• Servers maintain two different logfiles for these two different items.

• The audit log usually contains one entry for each connection made to the server.

• The error log contains mostly unexpected exceptions that occur while the server
is running.
• For instance, a server must log NullPointerException since it indicates error in
the server that must be fixed.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 14
• However the error log does not contain any client side errors such as
unexpected disconnection and illegal requests. They are saved in request log.

• The error log exclusively records unexpected exceptions.

• Every entry in the error log must be investigated and resolved hence one should
ensure that everything should not be logged thinking that one might need it
someday

• As a developer only the important errors and exceptions must be logged. Error
logs that fill up with too many messages become ignored and useless.

• Also many systems provide several tools that can be used to analyze the recorded
logs. Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 15
How to Log?
• Many legacy programs still rely on third party logging libraries such as log4j or
Apache Commons Logging but Java has java.util.logging package available
that provides most needs.

• Use of the built in Java package eliminates the dependency of complex 3rd party
tools.

• Logging generally follows two steps:


1. Create a new Logger:
Syntax: Logger logger = Logger.getLogger(“requests”)
• The getLogger() takes a string parameter that identifies the name of logger. If the
logger with the same name exists then it is returned else new Logger is created.
• It is a good practice to name a new Logger after the current class using
class.getName() method.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4 edition, Elliotte Rusty Harold
th 16
2. Logging events:

• Loggers provide several methods for recording log events, however you need to
assign log level to determine the severity of the log.
• The log() method of the Logger class can be used to log a error/warning.

• Example: logger.log(Level.WARNING, “Unexpected Error!!!”);


• The above statement can be represented in shorthand as
follows: logger.warning(“Unexpected Error!!!”)
• There are following log levels arranged from left to right , where leftmost is
highest and rightmost is lowest.
• Level.SEVERE, Level.WANRING, Level.INFO, Level.CONFIG, Level.FINE,
Level.Finer, Level.Finest
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 17
7.3 Constructing Server Sockets: Constructing Without Binding
• There exist four public ServerSocket constructors:
• public ServerSocket(int port) throws BindException, IOException
public ServerSocket(int port, int queueLength) throws BindException, IOException
public ServerSocket(int port, int queueLength, InetAddress bindAddress) throws
IOException
public ServerSocket() throws IOException

• Example:

• Create a server socket that would be used by an HTTP server on port 80:

• ServerSocket httpd = new ServerSocket(80);


Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 18
• Create a server socket that would be used by an HTTP server on port 80
and queues upto 50 unaccepted connections.
• ServerSocket httpd = new ServerSocket(80, 50)

• Servers can also be configured to listen for connections on specified local


interface. Incoming request for connection on other interfaces are disabled.
• InetAddress local = InetAddress.getByName(“192.168.100.203”);
• ServerSocket httpd = new ServerSocket(80,10,local);

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 19
Constructing without binding
• The noargs constructor of the ServerSocket can be used to create a
ServerSocket object that doesnot bind to a port.

• Such object cannot accept any connections and can be bound later using the
bind() methods.
• public void bind(SocketAddress endpoint) throws IOException
public void bind(SocketAddress endpoint, int queueLength) throws
IOException

• It is primarily used to set server socket options before binding to a port.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 20
• The general pattern is:

ServerSocket ss = new ServerSocket();


//set socket options…..
SocketAddress http = new InetSocketAddress(80);
ss.bind(http)

• Putting 0 as port number selects an arbitrary port.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 21
7.4 Getting Information about Server
•Socket
All the ServerSockets occupy is the local address and the port.

• The ServerSocket class provides two getter methods that extract the local
address and port occupied by the server socket.
• Useful when you choose to use an arbitrary port by passing 0 as port value.

• The getter methods are:

• public InetAddress getInetAddress(): returns the address being used by the


server. If server has a single IP extract it using the InetAddress.getLocalHost().
In case of multiple only a single address is returned. Example:
• ServerSocket httpd = new ServerSocket(80);
InetAddress ia = httpd.getInetAddress()
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 22
• If the ServerSocket has not bound to a network interface, this method returns null.

• public int getLocalPort(): Since ServerSocket constructors allow you to listen on an


unspecified port by passing 0 as port number. You can use the getLocalPort() method to
determine the arbitrary port.

• If server is not bound to a port it returns -1.

• Example: determine the chosen random port.


try {
ServerSocket server = new ServerSocket(0);
System.out.println("This server runs on port "
+ server.getLocalPort());
} catch (IOException ex) {
System.err.println(ex);}}}
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4 th edition, Elliotte Rusty Harold 23
7.5 Socket Options
• The ServerSocket relies on following three options:
• SO_TIMEOUT

• SO_REUSEADDR

• SO_RCVBUF

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 24
SO_TIMEOUT
• SO_TIMEOUT defines the amount of time in milliseconds, that accept() waits for
an incoming connection.

• If no connection is made till timeout an InterruptedIOException is raised.

• If the timeout value is set to 0, accept() will never timeout. (default = never time
out)

• The setter and getter methods are:


• public void setSoTimeout(int timeout) throws SocketException
public int getSoTimeout() throws IOException

• What happens if timeout value is set as a negative number. =>


IllegalArgumentException
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4 th edition, Elliotte Rusty Harold 25
SO_REUSEADDR
• This option is similar for both client and sockets.

• Setting the SO_REUSEADDR to on allows a single port to be utilized


by multiple sockets.

• The getter and setter methods are:

• public boolean getReuseAddress() throws SocketException


public void setReuseAddress(boolean on) throws SocketException

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 26
SO_RCVBUF
• The SO_RCVBUF option sets the default receive buffer size for client
sockets accepted by the server socket.

• It is read and written by these two methods:

• public int getReceiveBufferSize() throws SocketException


public void setReceiveBufferSize(int size) throws SocketException

• Similar to clientside options.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 27
Class of Service
• Different services have different performance needs.

• For example live streaming video requires relatively high bandwidth while email can
be held for several hours without any harm.

• The ServerSocket specifies class of service using three parameters using numbers 1,
2 and 3 (higher number higher priority) that represent
• ConnectionTime, latency and bandwidth in order.

• The setter method is:


• public void setPerformancePreferences(int connectionTime, int latency, int bandwidth)

• For instance ss.setPerformancePreferences(2, 1, 3); indicates that the socket requires


maximum priority for bandwidth, minimum priority for latency and middle priority
for
connection timComepi.edl by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4 th edition, Elliotte Rusty Harold 28
7.6 HTTP
servers.
• A single File Server

• A redirector

• A full-fledged
server.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 29
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 30
Unit 8 : Secure Sockets

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 1
Why Secure Sockets?
• Several internet backbone organizations allow government organizations to
access customers internet traffic.
• AT&T copies each packet to its mining server that can be accessed by National
Security Agency (NSA)
• Britian’s GCHQ taps into fiber-optic cables that carry most of the world’s
traffic

• As an internet user, one needs to defend against the eavesdropper.

• To make connections more secure, sockets can be encrypted. This allows


the transaction (data transfer) to be confidential, authenticated and
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 2
accurate.
8.1 Secure Communications
• Communicating confidential information through public internet requires that
the data be encrypted.

• Most of the encryption schemes are based on the key. A key can be thought as of
a general password that can be alphaneumeric and/or special symbols.

• Plain text message + Mathematical Algorithm(Key) gives Encrypted Ciphertext.

• Two schemes of encryption are: Symmetric encryption and Asymmetric


encryption.

• In symmetric encryption the same key is used for encryption and decryption
i.e. E(k) = D(k)
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 3
• In Asymmetric encryption, different keys are used for encryption and
decryption purpose. i.e. E(K1) ≠ D(K2).

• Example of both illustrating private and public key.

• So how do you implement there cryptographic schemes?

• Java Secure Socket Extension (JSSE) : allows to create sockets and


server sockets that handle the mechanisms necessary for secure
communication.
• And then utilize the sockets and streams studied in previous chapters.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 4
8.2 Creating Secure Client
Sockets
• Using an encrypted SSL socket to an existing secure server is
simple.
• Create a SocketFactory object using SSLSocketFactory’s getDefault() method.
• SocketFactory factory = SSLSocketFactory.getDefault();

• Create a Socket object using the createSocket() method of the SocketFactory.


• Socket socket = factory.createSocket(“login.ibiblio.org”, 3400);

• The createSocket() method provides five overloaded forms.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 5
public abstract Socket createSocket(String host, int port) throws IOException, UnknownHostException

public abstract Socket createSocket(InetAddress host, int port) throws IOException

public abstract Socket createSocket(String host, int port, InetAddress interface, int localPort) throws
IOException, UnknownHostException

public abstract Socket createSocket(InetAddress host, int port, InetAddress interface, int localPort)
throws IOException, UnknownHostException

public abstract Socket createSocket(Socket proxy, String host, int port, boolean autoClose) throws
IOException

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 6
Example: Sending data to server from
client
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
Socket socket = factory.createSocket("login.ibiblio.org", 7000);
Writer out = new OutputStreamWriter(socket.getOutputStream(), "US-ASCII");
out.write("Name: John Smith\r\n");
out.write("Product-ID: 67X-89\r\n");
out.write("Address: 1280 Deniston Blvd, NY NY 10003\r\n");
out.write("Card number: 4000-1234-5678-9017\r\n");
out.write("Expires: 08/05\r\n");
out.flush();

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 7
8.3 Event Handlers
• Network communications are slow compared to the speed of most computers.

• Secured communications are even slower.

• The necessary key generation and a connection setup can take several seconds.

• Java uses the event model to notify programs when the handshaking
between client and server is complete.

• To handle notifications you need to implement the HandshakeCompletedListener


interface.

• public interface HandshakeCompletedListener extends java.util.EventListener

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 8
• The interface declares the handshakeCompleted() method that receives
HandshakeCompletedEvent as an argument.

public void handshakeCompleted(HandshakeCompletedEvent event)

• The HandshakeCompletedEvent is a public class that provides four methods for getting
information about the event.

• HandshakeCompletedListener objects register their events form a particular SSLSocket via


its addHandshakeCompletedListener() and removeHandshakeCompletedListener()
methods.

• public abstract void addHandshakeCompletedListener(HandshakeCompletedListener listener)


public abstract void removeHandshakeCompletedListener(HandshakeCompletedListener listener)
throws IllegalArgumentException
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4 th edition, Elliotte Rusty Harold 9
8.4 Session Management
• SSL is commonly employed on web servers.

• Accessing every page on the server requires a separate socket.

• For example, consider checking out of Amazon requires loading seven


different pages and if it required 10 seconds to establish a secure
connection to a single page, 70 seconds would be lost.

• Solution: because of high overhead involved in handshaking between


two hosts for secure communications, SSL allows sessions to be
established that extend over multiple sockets.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 10
• Different sockets within the same session use the same set of public and private
keys. If connection to Amazon.com takes seven sockets, all seven sockets will
be established in the same session using the same keys.

• Only the first socket will suffer the overhead time of key generation and
exchange.

• As a programmer, there is no any extra work that needs to be done. If you open
multiple secure sockets to one host on one port within short period of time. JSSE
will reuse the session key automatically.

• However in high security applications, you may want to disallow session-


sharing between sockets (Out of Scope)
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 11
Client Mode
• In general, the server side party is required to authenticate itself using the appropriate
certificate but client does not require it.

• For example, if I wish to buy a book from Amazon and access the Amazon server, then the
server must prove that it is indeed the Amazon server and not other server.

• But in some cases the client needs to authenticate itself.

• The setUseClientMode(boolean mode) method defines either authentication is necessary


or not?

• If mode = true, it indicates that the socket is in client mode and does not need
any authentication. (server acts as client and no authentication necessary)

• If mode = false, it indicates that the server needs to authenticate itself. (default)
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 12
• The respective getter method can be used to determine either authentication
is necessary or not. (getUseClientMode();)

• Similarly a server can instruct all the clients to authenticate by using


the setNeedClientAuth(boolean value); in its SSLServerSocket object.

• If value = true, indicates that the clients invoking the server must
authenticate.

• If value = false, indicates that the clients do not need to authenticate.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 13
8.6 Creating Secure Server
Socket
• The server side secure sockets are provided by the SSLServerSocketFactory
abstract class that extends ServerSocketFactory.

public abstract class SSLServerSocket extends ServerSocket

• Like SSLSocketFactory an instance of SSLServerSocketFactory is returned by


SSLServerSocketFactory.getDefault() method.
• ServerSocketFactory ssf = SSLServerSocketFactory.getDefault();

• Then use the createServerSocket() method on the ServerSocketFactory object.

• ServerSocket ss = ssf.createServerSocket(int port);

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 14
8.7 Configure SSLServerSockets
• After creating a SSLServerSocket object, the next task is to adjust its behavior.

• The behavior defines how the socket operates. The following behavior can
be defined.
• Choosing the Cipher Suites

• Session Management

• Client Mode

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 15
Choosing the Cipher Suites
• The SSLServerSocket class has three methods to manipulate the cipher suites.

• public abstract String[] getSupportedCipherSuites() : Returns all the cipher suites


supported by the server socket.
public abstract String[] getEnabledCipherSuites(): returns all the enabled cipher
suites in the server socket.
public abstract void setEnabledCipherSuites(String[] suites): adds the new cipher
suite and enables it in the socket.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 16
Session Management
• Client and server both must agree to establish a session.

• The server uses the following methods to specify weather this will be allowed.

• public abstract void setEnableSessionCreation(boolean allowSessions): if


allowSessions is set to true, allows session establishment.
public abstract boolean getEnableSessionCreation(): returns either the session
establishment is supported or not.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 17
Client Mode
• public abstract void setNeedClientAuth(boolean flag)
public abstract boolean getNeedClientAuth()

• Discussed earlier

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 18
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 19
Unit 9 : Non-Blocking I/O

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 1
Non-Blocking IO
•Introduction
Compared to CPUs, memory and even disks, networks are slow.

• When a command is executed the CPU has to wait for some time until the data
complete data is fetched or sent.
• We say that the I/O operation blocks other resources in that case.

• Traditionally multithreading was implemented to handle this problem. (Classic


Multithreaded I/O)

• However Java now supports Non-blocking I/O. (NIO)

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 2
• Blocking I/O

• Blocking IO wait for the data to be write or read before returning. Java IO's
various streams are blocking. It means when the thread invoke a write() or
read(), then the thread is blocked until there is some data available for read, or
the data is fully written.
• Non blocking I/O

• Non blocking IO does not wait for the data to be read or write before
returning. Java NIO non- blocking mode allows the thread to request writing
data to a channel, but not wait for it to be fully written. The thread is
allowed to go on and do something else in a mean time.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 3
An Example Client
• Implementing a client that takes advantage of new I/O API, begin by invoking
the static factory method SocketChannel.open() that takes a SocketAddress
object indicating the host and the port to connect to.

SocketAddress rama = new InetSocketAddress("rama.poly.edu", 19);


SocketChannel client = SocketChannel.open(rama);

• The channel is opened in blocking mode, and if it was a traditional client,


you’d now ask for the socket’s input and/or output streams.

• With a channel, you need to write to the channel itself. Rather than writing
byte arrays, we write ByteBuffer objects. [ByteBuffer objects have lines of
text each having 74 characters followed by a carriage return]
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 4
• Now we create a ByteBuffer that has 74-byte capacity using the static allocate() method.

ByteBuffer buffer = ByteBuffer.allocate(74);

• And pas this object to channel’s read() method. The channel fills the buffer with the data
it reads from the socket. int bytesRead = client.read(buffer);

• Extracting the byte array from the byte buffer requires flipping the buffer and
chaining buffer to a WritableByteChannel object

WritableByteChannel output = Channels.newChannel(System.out);

buffer.flip();
output.write(buffer);

buffer.clear();
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 5
An Example Server
• As in client invoke the ServerSocektChannel.open() method and chain it to
a ServerSocket using a the above obtained object before binding it to a port.

ServerSocketChannel serverChannel = ServerSocketChannel.open();

ServerSocket ss = serverChannel.socket();
ss.bind(new InetSocketAddress(19));

• Now accept for incoming connections using accept() method that


returns a SocketChannel object.

SocketChannel clientChannel = serverChannel.accept();

• And finally make the channel non-blocking:

clientChannel.configureBlocking(false);
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 6
9.2 Buffers
• In NIO all the I/O is buffered i.e. instead of reading and writing via inputstreams
and outputstreams, read and write are performed via buffers.

• A buffer is an fixed size list of primitive data type such as an integer, character,
long integer etc.

• The subclasses of buffer are: ByteBuffer, CharBuffer, ShortBuffer, IntBuffer,


LongBuffer, FloatBuffer and DoubleBuffer each with their own defined methods.

• Every buffer also has common methods to track key pieces of information:

• Position, Capacity, Limit and Mark

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 7
• Position: It denotes the next location in the buffer that will be read from or
written to. It starts at 0 and has a maximum value equal to the size of buffer. It
can be set or get with these two methods.
• public final int position()
public final Buffer position(int newPosition)

• Capacity: The maximum number of elements the buffer can hold. This is set
when the buffer is created and cannot be changed thereafter. It can be read using:
• public final int capacity()

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 8
• Limit: it denotes the end of accessible data in the buffer. Reading or writing past
this point without changing the limit.

public final int limit()


public final Buffer limit(int newLimit)

• Mark: An index specified by client in the buffer. Ait can be set at current
position by invoking the mark() method. The current position is set to the
marked podition by invoking the reset() method.

public final Buffer mark()


public final Buffer reset()

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 9
• The buffer superclass also provides other methods:

• public final Buffer clear(): clear method sets the sets the position to 0 and the limit to
the capacity. If any data is written, it replaces the existing data.

• public final Buffer rewind(): it sets the position to zero but dowsnot change the limit.

• public final Buffer flip(): sets the limit to the current position and the position to zero.

• public final int remaining(): the remaining method returns the number of elements in
the buffer between the current position and the limit.
public final boolean hasRemaining(): it returns true if the number of remaining
elements is greater than zero.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 10
Creating Buffers
• To deal with buffers one should know the subclass of the buffer and code
according to the buffer used.

• Each buffer has several factory methods that create implementation


specific subclasses of that type. Empty buffers are created by allocate
methods and prefilled buffers by using the wrap methods.

• Allocate methods are generally used for input and wrap methods for
output purpose.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 11
Allocation:

• The basic allocate() method returns a new, empty buffer with fixed capacity. For
example the statements below create buffers of byte and int each of size 100.

ByteBuffer buffer1 = ByteBuffer.allocate(100);


IntBuffer buffer2 = IntBuffer.allocate(100)

• The initial position is 0 and the buffer created by allocate() will be


implemented on top of a Java array and can be accessed by the array() method.

byte[] data1 = buffer1.array();


int[] data2 = buffer2.array();

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 12
Direct Allocation: (Only in ByteBuffer)

• The ByteBuffer class has an additional allocateDirect() method that allocates


memory.

• Unlike allocate() it is not backed by Java’s array.

• Using array methods over these objects throw an


UnsupportedOperationException.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 13
Wrappin
•g Existing array of data can be put in buffer by using wrap() method and the
process is known as wrapping.

• Rather than allocating an new buffer and copying the data one by one, we
convert the data into array and use suitable buffer to wrap the data.

• For example:

byte[] data = "Some data".getBytes("UTF-8");


ByteBuffer buffer1 = ByteBuffer.wrap(data);
char[] text = "Some text".toCharArray();
CharBuffer buffer2 = CharBuffer.wrap(text);
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 14
Filling and Draining
• Buffers are designed for sequential access.

• The read and write operations of a buffer are controlled by the position and
limits.

• Position points somewhere between zero and the max limit where the next
data will be read from or written into.

• The buffer’s position is incremented by one when an element is read from or


written to buffer.

• For example an CharBuffer with capacity 10 and fill 4 characters

CharBuffer buffer = CharBuffer.allocate(10);


buffer.put('H');
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 15
buffer.put('e');
• Position now becomes 2. The process of putting the items into buffer is known as filling
the buffer.

• Buffer can be filled up to limit and exceeding the limit leads to


BufferOverflowException.

• Draining is the process of getting the data stored in the buffer. If you directly use the get()
method from the buffer you would get the null character or the last character. (Position
points to empty space or last location)

• Hence we flip the buffer using buffer.flip(), it sets limit to the position and position to 0
and now drain it into a new string.

String result = "";


while (buffer.hasRemaining()) {
result += buffer.get();
}
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 16
• Buffer class also has absolute methods that fill and drain at specific positions within
the buffer without updating the position. The ByteBuffer has following methods:

public abstract byte get(int index)


public abstract ByteBuffer put(int index, byte b)

• Accessing invalid positions or invalid limit throw an IndexOutOfBoundsException

• Example:

CharBuffer buffer = CharBuffer.allocate(12);


buffer.put(0, 'H');
buffer.put(1, 'e');
buffer.put(2, 'l');
buffer.put(3, 'l');

buffer.put(4, C'oom'p)iled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4 th edition, Elliotte Rusty Harold
17
Bulk Methods
• Rather than filling and draining one element at a time, buffers allow to work with
blocks of data.

• Working with blocks of data makes it faster and easier to manipulate data.

• Different buffer classes have bulk methods that fill and drain an array of their type

• The ByteBuffer has following get() and put() methods.


public ByteBuffer get(byte[] dst, int offset, int length)// read the data into dst array starting from offset
position till the length.
public ByteBuffer get(byte[] dst) // retrieves the whole buffer content at once.
public ByteBuffer put(byte[] array, int offset, int length) //put data from source array starting from offset
position to the specified length
public ByteBuffer put(byte[] array)// puts the array content at once
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 18
• The put method throws BufferOverflowException if the buffer doesnot have
sufficient space for the array or subarray.

• The get method throws BufferUnderflowException if the buffer doesnot have


enough data to fill the array.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 19
Data Conversion
• All data in Java can be resolved into bytes. i.e. any primitive data supported by
Java such as int, double, float, char etc. can be written as byts.

• The ByteBuffer class provides abstract methods to put and get data to and from a
buffer.

• Some methods are:

public abstract char getChar()

public abstract ByteBuffer putChar(char value)

public abstract char getChar(int index)

public abstract ByteBuffer putChar(int index, char value)


Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 20
View Buffers
• When a ByteBuffer read from a SocketChannel contains elements only of one
particular primitive data type, it is beneficial to create a view buffer.

• View buffer is an Buffer object of appropriate type such as DoubleBuffer,


IntBuffer etc. that draws its data from from underlying byte buffer.

• View buffers can be created with one of these six methods in ByteBuffer
objects.

public abstract ShortBuffer asShortBuffer()

public abstract CharBuffer asCharBuffer()

public abstract IntBuffer asIntBuffer()

public abstract LongBuffer asLongBuffer


21
So on for FlCoomaptiBeldubyfMfaenroj PaonkhdarelD@ NoepuatbhylaeCoBel ugef, Rfee.f rJava Network Programming, 4 th
• Example:

try {
SocketAddress address = new InetSocketAddress(args[0], port);
SocketChannel client = SocketChannel.open(address);
ByteBuffer buffer = ByteBuffer.allocate(4);
IntBuffer view = buffer.asIntBuffer();

for (int expected = 0; ; expected++) {


client.read(buffer);
int actual = view.get();
buffer.clear()

..

.. Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 22
Compacting Buffers
• Compacting buffers is the process of shifting any remaining data in the buffer to
the start of the buffer.

• Compacting is quite useful while a buffer acts as an intermediate component


during the copying process.

• Compacting sets the new buffer position point to the end of data hence new data
can be written.

• Following buffers support compact method. ByteBuffer, IntBuffer,


ShortBuffer, FloatBuffer, CharBuffer and DoubleBuffer

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 23
Duplicating Buffers
• Buffers can be duplicated to deliver the same information over multiple
channels. The following classes have duplicate method that allow to duplicate
buffers.
public abstract ByteBuffer duplicate()
public abstract IntBuffer duplicate()
public abstract ShortBuffer duplicate()
public abstract FloatBuffer duplicate()
public abstract CharBuffer duplicate()
public abstract DoubleBuffer duplicate()

• The duplicated buffers share the same data and changes in one buffer are reflected
in another. Hence these methods are recommended to use while only reading
from the buffers.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 24
Slicing Buffers
• Slicing is a variant of duplicating.

• Slicing also creates a new buffer that shares the data with original buffer, however
the slice’s zero position is the current position of the original buffer and the
slice’s capacity goes upto the source buffer limit.

• In other words we say that a slice is a sub-buffer of original buffer that


contains elements from current position to the limit.

• It is important to separate data often required for separate class or method.

• The ByteBuffer, IntBuffer, ShortBuffer, FloatBuffer, CharBuffer and


DoubleBuffer all have slice() method.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 25
Marking and Resetting
• public final Buffer mark()

public final Buffer reset()

• Already discussed earlier.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 26
Object Methods
• All the buffer classes provide the common equals(), hashCode() and toString() methods.

• Two buffers are considered to be equal if:


• They have same type (i.e. ByteBuffer is never equal to an IntBuffer)
• They have same number of elements in the buffer and
• The elements at the same relative positions are equal to each other

• CharBuffer buffer1 = CharBuffer.wrap("12345678");


CharBuffer buffer2 = CharBuffer.wrap("5678");
buffer1.get();
buffer1.get();
buffer1.get()
;
buffer1.get()
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 27
;
• The hash-code returns an integer and two buffers have same hashcode if and only
if they are equal.

• The toString method is used for debugging purpose and displays the position,
limit and capacity(empty) of the buffer

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 28
9.3 Channels
• Channels move blocks of data into and out of buffers to and from various I/O
sources such as sockets and files.

• We need to deal with two important channel classes; SocketChannel and


ServerSocketChannel

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 29
9.3.1 SocketChannel
• The SocketChannel class reads from and writes to TCP sockets and the data must
be encoded in ByteBuffer objects for reading and writing..

• Connecting:

• The SocketChannel class doesnot have any public constructors. Instead, you
create a new SocketChannel using one of the two static methods.
• public static SocketChannel open(SocketAddress remote) throws IOException
public static SocketChannel open() throws IOException

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 30
• The first method makes the connection as follows:

SocketAddress address = newInetSocketAddress("www.cafeaulait.org", 80);


SocketChannel channel = SocketChannel.open(address);

• The noargs version doesnot immediately connect and must be connected later
using connect() method, it helps configure non-blocking I/O.

SocketChannel channel = SocketChannel.open();


SocketAddress address = new InetSocketAddress("www.cafeaulait.org", 80);

channel.configureBlocking(false)

; channel.connect(address);

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 31
• Reading, Writing and Closing

• To read data from a SocketChannel, first create a ByteBuffer the channel can store
data and pass it to the read method.

• ByteBuffer buffers = ByteBuffer.allocate(1000);


while (buffers.hasRemaining() && channel.read(buffers) != -1) ;
……..

• To write data to buffer use the write() method as read() is used.

public abstract int write(ByteBuffer src) throws IOException

• Channels must be closed once task is completed . Use the close() method to
close the socket.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 32
9.3.2 ServerSocketChannel
• The ServerSocketChannel as one purpose to accept incoming connections,
as
SocketChannel it also uses open() method to create a new object.

• try {
ServerSocketChannel server = ServerSocketChannel.open();
SocketAddress address = new InetSocketAddress(80);
server.bind(address);

• The ServerSocketChannel object has the accept() method to listen for incoming
connections.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 33
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 34
Unit 10 : UDP

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 1
10.1 UDP
•Protocol
Data transmission over transport layer is based on use of two common
protocols: TCP and UDP.

• The TCP is a reliable protocol, in the sense that TCP ensures that the data
sent from one node must be received at another node at any cost.

• TCP handles error correction, quality of service and congestion control


that guarantee the delivery of data.

• But this reliability comes at the cost of speed, hence is not suitable for
short
transmissions.

• The UDP is an alternative to TCP that is very quick, but not reliable. Data
sent over Compiled
UDPbymay or may not arrive at the destination
Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4 edition, Elliotte Rusty Harold
th 2
• UDP is primarily used where speed matters than reliability.

• For example consider the real time audio and video, here speed matters as the lost
pixels remain static as in the previous frame.

• Another application of UDP is the Domain Name System (DNS).

• Since TCP and UDP have their own applications we will deal with UDP in this chapter.

• Implementation of UDP is handled by two classes DatagramPacket and


DatagramSocket.

• The DatagramPacket class stuffs bytes of data into UDP packets called datagrams and
unstuff datagrams at the receiving side.

• To send data over UDP, data must be put in a DatagramPacket and send the packet using a
DatagramSocket. DatagramPacket is received form the DatagramSocket and fetch
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4 edition, Elliotte Rusty Harold
th 3
• In UDP the destination address is included in the packet itself hence socket
only needs to know the port on which to listen for connections or port used to
send data.

• UDP can utilize a single DatagramSocket to send and receive data from many
hosts and a socket is not dedicated for a single connection as in TCP.

• Sending and receiving data is done as per each individual datagram


(No InputStreams and OutputStreams as TCP)

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 4
UDP
Clients
• We will implement NIST daytime server but this time using the UDP protocol.

• Step 1: Open a DatagramSocket at port 0 (Local Port):

DatagramSocket socket = new DatagramSocket(0);

• Step 2: timeouts are important for UDP than TCP since UDP is not reliable. So
set SOTIMEOUT socket option.
socket.setSoTimeout(10000);

• Now we need to set the packets, 1 packet to send request and another packet to
receive data.
InetAddress host = InetAddress.getByName("time.nist.gov");
DatagramPacket request = new DatagramPacket(new byte[1], 1, host, 13);
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 5
(byte[] = data to send, length of datagram, host to connect, remote port no)
byte[] data = new byte[1024];
DatagramPacket response = new DatagramPacket(data, data.length);

• Now we can send the 1st packet over the socket and receive response too.

socket.send(request);
socket.receive(response);

• Finally extract the bytes


form response and convert
them to string and print.

String daytime = new String(response.getData(), 0, response.getLength(),"US-ASCII");


System.out.println(daytime)

• The constructor, send() and receive methods throw an IOException hence iit is good
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 6
practice to wrap inside try catch block.
UDP
•Servers
A UDP server follows similar pattern as a UDP client. The key differences
are:
• It receives before sending

• Doesnot choose anonymous port to bind

• No separate serversocket class

• Example : Daytime Server Implementation:

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 7
try (DatagramSocket socket = new DatagramSocket(13)) {
while (true) {
try {
DatagramPacket request = new DatagramPacket(new byte[1024], 1024);
socket.receive(request);

String daytime = new Date().toString();


byte[] data = daytime.getBytes("US-ASCII");
DatagramPacket response = new DatagramPacket(data, data.length,
request.getAddress(), request.getPort());
socket.send(response);

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 8
10.4 DatagramPacket Class
• Describe UDP header

• IP header+ UDP
Header+ Data
= Datagram

• Data max limit is 65507


bytes but actual limit
in manu platforms is
8192 bytes.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 9
• A UDP datagram is represented by an instance of the DatagramPacket
class.

public final class DatagramPacket extends Object

• The class provides methods to


• get and set the source or destination address from the IP header

• get and set the destination port

• get and set the data

• get and set the length of data

• All these are the fields of UDP header.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 10
10.4.1 Constructors
• Different constructors depending on weather to send or receive the data.

• However, all these constructors take a byte array to hold datagram’s data and the
number of bytes in that array as the length.

• Following are the constructors for receiving datagrams:

public DatagramPacket(byte[] buffer, int length): if this constructor is used, when a socket
receives a datagram, it stores the datagram’s data beginning at buffer[0] till the length
bytes have been written into the buffer.
public DatagramPacket(byte[] buffer, int offset, int length): when it is used, storage begins
at buffer[offset]

• For both cases, length must be less than or equal to buffer length.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 11
• Example:

byte[] buffer = new byte[8192];


DatagramPacket dp = new DatagramPacket(buffer, buffer.length);

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 12
• Following constructors are used for sending datagrams:

public DatagramPacket(byte[] data, int length, InetAddress destination, int port):


public DatagramPacket(byte[] data, int offset, int length, InetAddress destination, int port)
public DatagramPacket(byte[] data, int length, SocketAddress destination)
public DatagramPacket(byte[] data, int offset, int length, SocketAddress destination)

• Each constructor has the data to send of length bytes as the byte array starting
at offset or 0 if offset is not specified.

• Destination address can be specified as separate InetAddress and a port OR


both combined as a single SocketAddress.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 13
• Following code creates a new DatagramPacket filled with data “This is a test”
in UTF-8 destined for port 7 of the www.ibiblio.org.
String s = "This is a test";
byte[] data = s.getBytes("UTF-8");
try {
InetAddress ia = InetAddress.getByName("www.ibiblio.org");
int port = 7;
DatagramPacket dp = new DatagramPacket(data, data.length, ia, port);
// send the packet...
} catch (IOException ex)
}

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 14
10.4.2 The get
Methods
• DatagramPacket has six get methods to extract different parts of a datagram including the data
itself and several parts of header.

• public InetAddress getAddress(): returns the InetAddress obj containing the address of the
remote host.

• public int getPort(): returns an integer that specifies the remote port.

• public SocketAddress getSocketAddress(): returns a SocketAddress object that contains


the address and port of the remote host.

• public byte[] getData(): returns a byte array containing the data from the datagram.

• public int getLength(): returns the number of bytes of data in the datagram

• public int getOff s e t( ) : r e t u r n s t h e p o i n t f r o m


C om p i le d by M a n oj Po kh ar el @ Ne pa thy a C olle g e , R
w
ef. Ja
h e r e
va N et w or k
15
10.4.3 The setter
Methods
• Java provides several methods for modifying the data, remote address, port
number after the datagram has been created.

• public void setData(byte[] data): it changes the payload of the UDP datagram. It is useful
when data is large and is divided into multiple datagrams. Object can be utilized to send
multiple data over the same object.

• public void setData(byte[] data, int offset, int length): overloaded setData()
method that permits changes to existing datagram object. Useful
when the size of existing datagram needs to be modified.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 16
• public void setAddress(InetAddress remote): changes the destined address of a datagram
packet. Useful to utilize a single object to send data to multiple recipients.

• public void setPort(int port): changes the destination port of a datagram.

• public void setAddress(SocketAddress remote): changes both the address and port of
the destination.

• public void setLength(int length): changes the number of bytes of data in the internal
buffer. It is often used to discard the unused space.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 17
10.5 DatagramSocket class
• To send or receive a DatagramPacket, you must open a datagram socket. In Java,
a datagram socket is created and accessed through the DatagramSocket class.
public class DatagramSocket extends Object

• All datagram sockets bind to a local port, on which they listen for incoming data.
• For a client, the local port is not of interest, any unused random port can
be used.
• For a server, clients need to know the port on which the server is listening
hence the local port must be modified.

• Unlike TCP , there is no any distinction between the client sockets


and server sockets.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 18
10.5.1 The
•Constructors
The DatagramSocket has overloaded constructors used for different situations:

• public DatagramSocket() throws SocketException: creates a socket bound to an


anonymous port. We use this constructor for a client that initiates conversation with a
server.

try {
DatagramSocket client = new DatagramSocket();
// send packets...
} catch (SocketException ex) {
System.err.println(ex);
}
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 19
• public DatagramSocket(int port) throws SocketException: creates a socket that listens for
incoming datagrams on a particular port. We use this constructor to write server side
program that listens for incoming connections on a port.
• Two programs can use the same port if one program uses UDP and another uses TCP

• public DatagramSocket(int port, InetAddress interface) throws SocketException: creates a


socket that listens over a specific port and a network interface. Generally used to gain root
privilege such as using well known ports and using specific network interface.

• public DatagramSocket(SocketAddress interface) throws SocketException: similar to


above but reads both address and port from SocketAddress.

SocketAddress address = new InetSocketAddress("127.0.0.1", 9999);


DatagramSocket socket = new DatagramSocket(address);
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 20
10.5.2 Sending and Receiving
•Datagrams
The primary task of the DatagramSocket is to send and receive UDP datagrams.
One socket can both send and receive the datagrams. Two methods are
primarily used to send and receive the data.

public void send(DatagramPacket dp) throws IOException

public void receive(DatagramPacket dp) throws IOException

• After the send and receive tasks are completed the port
occupied by the socket
must be freed. For that we use the close() method.

public void close()


Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 21
10.5.3 Managing Connections
• Unlike TCP, datagram sockets can send and receive data from any host trying to
communicate with it.

• But it is not what we want, for example; an applet must be allowed only to talk
to the applet host.

• The methods to be discussed later will help you manage from whom to
receive and send datagrams while rejecting others.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 22
• public void connect(InetAddress host, int port): it specifies that the DatagramSocket will only
send packets to and receive packets from the specified remote host on the specified remote
port.
• Any attempt to send packets to other host throws IllegalArgumentException while packets
from other hosts will be discarded.

• public void disconnect(): it breaks the connection established by the connect() method. i.e. the
socket can send and receive packets from any host when disconnect() method is called.

• public int getPort(), public InetAddress getInetAddress(), public InetAddress


getRemoteSocketAddress(): these three getter methods can be used to extract the socket
information after the connect() method is called. They work as ypu would expect.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 23
10.6 Socket Options
• Java supports six socket options for UDP:

• SO_TIMEOUT: specifies the amount of time in milliseconds that receive() waits for an
incoming datagram before throwing InterruptedException. Has following methods:

public void setSoTimeout(int timeout) throws SocketException


public int getSoTimeout() throws IOException

If timeout = 0, socket never expires.


• SO_RCVBUF: similar to socket option of TCP Sockets.
• SO_SNDBUF: Similar to socket option of TCP Sockets.

Both methods set the suggested buffer size for receiving and sending purpose
respectively.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 24
• • SO_REUSEADDR: Defines whether multiple sockets can bind to the same port
and address at the same time. Methods are similar to TCP.
• SO_BROADCAST: controls either the socket is allowed to send and receive
packets from broadcast addresses . Following methods control it:
public void setBroadcast(boolean on) throws SocketException
public boolean getBroadcast() throws SocketException

•IP_TOS: similar to TCP, defines a traffic class between 0 to 255, that prioritize the
datagrams.

public int getTrafficClass() throws SocketException


public void setTrafficClass(int trafficClass) throws SocketException

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 25
10.7 UDP
•Applications
UDP client

• UDP Server

• UDP Echo Cient

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 26
10.8 DatagramChannel: Using datagram
channel
• The DatagramChannel class is used for non-blocking UDP applications(Remember
SocketChannel and ServerSocketChannel were used for Nonblocking TCP
applications)

• Opening a socket: the java.nio.channels.DatagramChannel class provides


implementation for DatagramChannel. It doesnot have a public constructor but a static
open() method to create objects. i.e. DatagramChannel channel =
DatagramChannel.open();

• This channel is not bound to any port, hence it must be bind using the socket()
method over the channel that returns the DatagramSocket object. Finally use bind()
that takes SocketAddress object. For example:
SocketAddress address = new InetSocketAddress(3141);
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 27
• As the DatagramSocket class it also has send() and receive() method to send and
receive data over NIO.

public SocketAddress receive(ByteBuffer dst) throws IOException

public int send(ByteBuffer src, SocketAddress target) throws IOException

• It also provides a connect() method that allows connection over a particular


host.

SocketAddress remote = new InetSocketAddress("time.nist.gov", 37);


channel.connect(remote);

• And finally the write() method that acts an alternative to send and is used after the
connect() method is called.

• The close()Compiled
method at the end is used to free the occupied port after task is complete.
by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4 edition, Elliotte Rusty Harold
th 28
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 29
Unit 11 : IP
Multicast

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 1
Multicasting
• Multicasting refers to the process of sending data from one host to many different
hosts but not to everyone in the network.

• It is a broader term than unicast (point-to-point) communication and compact


than a broadcast communication.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 2
Multicast Addresses and
•Groups
The hosts that participate in the multicast communication are collectively
referred
as Multicast groups. A multicast group shares a multicast address.

• Each multicast group has a multicast address that identifies the group. All the
hosts within the group share the same multicast address. Hence multicast
address is the shared address of the multicast group.

• For IPV4 IP address in the CIDR 224.0.0.9/4 are multicast addresses

• For IPV6, IP address in the group ff00::/8 are Multicast addresses

• Like any normal hostname, multicast addresses can have a hostname.


Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 3
Clients and Servers
• When a host wants to send data to a multicast group, the host puts that data in
multicast datagrams. Multicast datagrams are nothing other than UDP
datagrams addressed to a multicast group.

• One problem in UDP multicasting is the TTL value. TTL defines the maximum
intermediate hops an packet is allowed to pass. However in UDP, the TTL limits
the multicast geographically.

• For example, a TTL value of 16 limits the packet to local area while the TTL
of 127 sends the packet around the world.

• Intermediate values in order of 2n are also possible. However there is no any


precise way to map TTLs to geographic distance.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 4
• Once the data is stuffed into two or more datagrams, the sending host forward the
datagrams onto the internet. It is similar like sending UDP data.

• When the sender forwards multicast datagram to local network, the hosts in local
multicast group receive the datagram and the the datagram’s TTL is checked, if
the TTL is greater than 1 it is routed to other networks that have members of the
multicast group. The routing is handled by the routers.

• The receiving host accepts the datagram and processes it.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 5
Routers and Routing

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 6
• Routing for multicast datagrams can be achieved using one of the possible two
configurations; use of the multicast socket and without using multicast
sockets.

• Configuration on the left side of the previous figure utilizes the multicast
sockets. In this
configuration a single socket is used to send data from server’s router to the client’s router.
The client router then duplicates the stream and sends it to each of the clients.

• Without multicast sockets (figure in right) the server needs to use separate but
identical streams to send the data to the client’s router.

• Using a multicast socket is much more efficient as it reduces the bandwidth


requirement.
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 7
11.2 Working with Multicast Sockets
• In Java the java.net.MulticastSocket class, a subclass of java.net.DatagramSocket
provides implementation for multicast data.

• The MulticastSocket’s behavior is very similar to DatagramSocket.

• Following are the steps to be followed to receive a multicast data from a


remote site:

• Create a MulticastSocket using the constructor, make sure to specify the port to
listen for connections and data.

MulticastSocket ms = new MulticastSocket(2300);

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 8
• Now join a multicast group using the MulticastSocket’s joinGroup() method.

InetAddress group = InetAddress.getByName("224.2.2.2");


ms.joinGroup(group);

• Now receive the data as done in UDP; use a byte array and
a DatagramPacket

byte[] buffer = new byte[8192];


DatagramPacket dp = new DatagramPacket(buffer,
buffer.length);
ms.receive(dp);

• Finally, leave the group and close the multiast socket.

ms.leaveGroup(group)
; ms.close();
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 9
11.2.1 Constructors
• There are three constructors that allow you to listen over a random port, or over
the specified port and over a specific network interface and port combined
together.

• public MulticastSocket() throws SocketException


public MulticastSocket(int port) throws SocketException
public MulticastSocket(SocketAddress bindAddress) throws IOException

• Example:

• MulticastSocket ms1 = new MulticastSocket();


MulticastSocket ms2 = new MulticastSocket(4000);

SocketAddress address = new InetSocketAddress("192.168.254.32", 4000);


Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 10
• MulticastSocket ms = new MulticastSocket(null);
ms.setReuseAddress(false);
SocketAddress address = new InetSocketAddress(4000);
ms.bind(address);

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 11
11.2.2 Communcating with a Multicast Group
• Once a MulticatSocket has been created, it can perform four key operations:

• Joining a multicast groups

• Send data to members of the group

• Receive data from members of the group

• Leave the multicast group

• To join a group, pass an InetAddress or SocketAddress for the multicast group to the
joinGroup() method.
public void joinGroup(InetAddress address) throws IOException
public void joinGroup(SocketAddress address, NetworkInterface interface) throws IOException
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 12
• Receiving datagrams is similar as receiving the unicast datagrams.

• Sending datagrams is also similar to sending as in unicast datagrams.

• After that, use the leaveGroup() method to stop receiving datagrams from
specified multicast group.
public void leaveGroup(InetAddress address) throws IOException
public void leaveGroup(SocketAddress multicastAddress, NetworkInterface interface)
throws
IOException

• Finally close the multicast socket using the close() method.

Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 13
Compiled by Manoj Pokharel@ Nepathya College, Ref. Java Network Programming, 4th edition, Elliotte Rusty Harold 14

You might also like