17
Java Network Java Network Programming Programming Network Programming Network Programming Spring 2000 Spring 2000 Jeffrey E. Care Jeffrey E. Care [email protected] [email protected]

Java(2)

Embed Size (px)

Citation preview

Page 1: Java(2)

Java Network ProgrammingJava Network Programming

Network ProgrammingNetwork Programming

Spring 2000Spring 2000

Jeffrey E. CareJeffrey E. Care

[email protected]@ieee.org

Page 2: Java(2)

Netprog 2000 - Java Network Programming 2

Java OverviewJava Overview

Object-orientedObject-oriented Developed with the network in Developed with the network in

mindmind Built-in exception handlingBuilt-in exception handling Extensive standard class libraryExtensive standard class library

Page 3: Java(2)

Netprog 2000 - Java Network Programming 3

Important Java PackagesImportant Java Packages

java.netjava.net TCP/IP networkingTCP/IP networking

java.iojava.io I/O streams & utilitiesI/O streams & utilities

java.rmijava.rmi Remote Method InvocationRemote Method Invocation

java.securityjava.security Security policiesSecurity policies

java.langjava.lang Threading classesThreading classes

Page 4: Java(2)

Netprog 2000 - Java Network Programming 4

Java Sockets ProgrammingJava Sockets Programming

Java uses BSD-style sockets to Java uses BSD-style sockets to interface with TCP/IP services (interface with TCP/IP services (java.netjava.net package) package)

Java distinguishes between UDP, TCP Java distinguishes between UDP, TCP server & TCP client socketsserver & TCP client sockets

Behind-the-scenes classes do the Behind-the-scenes classes do the actual work & can be updated or actual work & can be updated or swapped out transparentlyswapped out transparently

Page 5: Java(2)

Netprog 2000 - Java Network Programming 5

IP Addresses & HostnamesIP Addresses & Hostnames

java.net.java.net.InetAddressInetAddress class class Represents a single IP addressRepresents a single IP address Factory class – no public constructorFactory class – no public constructor Performs transparent DNS lookups or Performs transparent DNS lookups or

reverse lookupsreverse lookups java.net.java.net.UnkownHostExceptionUnkownHostException

thrown if DNS system can’t find IP thrown if DNS system can’t find IP address for specific hostaddress for specific host

Page 6: Java(2)

Netprog 2000 - Java Network Programming 6

TCP Server SocketsTCP Server Sockets

java.net.java.net.ServerSocketServerSocket class class Binds to a local port to listen for initial Binds to a local port to listen for initial

connectionsconnections Can be bound to a local IP for multi-Can be bound to a local IP for multi-

homed machineshomed machines accept()accept() method returns a method returns a java.net.Socketjava.net.Socket, not an integer , not an integer descriptordescriptor

Page 7: Java(2)

Netprog 2000 - Java Network Programming 7

TCP Client SocketsTCP Client Sockets

java.net.Socketjava.net.Socket class class Combines socket with socket options Combines socket with socket options

(timeout, linger, keep alive, no delay, (timeout, linger, keep alive, no delay, etc)etc)

Encapsulates a Encapsulates a java.java.ioio..InputStreamInputStream and a and a java.java.ioio..OutputStreamOutputStream – can be – can be retrieved for use in a layered I/O systemretrieved for use in a layered I/O system

Page 8: Java(2)

Netprog 2000 - Java Network Programming 8

UDP SocketsUDP Sockets

java.net.java.net.DatagramSocketDatagramSocket class class Java makes no distinction between Java makes no distinction between

client/server for UDP socketsclient/server for UDP sockets Connected mode UDP supported in Connected mode UDP supported in

Java 2Java 2 Can be bound to both a local port & a Can be bound to both a local port & a

local IP address – multi-homed supportlocal IP address – multi-homed support Supports some socket options (timeout, Supports some socket options (timeout,

buffer size)buffer size)

Page 9: Java(2)

Netprog 2000 - Java Network Programming 9

UDP DatagramsUDP Datagrams

java.net.java.net.DatagramPacketDatagramPacket class class Expects a byte array of dataExpects a byte array of data Address optional for connected-mode Address optional for connected-mode

UDPUDP This class is final – can’t be extended!This class is final – can’t be extended! java.net.java.net.DatagramSocketDatagramSocket

instances can only send instances of instances can only send instances of java.net.java.net.DatagramPacketDatagramPacket

Page 10: Java(2)

Netprog 2000 - Java Network Programming 10

ThreadingThreading Java doesn’t support the notion of Java doesn’t support the notion of

forking processes; how do we support forking processes; how do we support concurrency?concurrency?

– Java was designed to support multi-Java was designed to support multi-threading!threading!

– In server environments we can spawn new In server environments we can spawn new threads to handle each client threads to handle each client

– Thread groups allow for collective control Thread groups allow for collective control of many threadsof many threads

Page 11: Java(2)

Netprog 2000 - Java Network Programming 11

Java ServletsJava Servlets

Servlets are the Java analog to CGIServlets are the Java analog to CGI Advantages of servlets: full access to Advantages of servlets: full access to

other Java APIs, persistence between other Java APIs, persistence between invocations, guaranteed portabilityinvocations, guaranteed portability

Servlets can be generic services or Servlets can be generic services or specific to HTTPspecific to HTTP

Page 12: Java(2)

Netprog 2000 - Java Network Programming 12

HTTP ServletsHTTP Servlets

javaxjavax.servlet.http..servlet.http.HttpServletHttpServlet classclass

Uses HTTP to receive Uses HTTP to receive requestsrequests and and generate generate responsesresponses

Full support for all HTTP methods, Full support for all HTTP methods, cookiescookies, sessions, persistent , sessions, persistent connectionsconnections

Servlets can be chained – example: de-Servlets can be chained – example: de-blink servletblink servlet

Page 13: Java(2)

Netprog 2000 - Java Network Programming 13

Java AppletsJava Applets

Client-side Java programs that run in a Client-side Java programs that run in a browserbrowser

Applets have special security Applets have special security restrictions called the applet sandboxrestrictions called the applet sandbox

Only applets loaded over the network Only applets loaded over the network are subject to the applet sandboxare subject to the applet sandbox

The applet sandbox is controlled by a The applet sandbox is controlled by a java.lang.java.lang.SecurityManagerSecurityManager

Page 14: Java(2)

Netprog 2000 - Java Network Programming 14

Applet SandboxApplet Sandbox

Can’t load libraries or define native methodsCan’t load libraries or define native methods Can’t access local host filesystemCan’t access local host filesystem Can’t open sockets to hosts other than Can’t open sockets to hosts other than

originating hostoriginating host Can’t use Can’t use Runtime.exec()Runtime.exec() Applet windows have a unique appearanceApplet windows have a unique appearance Restricted access to certain system Restricted access to certain system

propertiesproperties

Page 15: Java(2)

Netprog 2000 - Java Network Programming 15

Escaping the Applet SandboxEscaping the Applet Sandbox

Browsers can define their own security Browsers can define their own security policy via a new policy via a new security managersecurity manager

Applets can be signed and executed as Applets can be signed and executed as trusted contenttrusted content

Security policies may vary from browser Security policies may vary from browser to browser, even for signed appletsto browser, even for signed applets

Page 16: Java(2)

Netprog 2000 - Java Network Programming 16

Remote Method Invocation Remote Method Invocation (RMI)(RMI)

RMI is the Java analog to RPCRMI is the Java analog to RPC RMI servers use a naming service RMI servers use a naming service

(rmiregistry) to register (rmiregistry) to register remote objectsremote objects RMI servers use a special security policy RMI servers use a special security policy

implemented by implemented by RMISecurityManagerRMISecurityManager The default RMI transport mechanism is via The default RMI transport mechanism is via

TCP sockets – this is transparent to RMI TCP sockets – this is transparent to RMI code!code!

Any object transferred in an RMI call must Any object transferred in an RMI call must implement the Serializable interfaceimplement the Serializable interface

Page 17: Java(2)

Netprog 2000 - Java Network Programming 17

Java Naming & Directory Java Naming & Directory Interface (JNDI)Interface (JNDI)

JNDI provides a generic API that can be used JNDI provides a generic API that can be used to interface with any naming systemto interface with any naming system

JNDI uses SPIs (service provider interfaces) JNDI uses SPIs (service provider interfaces) to access many different types of naming & to access many different types of naming & directory services from the JNDI APIdirectory services from the JNDI API

Sun supplies JNDI SPIs for LDAP, NIS, COS Sun supplies JNDI SPIs for LDAP, NIS, COS (CORBA naming), RMI registry & local (CORBA naming), RMI registry & local filesystemfilesystem