Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 65

Java.

net Package
Networking in Java
• Packages
– java.net  Networking
– java.io  I/O streams & utilities
– java.rmi  Remote Method Invocation
– java.security  Security policies
– java.lang.Thread  Threading classes
– Java.sql  database connection
• Support at multiple levels
– Data transport  Socket classes
– Network services URL classes
– Utilities & security
Terms
Some Background

 Hosts
 Internet Addresses
 Ports 

 Protocols
• Socket

© 2000 Elliotte Rusty Harold 17-Feb-09


Hosts

 Devices connected to the Internet are


called hosts
 Most hosts are computers, but hosts also
include routers, printers, fax machines,
soda machines, bat houses, etc.

© 2000 Elliotte Rusty Harold 17-Feb-09


Hostnames
• IP Addresses are great for computers
– IP address includes information used for
routing.
• IP addresses are tough for humans to
remember.
• IP addresses are impossible to guess.
– ever guessed at the name of a WWW site?
The Domain Name System
• The domain name system is usually used
to translate a host name into an IP
address .or address to a name.

• Domain names comprise a hierarchy so


that names are unique, yet easy to
remember.
• Name of an internet address=domain
name
History of DNS
• When internet was
small mapping-Host
File.
Name Address
• Solution is to keep
entire host file in a
single computer and
allow access to it.
• Results in traffic.
History of DNS
• Another solution.

Divide the huge information into smaller


parts store on different computer.

Health Food Education


DNS Hierarchy

edu com org jp

rpi albany
Host name structure
• Each host name is made up of a sequence
of labels separated by periods.
– Each label can be up to 63 characters
– The total name can be at most 255 characters.
• Examples:
– whitehouse.gov
– barney.the.purple.dinosaur.com
– monica.cs.rpi.edu
Domain Name
• The domain name for a host is the
sequence of labels that lead from the host
(leaf node in the naming tree) to the top of
the worldwide naming tree.

• A domain is a subtree of the worldwide


naming tree.
Top level domains
• edu, gov, com, net, org, mil, …
• Countries each have a top level domain (2
letter domain name).
• New top level domains include:
.aero .biz .coop .info .name .pro
DNS Organization
• Distributed Database
– The organization that owns a domain name
is responsible for running a DNS server
that can provide the mapping between
hostnames within the domain to IP
addresses.
– So - some machine run by RPI is
responsible for everything within the
rpi.edu domain.
Internet addresses
 Every host on the Internet is identified by a
unique, four-byte Internet Protocol (IP)
address.
 This is written in dotted quad format like
199.1.32.90 where each byte is an unsigned
integer between 0 and 255.
 There are about four billion unique IP
addresses, but they aren’t very efficiently
allocated
© 2000 Elliotte Rusty Harold 17-Feb-09
Ports

 In general a host has only one Internet


address
 This address is subdivided into 65,536
ports
 Ports are logical abstractions that allow
one host to communicate simultaneously
with many other hosts
 Many services run on well-known ports.
For example, http tends to run on port 80
© 2000 Elliotte Rusty Harold 17-Feb-09
Note:

An IP address is a 32-bit address.


Note:

The IP addresses are unique.


Note:

The address space of IPv4 is


232 or 4,294,967,296.
Figure 4.1 Dotted-decimal notation
Note:

The binary, decimal, and hexadecimal


number systems are reviewed in
Appendix B.
Example 1

Change the following IP addresses from binary notation to


dotted-decimal notation.
a. 10000001 00001011 00001011 11101111
b. 11000001 10000011 00011011 11111111
c. 11100111 11011011 10001011 01101111
d. 11111001 10011011 11111011 00001111
Solution
We replace each group of 8 bits with its equivalent decimal
number (see Appendix B) and add dots for separation:
a. 129.11.11.239 b. 193.131.27.255
c. 231.219.139.111 d. 249.155.251.15
Example 2

Change the following IP addresses from dotted-decimal


notation to binary notation.
a. 111.56.45.78 b. 221.34.7.82
c. 241.8.56.12 d. 75.45.34.78

Solution
We replace each decimal number with its binary equivalent:
a. 01101111 00111000 00101101 01001110
b. 11011101 00100010 00000111 01010010
c. 11110001 00001000 00111000 00001100
d. 01001011 00101101 00100010 01001110
Example 3

Find the error, if any, in the following IP addresses:


a. 111.56.045.78 b. 221.34.7.8.20
c. 75.45.301.14 d. 11100010.23.14.67

Solution
a. There are no leading zeroes in dotted-decimal notation (045).
b. We may not have more than four numbers in an IP address.
c. In dotted-decimal notation, each number is less than or equal
to 255; 301 is outside this range.
d. A mixture of binary notation and dotted-decimal notation is
not
allowed.
Example 4

Change the following IP addresses from binary notation to


hexadecimal notation.
a. 10000001 00001011 00001011 11101111
b. 11000001 10000011 00011011 11111111
Solution
We replace each group of 4 bits with its hexadecimal
equivalent (see Appendix B). Note that hexadecimal notation
normally has no added spaces or dots; however, 0X (or 0x) is
added at the beginning or the subscript 16 at the end to show
that the number is in hexadecimal.
a. 0X810B0BEF or 810B0BEF16
b. 0XC1831BFF or C1831BFF16
Figure 4.2 Occupation of the address space
Addresses per class
Figure 4.3 Finding the class in binary notation
Figure 4.4 Finding the address class
Example 6

Find the class of each address:


a. 00000001 00001011 00001011 11101111
b. 11000001 10000011 00011011 11111111
c. 10100111 11011011 10001011 01101111
d. 11110011 10011011 11111011 00001111

Solution
See the procedure in Figure 4.4.
a. The first bit is 0. This is a class A address.
b. The first 2 bits are 1; the third bit is 0. This is a class C address.
c. The first bit is 0; the second bit is 1. This is a class B address.
d. The first 4 bits are 1s. This is a class E address..
Figure 4.5 Finding the class in decimal notation
Example 7

Find the class of each address:


a. 227.12.14.87 b.193.14.56.22 c.14.23.120.8
d. 252.5.15.111 e.134.11.78.56

Solution
a. The first byte is 227 (between 224 and 239); the class is D.
b. The first byte is 193 (between 192 and 223); the class is C.
c. The first byte is 14 (between 0 and 127); the class is A.
d. The first byte is 252 (between 240 and 255); the class is E.
e. The first byte is 134 (between 128 and 191); the class is B.
Java Networking Classes
• IP addresses
– InetAddress
• Packets
– DatagramPacket
• Sockets
– Socket
– ServerSocket
– DatagramSocket
• URLs
– URL
InetAddress Class
• Represents an IP address
• Can convert domain name to IP address
– Performs DNS lookup
• Getting an InetAddress object
– getLocalHost()
– getByName(String host)
– getByAddress(byte[] addr)
DatagramPacket Class
• Each packet contains
– InetAddress
– Port of destination
– Data
DatagramPacket Class
• Data in packet represented as byte array
DatagramPacket Methods
• getAddress()
• getData()
• getLength()
• getPort()
• setAddress()
• setData()
• setLength()
• setPort()
Socket Classes
• Provides interface to TCP, UDP sockets
• Socket
– TCP client sockets
• ServerSocket
– TCP server sockets
• DatagramSocket
– UDP sockets (server or client)
Socket Class
• Creates socket for client
• Constructor connects to
– Machine name or IP address
– Port number
• Transfer data via streams
– Similar to standard Java I/O streams
Socket Methods
• getInputStream()
• getOutputStream()
• close()
• getInetAddress()
• getPort()
• getLocalPort()
ServerSocket Class
• Create socket on server
• Constructor specifies local port
– Server listens to port
• Usage
– Begin waiting after invoking accept()
– Listen for connection (from client socket)
– Returns Socket for connection
ServerSocket Methods
• accept()
• close()
• getInetAddress()
• getLocalPort()
DatagramSocket Class
• Create UDP socket
– Does not distinguish server / client sockets
• Constructor specifies InetAddress, port
• Set up UPD socket connection
• Send / receive DatagramPacket
DatagramSocket Methods
• close()
• getLocalAddress()
• getLocalPort()
• receive(DatagramPacket p)
• send(DatagramPacket p)
• setSoTimeout(int t)
• getSoTimeout()
Socket
• Like an electrical socket.
• All of the sockets in your houses have a
particular address.
• Server uses ServerSocket method for
connection
• Client uses Socket method for connection.
Reserved Socket
• TCP/IP reserves lower 1024 ports for
specific protocol.
• 21--FTP
• 23-TELNET
• 25--E-Mail
• 80--HTTP
Communication Model

• A single client and server program connected through a bi-


directional channel such as a LAN

• sockets serve to connect a program to the network.

• Programs on different computers communicate through their


socket.
• Once established, using the connection is much like reading
and writing a file.
Internet
program socket socket program

Client Server
“Hello World” Client/Server

1. Server listens for connection on port 888.


2. Client connects on port 888.
3. Server writes out “Hello World” to connection.
4. Client reads in “Hello World” from connection.

2. Client connects on 1. Server listens for


port 888. “Hello World” connection on port
4. Client reads in 888.
“Hello World” 3. Server writes out
from connection. “Hello World” to
connection.

Client Server
Server Program

import java.net.*;
import java.io.*;
public class Server
{
public static void main(String args[]) throws Exception
{
ServerSocket connection = new ServerSocket( 888 );

Socket s = connection.accept();// connection wait

PrintStream out = new PrintStream(s.getOutputStream( ) );

out.println("Hello World");
}
}
Client Program

import java.net.*;
import java.io.*;
public class Client
{
public static void main(String args[]) throws Exception
{
Socket s = new Socket("localhost", 888 ); // Connect
BufferedReader in = new BufferedReader(new InputStreamReader(
s.getInputStream( ) ) );

System.out.println( in.readLine() );
}
}
TCP Protocol
• Connection Oriented
• Guarantees the safe delivery of
data(packet)
UDP Protocol

• Connectionless
• Packet can be lost during the
transmission.
How HTTP works?
Server Client
1. Listens to port 80 1. Connects to port 80
2.writes”GET/INDEX.html HTTP/1.0\n\n”
2. Accepts the connection

3. Reads up until the second End of Line(\n) 3.


Sees that GET is a known command
HTTP/1.0 is a valid protocol version
Reads a local file called INDEX.html
Writes “HTTP/1.0 200 OK\n” 200 means here comes the file.

4.Copies the contents of the file into the


socket 4.Reads the contents of the file and displays
it.
5.Hangs up 5. Hangs up
Note:-Its is not the actual working but it is the actual transactions for any web
server.
Proxy Servers
• A proxy server speaks the client side of a
protocol to another server.

SERVER Proxy SERVER


(Restrictions)
(No Restrictions)
CLIENT

CLIENT
CLIENT
CLIENT
Proxy Servers
• Proxy Servers would in turn communicate
for the client.
Proxy Servers-APLICATION
• When a popular website being hit by
hundreds of the users Clients),

• Proxy Server can get the contents of Web


server’s popular pages once.

• It saves expensive internet work transfers.


Datagrams
• Are the bundles of information passed
between machines.
• It uses UDP(unreliable.)
• Once the datagram released no assurance
to arrive at particular address.
• Even it is received no guarantee that it has
not been damaged in transit?
Datagrams
• Java implements datagrams on top of the
UDP protocol using two classes.

– DatagramPacket-Data container
– DatagramSocket-mechanism used to send or
receive the DatagramPacket.
URL Class
• Provides high-level access to network data
• Abstracts the notion of a connection
• Constructor opens network connection
– To resource named by URL
URL Constructors
• URL( fullURL )
– URL( "http://www.cs.umd.edu/class/index.html" )
• URL( baseURL, relativeURL )
– URL base = new URL("http://www.cs.umd.edu/" );
– URL class = new URL( base, "/class/index.html " );
• URL( protocol, baseURL, relativeURL )
– URL( "http", www.cs.umd.edu, "/class/index.html" )
• URL( protocol, baseURL, port, relativeURL )
– URL( "http", www.cs.umd.edu, 80,"/class/index.html" )
URL Methods
• getProtocol( )
• getHost( )
• getPort( )
• getFile( )
• getContent( )
• openStream()
• openConnection()
URL Connection Classes
• High level description of network service
• Access resource named by URL
• Can define own protocols
• Examples
– URLConnection  Reads resource
– HttpURLConnection  Handles web page
– JarURLConnection  Manipulates Java Archives
– URLClassLoader  Loads class file into JVM
Java Applets
• Applets are Java programs
– Classes downloaded from network
– Run in browser on client
• Applets have special security restrictions
– Executed in applet sandbox
– Controlled by java.lang.SecurityManager
Applet Sandbox
• Prevents
– Loading libraries
– Defining native methods
– Accessing local host file system
– Running other programs (Runtime.exec())
– Listening for connections
– Opening sockets to new machines
• Except for originating host
• Restricted access to system properties
Applet Sandbox
Network Summary
• Internet
– Designed with multiple layers of abstraction
– Underlying medium is unreliable, packet oriented
– Provides two views
• Reliable, connection oriented (TCP)
• Unreliable, packet oriented (UDP)
• Java
– Object-oriented classes & API
• Sockets, URLs
• Extensive networking support

You might also like