Title: Java Networking
1Java Networking
2Introduction to Networking
- Encapsulation building data transmission
Request
Data Request
TCP Header
Request
TCP Request
IP Request
TCP Header
Request
IP Header
TCP Header
Request
IP Header
Ethernet Header
Eth Request
3Introduction to Networking
Client
Router
Server
Request
Request
TCP Segment
TCP Segnment
IP Datagram
IP Datagram
IP Datagram
Ethernet Frame
Ethernet Frame
Ethernet Frame Ethernet Frame
Voltage (phy)?
Voltage (phy)?
Voltage Voltage
4Introduction to Networking
- URL Uniform Resource Locator
http//www.somewhere.com8080/maps.html
protocol
Host / ip address
port
File / resource
5Introduction to Networking
Throwable
Exception
Error
ThreadDeath
VM Error
I/O
Runtime
EOF
NullPointer
OOfMem
6Introduction to Networking
- Java runtime Exceptions
- All exceptions caught with a try/catch block
- try
- something
- catch (someException ex)
- error something
-
- NOTE can have many different exceptions caught
per try block
7Introduction to Networking
- Java runtime Exceptions
- Major types of exceptions
- Error something failed in the JVM
- Exception normal errors in program execution
- RuntimeException null pointer oob errors
- Generating Exceptions
- Method Calls
- Runtime Exceptions
- User Generated
- Handling Exceptions
- Passing (ignore)?
- Catching
8Introduction to Networking
- Java runtime Exceptions
- Class Throwable
- Constructors
- Throwable() - create a Throwable object
- Throwable(string) create with string message
- Methods
- fillInStackTrace()?
- printStackTrace()?
- printStackTrace(PrintStream)?
- getMessage() - get detail message
9Introduction to Networking
- Java runtime Exceptions
- Class Throwable
- Constructors
- Throwable() - create a Throwable object
- Throwable(string) create with string message
- Methods
- fillInStackTrace()?
- printStackTrace()?
- printStackTrace(PrintStream)?
- getMessage() - get detail message
10Introduction to Networking Example Clients
- Standards based in RFCs
- RFCs
- www.ietf.org/rfc.html
- Internet Protocol assignments
- www.iana.org
- These definitions allow developers a guide to
what is available from other clients/servers
11Introduction to Networking Example Clients
- DNS Client RFC 1035
- Single Header for all queries
- 16 bit fields (network byte order)?
- ID (server to bounce back to client)?
- Query/Response flags (total 16 bits)?
- QR 1 bit
- OPCODE 4 bits
- AA, TC, RD, RA 1 bit
- Z 3 bits
- RCODE 4 bits
- QDCOUNT, ANCOUNT, NSCOUNT, ARCOUNT 16 bits
0
15
ID
QR
OPCODE
AA
TZ
RD
RCODE
RA
Z
QDCOUNT
ANCOUNT
NSCOUNT
ARCOUNT
12Introduction to Networking Example Clients
- ID 16 bits for identification
- QR bit
- 0 for query and 1 for response
- AA bit
- Set in response if Authoritative
- TC bit
- Set if truncated
- RD bit
- Set in query for recursion
- RA bit
- Set in response if recursion available
- Z bits
- Reserved (always need some extra!!!)?
- RCODE bits
- Error code in response (0 is ok)?
0
15
ID
QR
OPCODE
AA
TZ
RD
RA
Z
RCODE
QDCOUNT
ANCOUNT
NSCOUNT
ARCOUNT
13Introduction to Networking Example Clients
- QDCOUNT, ANCOUNT, NSCOUNT, ARCOUNT
- Represent number of questions
0
15
ID
QR
OPCODE
AA
RCODE
TZ
RD
RA
Z
QDCOUNT
ANCOUNT
NSCOUNT
ARCOUNT
14Introduction to Networking Example Clients
- Questions Each question has the same format
2
2
2
QNAME
QTYPE
QCLASS
- QNAME domain name or host name
- Name split into component labels
- e.g. www.abcdef.com - www, abcdef, com (3
labels)? - Each label written as a byte length, then text
- e.g. 3www6abcdef3com0 (each 1 byte)?
- Max Length of label is 63 bytes (1 for size, 1
for 0 term)? - QTYPE Type of resource required
- e.g. 255 all information desired
- QCLASS network class
- e.g. 1 - Internet
15Introduction to Networking Example Clients
2
2
2
2
n
n
NAME
TYPE
CLASS
TTL
RDLEN
DATA
- NAME n byte domain or host name
- TYPE unsigned short indicating type
- CLASS unsigned short indicating class
- TTL unsigned int indicating number of seconds
TTL - RDLEN unsigned short indicating length of DATA
- DATA n bytes of data
16Introduction to Networking Example Clients
- Implementation (classes)?
- DNS Base defaults /etc
- DNSQuery details of hostname (request/response
codes)? - DNSRR Resource Records
- record.Address record package address record
- record.MailExchanger Mail Exchanger record
(MX)? - DNSInputStream decode data returned
- NSLookup command-line nslookup class (main)?
- Command Syntax
- Java NSLookup lthostnamegt_at_ltnameservergt
- _at_ ltnameservergt optional
17Introduction to Networking Example Clients
- DNS Class
- Open RFC 1035
- Match Types to RFC 1035
- DNSQuery Class
- Vectors used for adding answers/etc
- Decode flags routine
- Walking through bits
- DNSInputStream
- ByteArrayInputStream used
- Ability to parse out pieces used in DNSRR
- DNSRR Class
- Superclass for all resource record classes
- record.Address
- Extends DNSRR Class
18Introduction to Networking Example Clients
public class NSLookup public static void main
(String args) if (args.length ! 1)?
throw new IllegalArgumentException
("Syntax NSLookup lthostnamegt_at_ltnameservergt")
int atIdx args0.indexOf ("_at_") String
nameServer (atIdx gt -1) ? args0.substring
(atIdx 1) "ns" String hostName (atIdx
gt -1) ? args0.substring (0, atIdx) args0
System.out.println ("Nameserver "
nameServer) System.out.println ("Request "
hostName) DNSQuery query new DNSQuery
(hostName, DNS.TYPE_ANY, DNS.CLASS_IN) try
Socket socket new Socket (nameServer,
DNS.DEFAULT_PORT) socket.setSoTimeout
(10000) sendQuery (query, socket)
getResponse (query, socket) socket.close
() printRRs (query) catch
(IOException ex) System.out.println
(ex)
19Introduction to Networking Example Server
- Class ServerSocket
- Constructor
- ServerSocket(int port)?
- ServerSocket(int port, int backlog)?
- ServerSocket(int port, int backlog, InetAddress
bindAddr)? - Methods
- accept() - throws IOException
- close()?
- getInetAddress()?
- getLocalPort()?
- setSoTimeout(int timeout)?
- getSoTimeout()?
- implAccept()?
- setSocketFactory(SocketImplFactory)
20Introduction to Networking Example Server
- Non Blocking Socket I/O
- available() - used in InputStream
- Socket Timeout Values
- Socket SO
- setSoTimeout(10)?
- Catch InterruptedIOException
21Introduction to Networking Example Server
Errors
WebBrowser
Web Server
Static
Dynamic CGI
Network
Database
Web Service
22Introduction to Networking Example Server
WebBrowser
Httpd
ReThread
HttpOuputStream
HttpInputStream
Network
HttpProcessor processRequest()?
main()?
HttpFile
HttpCGI
HttpClass
HttpException
HttpClassProcessor
RedirectException
CGI
Echo
23Introduction to Networking Example Server
- Class HTTP (HTTP.java)?
- Default values and static variables
- public static final String METHOD_GET "GET"
- public static final String METHOD_POST
"POST" - public static final String METHOD_HEAD
"HEAD" - public static final int STATUS_OKAY 200
- public static final int STATUS_NO_CONTENT
204 - public static final int STATUS_MOVED_PERMANENTLY
301 - public static final int STATUS_MOVED_TEMPORARILY
302 - public static final int STATUS_BAD_REQUEST
400 - public static final int STATUS_FORBIDDEN 403
- public static final int STATUS_NOT_FOUND 404
- public static final int STATUS_NOT_ALLOWED
405 - public static final int STATUS_INTERNAL_ERROR
500 - public static final int STATUS_NOT_IMPLEMENTED
501
24Introduction to Networking Example Server
- Class Httpd
- Main class
- public static void main (String args) throws
IOException - ServerSocket server new ServerSocket
(HTTP.PORT) - while (true)
- Socket client server.accept ()
- Httpd httpd new Httpd (client)
- ReThread reThread new ReThread (httpd)
- reThread.start ()
-
-
25Introduction to Networking Example Server
- Class HttpInputStream
- InputStream class
- public void readRequest () throws IOException
- String request readLine ()
- if (request null)?
- throw new HttpException (HTTP.STATUS_BAD_REQ
UEST, "Null query") - StringTokenizer parts new StringTokenizer
(request) - try
- parseMethod (parts.nextToken ())
- parseRequest (parts.nextToken ())
- catch (NoSuchElementException ex)
- throw new HttpException (HTTP.STATUS_BAD_REQ
UEST, request) -
- if (parts.hasMoreTokens ())?
- parseVersion (parts.nextToken ())
- else
- ..............
-
26Introduction to Networking Example Server
- Class HttpFile
- Class to deal with static files
- public HttpFile (HttpInputStream in) throws
IOException - if (in.getMethod () HTTP.METHOD_POST)?
- throw new HttpException (HTTP.STATUS_NOT_ALL
OWED, - "ltTTgt"
in.getMethod () " " in.getPath () "lt/TTgt") - file new File (HTTP.HTML_ROOT,
HTTP.translateFilename (in.getPath ())) - if (in.getPath ().endsWith ("/"))?
- file new File (file, HTTP.DEFAULT_INDEX)
- if (!file.exists ())?
- throw new HttpException (HTTP.STATUS_NOT_FOU
ND, - "File ltTTgt"
in.getPath () "lt/TTgt not found.") - if (file.isDirectory ())?
- throw new RedirectException
(HTTP.STATUS_MOVED_PERMANENTLY, - in.getPath ()
"/") - if (!file.isFile () !file.canRead ())?
- throw new HttpException (HTTP.STATUS_FORBIDD
EN, in.getPath ()) -
27Introduction to Networking Example Server
- Class HttpCGI
- Class to deal with dynamic requests
-
- public void processRequest (HttpOutputStream
out) throws IOException - ReThread drain null
- try
- if (in.getMethod () ! HTTP.METHOD_POST)
- cgi jvm.exec (cgiScript.getPath (),
env) - cgi.getOutputStream ().close ()
- out.write (cgi.getInputStream ())
- else
- cgi jvm.exec (cgiScript.getPath (),
env) - drain new ReThread (this)
- drain.start ()
- out.write (cgi.getInputStream ())
-
28Introduction to Networking Example Server