Download Document

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Implementation
of a new protocol
on top of IP
Agenda
The IP header
Benefits of a Java implementation
Obstacles of the Java implementation
Useful Java Classes
Examples in Java
The IP header
Benefits
Platform independent->pre-condition
Java Virtual Machine
Security ->separation from hardware by
Virtual Machine
Methods for IP-socket connections are
already implemented
Easy handling of multiple threads
possible
Obstacles
Handling devices isn‘t very easy->
hardware can‘t be accessed directly in
Java
Lack of low-level abstractions e.g.
handling raw byte arrays(needed for
implementation of system modules)
Java Classes
InetAddress
Socket
ServerSocket
Class InetAddress
Included in package java.net
The object InetAddress contains the IP
address and the hostname
Methods GetHostName and
GetHostAddress return the string values
GetAddress returns the IP address as
byte array directly
Generating InetAddress
object
Methods GetByName and
GetLocalHost:
public static InetAddress
getByName(String host) throws
UnknownHostException
public static InetAddress getLocalHost()
throws UnknownHostException
Code Example
import java.net.*;
public class Listing4501
{
public static void main(String[] args)
{
if (args.length != 1) {
System.err.println("Usage: java Listing4501 <host>");
System.exit(1);
}
try {
//Get requested address
InetAddress addr=InetAddress.getByName(args[0]);
System.out.println(addr.getHostName());
System.out.println(addr.getHostAddress());
} catch (UnknownHostException e) {
System.err.println(e.toString());
System.exit(1); } } }
Example output
java.sun.com
192.18.97.71
www.test.com
213.221.123.45
www.addison-wesley.de
194.163.213.76
Class Socket
Also included in package java.net
A socket connection consists of three
steps:
-Establishing connection
-Reading/writing data
-Closing connection
Establishing connection
Constructors for creating new sockets
Two examples:
public Socket(String host, int port)
throws UnknownHostException,
IOException
public Socket(InetAddress address, int
port) throws IOException
Reading/Writing data
After successful connection a data
stream can be sent
public InputStream getInputStream()
throws IOException
public OutputStream getOutputStream()
throws IOException
Closing connection
At the end of communication data input
and output stream should be closed
The socket itself should be closed also
ServerSocket
Constructor and method accept:
public ServerSocket(int port) throws
IOException
public Socket accept() throws
IOException
Conclusion
Java can be used to implement TCP/IP
stacks
Concerning performance Java isn‘t the
appropriate solution for a protocol
implementation
Books
Handbuch der Java-Programmierung
Thank you for your
attention!
Feel free to ask questions!
Related documents