IP Security Group Presentation

advertisement
Session-6
Lecturer:
Kalamullah Ramli
Electrical Engineering Dept.
University of Indonesia
Introduction to Java Networking
Networking Topics

Network Protocols in Java:





TCP
UDP
Multicast
Threaded and Non-Threaded examples
Remote Method Invocation (RMI)
OOP Lecture 2004
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Slide - 2
A Short History of Java Networking

(1/3)
The Dark Ages: C/C++ Networking and the
Need for Change


Before Java, simple network connectivity
required lots of code.
C/C++ provided many, many choices/options
for networking


Good for C programmers.
Most programmers did not use all these options.
OOP Lecture 2004
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Slide - 3
A Short History of Java Networking

(2/3)
Java 1.0




Basic networking capability with high level
abstraction of URL handlers.
Low level choices were missing (e.g., connection
timeouts.)
Raw socket classes were final--java.net.Socket
and java.net.ServerSocket could not be extended.
“One shot” implementations with
java.net.SocketImpl allowed only one
implementation in each VM.
OOP Lecture 2004
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Slide - 4
A Short History of Java Networking

JDK 1.1 (the big leap)





(3/3)
Added four common socket options.
Allowed raw data manipulations within high-level abstractions.
Removed final designations.
Added multicast connections (previously limited to LAN and/or
external native calls).
JDK 1.2 (a second big leap)

Addition of fine-grained Java Security Model.


ability to control processes/connections/ports with very fine granular
control over details.
Added functionality to APIs.
OOP Lecture 2004
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Slide - 5
Competing Networking Models: OSI vs. Internet
Whose Kung Fu is Better?

Two competing models are used to describe networking



Open Systems Interconnection Standard



Open Systems Interconnection (OSI) architecture
Internet Architecture
partitions network connectivity into seven layers.
published by OSI and International Telecommunications Union
(ITU) in series of “X dot” standards: X.25, X.400, etc.
Internet Architecture Standard



also known as “TCP/IP architecture” after its two key protocols
evolved from packet-switched ARPANET of DoD
Internet Engineering Task Force (IETF) maintenance.
OOP Lecture 2004
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Slide - 6
OSI in Action
End Host
Application
Presentation
End Host
Network traffic in the OSI
model must always travel
up and down the protocol
stack to be routed.
Application
Presentation
Session
Transport
Session
Nodes in Network
Transport
Network
Network
Network
Network
Data Link
Data Link
Data Link
Data Link
Physical
Physical
Physical
Physical
OOP Lecture 2004
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Slide - 7
Internet Architecture Model
Design theory: “rough consensus and running code”
Application Protocols
Transport Protocols
(TCP/UDP)
Internet Protocol (IP)
The IETF culture requires
that new layers provide a
protocol specification and at
least two or three running
implementations.
Likely, the four layer model
will not grow in complexity
(unless the internet does
first.)
Network Protocols
OOP Lecture 2004
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Slide - 8
Internet Architecture Model
(1/2)
The Internet Architecture is also flexible; applications can reach the
network layer directly, or work through protocols such as TCP/UDP and IP
APPLICATION
TCP UDP
IP
NETWORK
OOP Lecture 2004
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Slide - 9
Internet Architecture Model
(2/2)
Transport through each layer requires the use of additional headers.
Application Layer
DATA
Transport Layer
Internet Layer
Network Layer
OOP Lecture 2004
HEADER
HEADER
DATA
HEADER
HEADER
DATA
HEADER
HEADER
DATA
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Slide - 10
Network Protocols in Java (1/3)
In terms of protocols and services, Java offers API support for three
basic types of connectivity:
TCP -- Transport Control Protocol
UDP -- User Datagram Protocol
java.net
package
Mcast -- Multicast Protocol.
-- Basic Terminology -A Socket is an abstraction of a "communications link" between
machines over some network.
Socket communication is the same regardless of whether the
network connection is via a phone line, cable modem, ethernet, or
fiber-optic line.
A packet is a discrete quantity of information suitable for routed
transport over a shared network. Packet sizes are limited, so a
packet may be a fragment of a large file or message.
OOP Lecture 2004
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Slide - 11
Basic Terminology: IP Address (2/3)
Naming and Addressing
The “IPv4” addressing scheme uses 32-bit addresses, often
presented as a set of four octets.
Read from left to right, we progressively define a particular machine:
192.168.1.200
The “dotted quad” or “dotted decimal” format is quite common.
Numbering is controlled by the Internet Assigned Numbers Authority
(IANA), and later the Internet Corporation for Assigned Names and
Numbers (ICANN).
OOP Lecture 2004
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Slide - 12
Basic Terminology: Ports (3/3)
Most computers have a single network connection used for all data.
Most operating systems allow multi-tasking, so several network applications
can be running at once. How does the computer know which application gets
the data?
Ports allow us to specify specific applications on the host. The TCP and UDP
protocols attach port information (16 bit number) to each packet.
Machine2
Machine1
Machine3
Thus, when creating connections between computers, we need an IP
address as well as the port to specify the machine and client process
OOP Lecture 2004
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Slide - 13
The java.net.* Package
Key Classes:
java.net.InetAddress
IP Address structures, and services
java.net.ServerSocket
This class implements server sockets.
java.net.Socket
This class implements client sockets (also called
just "sockets").
java.net.URL
Class URL represents a Uniform Resource Locator,
a pointer to a "resource" on the World Wide Web.
OOP Lecture 2004
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Slide - 14
Address Representations
java.net.InetAddress:
-- A class representing an IP address
-- Performs validity checking on IP names
-- Thus, there are no public constructors!
-- Instead, just call a static class method
to obtain an InetAddress:
InetAddress
has no public
constructors
since it performs
validity checking
of all IP names
import java.net.*;
public class MyMachineName {
public static void main (String arg[]){
InetAddress local = null;
try {
local = InetAddress.getLocalHost();
} catch (UnknownHostException e){
System.err.println ("Identity Crisis!");
System.exit(0);
}
String strAddress = local.getHostName();
System.out.println ("Local Host = " + strAddress);
}
}
OOP Lecture 2004
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Slide - 15
Converting IP Numbers to String
import java.net.*;
This does things
public class MyMachineName {
the hard way!
public static void main (String arg[]){
InetAddress local = null;
Just call
try {
getHostAddress()
local = InetAddress.getLocalHost();
instead!
} catch (UnknownHostException e){
System.err.println
Lesson: Java’s
("Identity Crisis!");
net package has most
System.exit(0);
every method you
}
will need!
byte[] b = local.getAddress();
String strAddress=“”;
for (int i = 0; i < b.length; i++)
strAddress += ((int)255 & b[i]) + “.”;
System.out.println (“Local = “ + strAddress);
}
}
OOP Lecture 2004
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Slide - 16
Other InetAddress Services

public boolean isMulticastAddress();

static InetAddress[] getAllByName(String host);
Determines all the IP addresses of a host, given the
host's name.

static InetAddress getByName(String host);
Determines the IP address of a host, given the host's
name.
OOP Lecture 2004
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Slide - 17
Sockets: Constructors
A java.net.Socket provides an easy interface to a TCP connection.
Of the eight Socket constructors, there are generally four commonly used:
public Socket(InetAddress address, int port);
Creates a stream socket and connects it to the specified port number at the
specified IP address.
public Socket(InetAddress address, int port, InetAddress
localAddr, int localPort);
Creates a socket and connects it to the specified remote address on the
specified remote port.
public Socket(String host, int port);
Creates a stream socket and connects it to the specified port number on
the named host.
public Socket(String host, int port, InetAddress localAddr,
int localPort);
Creates a socket and connects it to the specified remote host on the
specified remote port.
OOP Lecture 2004
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Slide - 18
Sockets: Simple Usage
We can therefore create a simple Socket with:
Socket s = new Socket (“acme.gatech.edu”, 13);
We can obtain a Stream for receiving information with:
InputStream in = s.getInputStream();
We can obtain a stream for sending information with:
OutputStream out = s.getOutputStream();
OOP Lecture 2004
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Slide - 19
TCP/UDP Connections Distinguished
The foregoing examples used TCP connections: session oriented
connections with a high degree of congestion control and error
correction.
The Socket abstraction hides the SocketImpl class, which provides
these services. To that extent, TCP is said to provide “reliable”
services.
unreliable but fast
Datagram
TCP
error
correction;
reliable
TCP
There may be circumstances where we don’t want the overhead
associated with reliability. UDP, user datagram protocol, allows us to use
so-called ‘unreliable’ networking services. UDP is a datagram-oriented
protocol, meaning there is no session.
OOP Lecture 2004
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Slide - 20
Reading and Writing from Sockets (1/2)




Sockets in Java are full duplex--meaning that
they can both send and receive information.
A good example of this feature is an echo
client. The echo service listens to Port 7, and
simply returns or ‘echoes back’ all information
received from the client.
An echo client, therefore, has to be able to
send and receive over the same socket.
Let’s look at an echo client written by Sun. . . .
OOP Lecture 2004
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Slide - 21
Reading and Writing from Sockets (2/2)
public class EchoClient {
public static void main(String[] args) throws IOException {
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
echoSocket = new Socket("taranis", 7);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println ("Don't know about host: taranis.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: taranis.");
System.exit(1);
}
BufferedReader stdIn =
new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println
("echo: " + in.readLine());
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}
OOP Lecture 2004
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Slide - 22
ServerSockets (1/3)
We may also wish to have a process listening on a port for incoming
calls. Perhaps we’ve written a web browser that stands ready to
send HTML content when a connection is made.
The ServerSocket class allows us to capture incoming calls.
ServerSocket outSock = new ServerSocket (6000);
while (true)
{
Socket inSock = outSock.accept();
handleConnection(inSock);
inSock.close();
}
OOP Lecture 2004
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Slide - 23
ServerSockets (2/3)
public ServerSocket(int port);
Creates a server socket on a specified port.
public ServerSocket(int port, int backlog);
Creates a server socket and binds it to the specified
local port number.
public ServerSocket
(int port, int backlog, InetAddress bindAddr);
Create a server with the specified port, listen backlog,
and local IP address to bind to.
public Socket accept();
Listens for a connection to be made to this socket and accepts it.
public void close();
Closes this socket.
public InetAddress getInetAddress();
Returns the local address of this server socket.
OOP Lecture 2004
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Slide - 24
ServerSockets (3/3)
When using ServerSockets, be aware: the call to “accept()”
causes the program to wait until the method returns. This
“blocking call” can cause a program to hang.
ServerSocket servSock = new ServerSocket (6000);
while (true)
{
Socket inSock = servSock.accept();
handleConnection(inSock);
inSock.close();
}
If other operations must take place, we need some way of
placing the “accept()” call in its own thread.
OOP Lecture 2004
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Slide - 25
To be continued…
Advanced Java Networking
OOP Lecture 2004
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Slide - 26
The End
QUESTIONS & COMMENTS ?
OOP Lecture 2004
Dr. –Ing. Ir. Kalamullah Ramli, M.Eng
Slide - 27
Download