Professional Documents
Culture Documents
Actualnet
Actualnet
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
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.
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
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
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
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
Client Server
“Hello World” Client/Server
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 );
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
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),
– 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